From 5e387e7d33a55b8d6b7c5025379b97cc9418fabf Mon Sep 17 00:00:00 2001 From: Tobias Schmidt Date: Tue, 30 Mar 2021 15:43:03 +0200 Subject: [PATCH] Implement custom benchmark name (#1107) * Implement custom benchmark name The benchmark's name can be changed using the Name() function which internally uses SetName(). * Update AUTHORS and CONTRIBUTORS * Describe new feature in README * Move new name function up Fixes #1106 --- AUTHORS | 1 + CONTRIBUTORS | 1 + README.md | 15 +++++++++++++++ include/benchmark/benchmark.h | 3 +++ src/benchmark_register.cc | 5 +++++ test/reporter_output_test.cc | 24 ++++++++++++++++++++++++ 6 files changed, 49 insertions(+) diff --git a/AUTHORS b/AUTHORS index 3068b2eb..9b980419 100644 --- a/AUTHORS +++ b/AUTHORS @@ -52,6 +52,7 @@ Sayan Bhattacharjee Shuo Chen Steinar H. Gunderson Stripe, Inc. +Tobias Schmidt Yixuan Qiu Yusuke Suzuki Zbigniew Skowron diff --git a/CONTRIBUTORS b/CONTRIBUTORS index b5e1aa4f..8370a2d7 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -75,6 +75,7 @@ Roman Lebedev Sayan Bhattacharjee Shuo Chen Steven Wan +Tobias Schmidt Tobias Ulvgård Tom Madams Yixuan Qiu diff --git a/README.md b/README.md index 43b77ca7..0d827b8d 100644 --- a/README.md +++ b/README.md @@ -278,6 +278,8 @@ too (`-lkstat`). [Passing Arguments](#passing-arguments) +[Custom Benchmark Name](#custom-benchmark-name) + [Calculating Asymptotic Complexity](#asymptotic-complexity) [Templated Benchmarks](#templated-benchmarks) @@ -652,6 +654,19 @@ BENCHMARK(BM_StringCompare)->RangeMultiplier(2) ->Range(1<<10, 1<<18)->Complexity([](benchmark::IterationCount n)->double{return n; }); ``` + + +### Custom Benchmark Name + +You can change the benchmark's name as follows: + +```c++ +BENCHMARK(BM_memcpy)->Name("memcpy")->RangeMultiplier(2)->Range(8, 8<<10); +``` + +The invocation will execute the benchmark as before using `BM_memcpy` but changes +the prefix in the report to `memcpy`. + ### Templated Benchmarks diff --git a/include/benchmark/benchmark.h b/include/benchmark/benchmark.h index f57e3e79..2e714b6e 100644 --- a/include/benchmark/benchmark.h +++ b/include/benchmark/benchmark.h @@ -790,6 +790,9 @@ class Benchmark { // Note: the following methods all return "this" so that multiple // method calls can be chained together in one expression. + // Specify the name of the benchmark + Benchmark* Name(const std::string& name); + // Run this benchmark once with "x" as the extra argument passed // to the function. // REQUIRES: The function passed to the constructor must accept an arg1. diff --git a/src/benchmark_register.cc b/src/benchmark_register.cc index 65d9944f..c007876f 100644 --- a/src/benchmark_register.cc +++ b/src/benchmark_register.cc @@ -278,6 +278,11 @@ Benchmark::Benchmark(const char* name) Benchmark::~Benchmark() {} +Benchmark* Benchmark::Name(const std::string& name) { + SetName(name.c_str()); + return this; +} + Benchmark* Benchmark::Arg(int64_t x) { CHECK(ArgsCnt() == -1 || ArgsCnt() == 1); args_.push_back({x}); diff --git a/test/reporter_output_test.cc b/test/reporter_output_test.cc index d24a57dd..b3d8edba 100644 --- a/test/reporter_output_test.cc +++ b/test/reporter_output_test.cc @@ -334,6 +334,30 @@ ADD_CASES(TC_JSONOut, {"\"threads\": 1,$", MR_Next}}); ADD_CASES(TC_CSVOut, {{"^\"BM_arg_names/first:2/5/third:4\",%csv_report$"}}); +// ========================================================================= // +// ------------------------ Testing Name Output ---------------------------- // +// ========================================================================= // + +void BM_name(benchmark::State& state) { + for (auto _ : state) { + } +} +BENCHMARK(BM_name)->Name("BM_custom_name"); + +ADD_CASES(TC_ConsoleOut, {{"^BM_custom_name %console_report$"}}); +ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_custom_name\",$"}, + {"\"run_name\": \"BM_custom_name\",$", MR_Next}, + {"\"run_type\": \"iteration\",$", MR_Next}, + {"\"repetitions\": 0,$", MR_Next}, + {"\"repetition_index\": 0,$", MR_Next}, + {"\"threads\": 1,$", MR_Next}, + {"\"iterations\": %int,$", MR_Next}, + {"\"real_time\": %float,$", MR_Next}, + {"\"cpu_time\": %float,$", MR_Next}, + {"\"time_unit\": \"ns\"$", MR_Next}, + {"}", MR_Next}}); +ADD_CASES(TC_CSVOut, {{"^\"BM_custom_name\",%csv_report$"}}); + // ========================================================================= // // ------------------------ Testing Big Args Output ------------------------ // // ========================================================================= //