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
This commit is contained in:
Tobias Schmidt 2021-03-30 15:43:03 +02:00 committed by GitHub
parent cc9abfc8f1
commit 5e387e7d33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 0 deletions

View File

@ -52,6 +52,7 @@ Sayan Bhattacharjee <aero.sayan@gmail.com>
Shuo Chen <chenshuo@chenshuo.com>
Steinar H. Gunderson <sgunderson@bigfoot.com>
Stripe, Inc.
Tobias Schmidt <tobias.schmidt@in.tum.de>
Yixuan Qiu <yixuanq@gmail.com>
Yusuke Suzuki <utatane.tea@gmail.com>
Zbigniew Skowron <zbychs@gmail.com>

View File

@ -75,6 +75,7 @@ Roman Lebedev <lebedev.ri@gmail.com>
Sayan Bhattacharjee <aero.sayan@gmail.com>
Shuo Chen <chenshuo@chenshuo.com>
Steven Wan <wan.yu@ibm.com>
Tobias Schmidt <tobias.schmidt@in.tum.de>
Tobias Ulvgård <tobias.ulvgard@dirac.se>
Tom Madams <tom.ej.madams@gmail.com> <tmadams@google.com>
Yixuan Qiu <yixuanq@gmail.com>

View File

@ -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; });
```
<a name="custom-benchmark-name" />
### 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`.
<a name="templated-benchmarks" />
### Templated Benchmarks

View File

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

View File

@ -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});

View File

@ -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 ------------------------ //
// ========================================================================= //