Search code examples
c++gccbenchmarkingmicrobenchmarkgoogle-benchmark

google benchmark: Assertion `has_range_x_' failed


In a project i start to use google/benchmark from https://github.com/google/benchmark.git tag v1.0.0.

I run the very simple test

#include <benchmark/benchmark.h>
#include <cstring>

static void BM_memcpy(benchmark::State& state) {
  char* src = new char[state.range_x()]; char* dst = new char[state.range_x()];
  memset(src, 'x', state.range_x());
  while (state.KeepRunning())
    memcpy(dst, src, state.range_x());
  state.SetBytesProcessed(int64_t(state.iterations()) *
                          int64_t(state.range_x()));
  delete[] src;
  delete[] dst;
}
BENCHMARK(BM_memcpy)->Arg(8)->Arg(64)->Arg(512)->Arg(1<<10)->Arg(8<<10);

// Register the function as a benchmark
BENCHMARK(BM_memcpy);

BENCHMARK_MAIN();

but i get the error

./bench/simple-benchmark 
Run on (8 X 4000 MHz CPU s)
2016-08-02 18:22:30
Benchmark               Time           CPU Iterations
-----------------------------------------------------
BM_memcpy/8             9 ns          9 ns   79545455       877MB/s
BM_memcpy/64            9 ns          9 ns   56451613   6.67615GB/s
BM_memcpy/512          21 ns         21 ns   33018868   23.0185GB/s
BM_memcpy/1024         30 ns         29 ns   23648649   32.4039GB/s
BM_memcpy/8k          516 ns        514 ns    1346154   14.8415GB/s
simple-idl-benchmark: /usr/local/include/benchmark/benchmark_api.h:417: int benchmark::State::range_x() const: Assertion `has_range_x_' failed.

I tried master and older tags but i always get this assertion. I use gcc 5.4.0 on debian/testing for both the benchmark library and the simple-benchmark executable.

It seems for me, that it cannot detect the end of the argument list and it asserts. But what is wrong? How to prevent it?


Solution

  • You're registering the benchmark twice (the calls to BENCHMARK) but the second time you don't provide an Arg. Ie, you're calling it without setting a range, just as the assertion asserts.