-
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
base: main
Are you sure you want to change the base?
Changes from all commits
1e37eb8
b4b9f73
f672117
11fdafe
4b0110a
a068d5a
0b8580a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| // 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, PauseBeforeTheLoopIsDiagnosed) { | ||
| ASSERT_DEBUG_DEATH(RunOne("BM_pause_before_loop"), "PauseTiming"); | ||
| } | ||
|
|
||
| TEST(Diagnostics, PauseAfterTheLoopIsDiagnosed) { | ||
| ASSERT_DEBUG_DEATH(RunOne("BM_pause_after_loop"), "PauseTiming"); | ||
| } | ||
|
|
||
| TEST(Diagnostics, ResumeBeforeTheLoopIsDiagnosed) { | ||
| ASSERT_DEBUG_DEATH(RunOne("BM_resume_before_loop"), "ResumeTiming"); | ||
| } | ||
|
|
||
| TEST(Diagnostics, ResumeAfterTheLoopIsDiagnosed) { | ||
| ASSERT_DEBUG_DEATH(RunOne("BM_resume_after_loop"), "ResumeTiming"); | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| } // namespace | ||
This file was deleted.
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.
this is a weird name for this test. shouldn't these tests be part of an existing googletest instead?