破解整个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