【问题标题】:google benchmark: Assertion `has_range_x_' failed谷歌基准:断言“has_range_x_”失败
【发布时间】:2016-08-02 16:30:59
【问题描述】:

在一个项目中,我从https://github.com/google/benchmark.git tag v1.0.0 开始使用 google/benchmark。

我运行了一个非常简单的测试

#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();

但我得到了错误

./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.

我尝试了主标签和旧标签,但我总是得到这个断言。我在 debian/testing 上使用 gcc 5.4.0 作为基准库和 simple-benchmark 可执行文件。

在我看来,它无法检测到参数列表的结尾并断言。但是有什么问题呢?如何预防?

【问题讨论】:

  • 你不需要像这样定义范围:BENCHMARK(BM_memcpy)->Range(8, 8
  • 实际上,根据文档,这应该几乎相同。我得到了同样的断言。
  • 您在计时中包括分配和归零。如果您没有这种开销,尤其是,您可以在更短的时间间隔内获得准确的结果。如果您打算使用非常大的缓冲区进行测试。
  • 问题不在于时机。它是最后的断言。

标签: c++ gcc benchmarking microbenchmark google-benchmark


【解决方案1】:

您注册了两次基准测试(调用BENCHMARK),但第二次您没有提供Arg。即,正如断言所断言的那样,您在没有设置范围的情况下调用它。

【讨论】:

    猜你喜欢
    • 2011-05-27
    • 2021-03-16
    • 2010-09-26
    • 2018-11-09
    • 2021-01-20
    • 2014-06-10
    • 2014-02-10
    • 2013-04-03
    相关资源
    最近更新 更多