-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Exclude setup/teardown from MemoryManager measurement window (#2149) #2247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devtejasx
wants to merge
2
commits into
google:main
Choose a base branch
from
devtejasx:fix-memory-manager-measurement-window
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+89
−3
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Verifies that a registered MemoryManager only brackets the benchmark's | ||
| // measured work: the per-benchmark Setup()/Teardown() must run outside the | ||
| // Start()/Stop() window, consistent with how they run outside the timed | ||
| // region. See https://github.com/google/benchmark/issues/2149. | ||
|
|
||
| #include <vector> | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace benchmark { | ||
| namespace { | ||
|
|
||
| // True while the MemoryManager measurement window is open, i.e. between | ||
| // MemoryManager::Start() and Stop(). The fixture callbacks must never observe | ||
| // it as true. Everything here runs on the main thread (thread_index 0), so no | ||
| // synchronization is needed. | ||
| bool in_measurement_window = false; | ||
|
|
||
| // Set from the callbacks so the test fails loudly rather than passing | ||
| // vacuously if a code path is never exercised. | ||
| bool memory_manager_ran = false; | ||
| bool setup_ran = false; | ||
| bool teardown_ran = false; | ||
|
|
||
| class OrderingMemoryManager : public MemoryManager { | ||
| public: | ||
| void Start() override { | ||
| in_measurement_window = true; | ||
| memory_manager_ran = true; | ||
| } | ||
| void Stop(Result& result) override { | ||
| in_measurement_window = false; | ||
| result.num_allocs = 0; | ||
| result.max_bytes_used = 0; | ||
| } | ||
| }; | ||
|
|
||
| void DoSetup(const State&) { | ||
| EXPECT_FALSE(in_measurement_window) | ||
| << "Setup ran inside the MemoryManager Start/Stop window"; | ||
| setup_ran = true; | ||
| } | ||
| void DoTeardown(const State&) { | ||
| EXPECT_FALSE(in_measurement_window) | ||
| << "Teardown ran inside the MemoryManager Start/Stop window"; | ||
| teardown_ran = true; | ||
| } | ||
|
|
||
| void BM_ordering(State& state) { | ||
| for (auto _ : state) { | ||
| } | ||
| } | ||
| BENCHMARK(BM_ordering)->Iterations(1)->Setup(DoSetup)->Teardown(DoTeardown); | ||
|
|
||
| // Swallows reporter output so the benchmark run does not pollute test output. | ||
| class NullReporter : public BenchmarkReporter { | ||
| public: | ||
| bool ReportContext(const Context&) override { return true; } | ||
| void ReportRuns(const std::vector<Run>&) override {} | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(MemoryManagerOrdering, SetupTeardownRunOutsideMeasurementWindow) { | ||
| OrderingMemoryManager mm; | ||
| RegisterMemoryManager(&mm); | ||
| NullReporter reporter; | ||
| const size_t ran = RunSpecifiedBenchmarks(&reporter); | ||
| RegisterMemoryManager(nullptr); | ||
|
|
||
| EXPECT_GT(ran, 0u); | ||
| EXPECT_TRUE(memory_manager_ran) << "MemoryManager measurement pass never ran"; | ||
| EXPECT_TRUE(setup_ran) << "Setup callback never ran"; | ||
| EXPECT_TRUE(teardown_ran) << "Teardown callback never ran"; | ||
| EXPECT_FALSE(in_measurement_window); | ||
| } | ||
|
|
||
| } // namespace benchmark |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure what this comment is adding to the code.