Skip to content

[BUG] --benchmark_min_time flag not working as intended #1663

@andreas-abel

Description

@andreas-abel

Consider the following example:

test.cc:

#include <benchmark/benchmark.h>

static void BM_Test(benchmark::State& state) {
  int i = 0;
  for (auto _ : state) {
    benchmark::DoNotOptimize(i++);
  }
}
BENCHMARK(BM_Test);

BENCHMARK_MAIN();
$ g++ test.cc -std=c++11 -isystem include -Lbuild/src -lbenchmark -lpthread -o test_b
$ time ./test_b --benchmark_min_time=30s

2023-09-15T14:58:41+02:00
Running ./test_b
Run on (128 X 4027.19 MHz CPU s)
CPU Caches:
  L1 Data 32 KiB (x64)
  L1 Instruction 32 KiB (x64)
  L2 Unified 512 KiB (x64)
  L3 Unified 16384 KiB (x16)
Load Average: 0.90, 0.85, 0.75
--------------------------------------------------------
Benchmark           Time             CPU      Iterations
--------------------------------------------------------
BM_Test          2.03 ns         2.03 ns      1000000000

real    0m2.274s
user    0m2.259s
sys     0m0.016s

Expected behavior
The benchmark runs for at least 30s.

Actual behavior
The benchmark runs for only a little more than 2s.

Additional context
The reason why the benchmark stops early is the hard-coded limit of 1,000,000,000 iterations in benchmark_runner.cc. However, 1,000,000,000 iterations can be reached relatively quickly on recent machines (if KeepRunningBatch() is used it can be reached even faster than in the example above).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions