c++ - 如何将参数传递给Google Benchmark计划

标签 c++ google-api parameter-passing benchmarking google-benchmark

我有一个C++ Google Benchmark Program。它使用Google的BENCHMARK_MAIN()方法。现在,我使用Go脚本调用并执行编译后的程序。有没有办法将参数传递到基准程序中? (我知道主要方法的通用方法,但是我不确定如何在Googletest中进行操作,因为它是在benchmark_api.h中实现的,而我不能只是更改它。)

更新:

到目前为止,我已将宏主体复制到benchmark.cpp中并添加了一行。这不是一个很好的解决方案,因为Google可能对此宏进行了更改(例如更改名称或添加了代码行)不会影响我的副本。它终于在工作了。

int main (int argc, char** argv)
{
    MyNamespace::conf = {argv[1]};
    ::benchmark::Initialize (&argc, argv);
    ::benchmark::RunSpecifiedBenchmarks ();
}

最佳答案

破解整个BENCHMARK_MAIN函数当然是做到这一点的一种方法,但是IMO确实很麻烦而且很丑陋。所以我只想提出一种不同的方法:

// define your chunksize and iteration count combinations here (for i and j) 
static void CustomArguments(benchmark::internal::Benchmark* b) {
  for (int i = 0; i <= 10; ++i)
    for (int j = 0; j <= 50; ++j)
      b->Args({i, j});
}

// the string (name of the used function is passed later)
static void TestBenchmark(benchmark::State& state, std::string func_name) {
  // cout for testing purposes
  std::cout << state.range(0) /* = i */ << " " << state.range(1) /* = j */ 
  << " " << func_name << std::endl;
  for (auto _ : state) {
     // do whatever with i and j and func_name
  }
}

// This macro is used to pass the string "function_name1/2/3" 
// as a parameter to TestBenchmark
BENCHMARK_CAPTURE(TestBenchmark, benchmark_name1, "function_name1")
    ->Apply(CustomArguments);
BENCHMARK_CAPTURE(TestBenchmark, benchmark_name2, "function_name2")
    ->Apply(CustomArguments);
BENCHMARK_CAPTURE(TestBenchmark, benchmark_name3, "function_name3")
    ->Apply(CustomArguments);

BENCHMARK_MAIN()

然后在go脚本中,使用正则表达式过滤器调用基准测试:./prog_name --benchmark_filter=InsertRegexFilterHere
因此,例如:./prog_name --benchmark_filter=TestBenchmark/benchmark_name2/5/35
上面的示例将调用基准测试,并将传递“function_name2”,5和35(这些是您的块大小和迭代计数的值),因此输出将类似于:
------------------------------------------------------------------------------
Benchmark                                       Time           CPU Iterations
------------------------------------------------------------------------------
TestBenchmark/benchmark_name2/5/35               2 ns          2 ns  308047644

关于c++ - 如何将参数传递给Google Benchmark计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51519376/

相关文章:

plugins - Jenkins Gradle 插件 - 将参数作为 -P 而不是 -D 传递

python - 在 Airflow 中,最终用户可以将参数传递给与某些特定 dag 关联的键

c++ - 清除 unordered_map 时对 max_load_factor 和 bucket 计数的影响

c++ - 去除大写字母并延长字符串中的收缩

google-api - Google Cloud Resource Manager API - 向用户授予所有者角色

javascript - 使用 js 启动 Google 语音识别会出错 - 不允许

powershell - 将空值传递给 cmdlet 参数

c++ - std 命名空间中的 operator<< 有什么作用?

c++ - CMake 生成的 Xcode 项目无法找到从 Xcode 构建的二进制 "Products"

python - 谷歌 OAuth 与 Django