-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Diagnose invalid PauseTiming()/ResumeTiming() calls (#2235) #2274
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
1
commit into
google:main
Choose a base branch
from
devtejasx:fix-timing-misuse-diagnostics
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.
Open
Changes from all commits
Commits
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
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,98 @@ | ||
| // Testing: | ||
| // State::PauseTiming() | ||
| // State::ResumeTiming() | ||
| // Test that the assertions in these functions diagnose calls made outside of | ||
| // the benchmark loop, and that a run they do not diagnose still reports a | ||
| // sane time. | ||
|
|
||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "benchmark/benchmark.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace { | ||
|
|
||
| void BM_pause_before_loop(benchmark::State& state) { | ||
| state.PauseTiming(); | ||
| for (auto _ : state) { | ||
| } | ||
| } | ||
| BENCHMARK(BM_pause_before_loop)->Iterations(1); | ||
|
|
||
| void BM_resume_before_loop(benchmark::State& state) { | ||
| state.ResumeTiming(); | ||
| for (auto _ : state) { | ||
| } | ||
| } | ||
| BENCHMARK(BM_resume_before_loop)->Iterations(1); | ||
|
|
||
| void BM_pause_after_loop(benchmark::State& state) { | ||
| for (auto _ : state) { | ||
| } | ||
| state.PauseTiming(); | ||
| } | ||
| BENCHMARK(BM_pause_after_loop)->Iterations(1); | ||
|
|
||
| void BM_resume_after_loop(benchmark::State& state) { | ||
| for (auto _ : state) { | ||
| } | ||
| state.ResumeTiming(); | ||
| } | ||
| BENCHMARK(BM_resume_after_loop)->Iterations(1); | ||
|
|
||
| void BM_pause_and_resume_in_loop(benchmark::State& state) { | ||
| for (auto _ : state) { | ||
| state.PauseTiming(); | ||
| state.ResumeTiming(); | ||
| } | ||
| } | ||
| BENCHMARK(BM_pause_and_resume_in_loop)->Iterations(1); | ||
|
|
||
| class CapturingReporter : public benchmark::BenchmarkReporter { | ||
| public: | ||
| bool ReportContext(const Context& /*context*/) override { return true; } | ||
| void ReportRuns(const std::vector<Run>& runs) override { | ||
| runs_.insert(runs_.end(), runs.begin(), runs.end()); | ||
| } | ||
|
|
||
| const std::vector<Run>& runs() const { return runs_; } | ||
|
|
||
| private: | ||
| std::vector<Run> runs_; | ||
| }; | ||
|
|
||
| std::vector<benchmark::BenchmarkReporter::Run> RunOne(const std::string& name) { | ||
| CapturingReporter reporter; | ||
| benchmark::RunSpecifiedBenchmarks(&reporter, name); | ||
| return reporter.runs(); | ||
| } | ||
|
|
||
| TEST(Diagnostics, PauseOutsideOfTheLoopIsDiagnosed) { | ||
| #ifndef NDEBUG | ||
| ASSERT_DEATH_IF_SUPPORTED(RunOne("BM_pause_before_loop"), "PauseTiming"); | ||
| ASSERT_DEATH_IF_SUPPORTED(RunOne("BM_pause_after_loop"), "PauseTiming"); | ||
| #endif | ||
| } | ||
|
|
||
| TEST(Diagnostics, ResumeOutsideOfTheLoopIsDiagnosed) { | ||
| #ifndef NDEBUG | ||
| ASSERT_DEATH_IF_SUPPORTED(RunOne("BM_resume_before_loop"), "ResumeTiming"); | ||
| ASSERT_DEATH_IF_SUPPORTED(RunOne("BM_resume_after_loop"), "ResumeTiming"); | ||
| #endif | ||
| } | ||
|
|
||
| TEST(Diagnostics, PauseAndResumeInsideTheLoopReportASaneTime) { | ||
| const std::vector<benchmark::BenchmarkReporter::Run> runs = | ||
| RunOne("BM_pause_and_resume_in_loop"); | ||
| ASSERT_EQ(runs.size(), 1u); | ||
| EXPECT_EQ(runs[0].skipped, 0u); | ||
| // One iteration of an empty loop. A whole second would mean an absolute | ||
| // clock reading was accumulated instead of a duration. | ||
| EXPECT_GE(runs[0].real_accumulated_time, 0.0); | ||
| EXPECT_LT(runs[0].real_accumulated_time, 1.0); | ||
| EXPECT_GE(runs[0].cpu_accumulated_time, 0.0); | ||
| EXPECT_LT(runs[0].cpu_accumulated_time, 1.0); | ||
| } | ||
|
|
||
| } // namespace | ||
This file was deleted.
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.
what about pause of paused, resume of resumed, pause of skipped, resume of skipped, etc?
Uh oh!
There was an error while loading. Please reload this page.
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.
And note that skipping can happen before/during/after the loop, and time may be either running or paused, so some more variations to test.
Oh, and we should actually test that the timings reported aren't absurd in the cases that aren't diagnosed.