Skip to content

Commit 57faaec

Browse files
committed
fix: skip empty benchmarks to avoid infinite loops when CODSPEED_MODE=off
When `CODSPEED_MODE=off`, empty benchmark bodies (which don't run the `KeepRunning()` loop) fail to advance iteration counts. Instead of triggering a hard `BM_CHECK` failure or stalling in an infinite loop, the runner now intercepts the state and gracefully drops a meaningful error message. References: - Issue: google/benchmark#1962 - Pull Request: google/benchmark#1976
1 parent 1c22e59 commit 57faaec

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

google_benchmark/src/benchmark_runner.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,11 @@ void RunInThread(const BenchmarkInstance* b, IterationCount iters,
143143

144144
State st = b->__codspeed_root_frame__Run(iters, thread_id, &timer, manager,
145145
perf_counters_measurement, profiler_manager_, is_warmup);
146-
BM_CHECK(st.skipped() || st.iterations() >= st.max_iterations)
147-
<< "Benchmark returned before State::KeepRunning() returned false!";
146+
if (!(st.skipped() || st.iterations() >= st.max_iterations)) {
147+
st.SkipWithError(
148+
"The benchmark didn't run, nor was it explicitly skipped. Please call "
149+
"'SkipWithXXX` in your benchmark as appropriate.");
150+
}
148151
{
149152
MutexLock l(manager->GetBenchmarkMutex());
150153
internal::ThreadManager::Result& results = manager->results;

google_benchmark/test/skip_with_error_test.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ ADD_CASES("BM_error_while_paused", {{"/1/threads:1", true, "error message"},
181181
{"/2/threads:4", false, ""},
182182
{"/2/threads:8", false, ""}});
183183

184+
void BM_malformed(benchmark::State&) {
185+
// NOTE: empty body wanted. No thing else.
186+
}
187+
BENCHMARK(BM_malformed);
188+
ADD_CASES("BM_malformed",
189+
{{"", true,
190+
"The benchmark didn't run, nor was it explicitly skipped. Please "
191+
"call 'SkipWithXXX` in your benchmark as appropriate."}});
192+
184193
int main(int argc, char* argv[]) {
185194
benchmark::Initialize(&argc, argv);
186195

0 commit comments

Comments
 (0)