Skip to content

Commit cf778ec

Browse files
not-matthiasGuillaumeLagrange
authored andcommitted
feat: add support for setting a custom benchmark name
Signed-off-by: Guillaume Lagrange <[email protected]>
1 parent 810d6b4 commit cf778ec

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

examples/google_benchmark_cmake/main.cpp

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,68 @@ static void BM_memcpy(benchmark::State &state) {
5656
delete[] src;
5757
delete[] dst;
5858
}
59-
6059
BENCHMARK(BM_memcpy)->Range(8, 8 << 10);
6160

61+
static void BM_some_super_long_name_that_we_dont_want_to_use(
62+
benchmark::State &state) {
63+
for (auto _ : state) {
64+
benchmark::DoNotOptimize(42);
65+
benchmark::ClobberMemory();
66+
}
67+
}
68+
BENCHMARK(BM_some_super_long_name_that_we_dont_want_to_use)
69+
->Name("BM_short_name");
70+
71+
template <class... Args>
72+
void BM_Capture_custom_name(benchmark::State &state, Args &&...args) {
73+
auto args_tuple = std::make_tuple(std::move(args)...);
74+
for (auto _ : state) {
75+
benchmark::DoNotOptimize(args_tuple);
76+
benchmark::ClobberMemory();
77+
}
78+
}
79+
BENCHMARK_CAPTURE(BM_Capture_custom_name, int_string_test, 42,
80+
std::string("abc::def"))
81+
->Name("BM_Capture_int_string");
82+
BENCHMARK_CAPTURE(BM_Capture_custom_name, int_test, 42, 43)
83+
->Name("BM_Capture_int");
84+
85+
namespace one {
86+
namespace two {
87+
namespace three {
88+
static void BM_nested_namespaces_with_custom_name(benchmark::State &state) {
89+
for (auto _ : state) {
90+
benchmark::DoNotOptimize(42);
91+
benchmark::ClobberMemory();
92+
}
93+
}
94+
BENCHMARK(BM_nested_namespaces_with_custom_name)
95+
->Name("BM_custom_name_in_namespace");
96+
} // namespace three
97+
} // namespace two
98+
} // namespace one
99+
100+
// Test with actual numeric parameters
101+
static void BM_with_args(benchmark::State &state) {
102+
for (auto _ : state) {
103+
benchmark::DoNotOptimize(state.range(0));
104+
benchmark::ClobberMemory();
105+
}
106+
}
107+
BENCHMARK(BM_with_args)->Arg(100)->Arg(1000);
108+
BENCHMARK(BM_with_args)->Name("BM_custom_args")->Arg(100)->Arg(1000);
109+
110+
// Test with multiple arguments
111+
static void BM_with_multiple_args(benchmark::State &state) {
112+
for (auto _ : state) {
113+
benchmark::DoNotOptimize(state.range(0) + state.range(1));
114+
benchmark::ClobberMemory();
115+
}
116+
}
117+
BENCHMARK(BM_with_multiple_args)->Args({10, 20})->Args({100, 200});
118+
BENCHMARK(BM_with_multiple_args)
119+
->Name("BM_custom_multi")
120+
->Args({10, 20})
121+
->Args({100, 200});
122+
62123
BENCHMARK_MAIN();

google_benchmark/src/benchmark_register.cc

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,32 @@ Benchmark* Benchmark::ThreadPerCpu() {
474474
return this;
475475
}
476476

477-
void Benchmark::SetName(const std::string& name) { name_ = name; }
477+
void Benchmark::SetName(const std::string& name) {
478+
#ifdef CODSPEED_ENABLED
479+
// Ensure that we sanitize the URI to not run into issues when searching for `::`.
480+
codspeed::sanitize_bench_args(name_);
481+
482+
// Preserve file path and namespace prefix if present
483+
size_t separator_pos = name_.rfind("::");
484+
if (separator_pos != std::string::npos) {
485+
std::string file_prefix = name_.substr(0, separator_pos + 2);
486+
487+
// Extract any parameters from the old name (format: "file::namespace::name[param1/param2/...]")
488+
size_t bracket_pos = name_.find('[', separator_pos);
489+
std::string params;
490+
if (bracket_pos != std::string::npos) {
491+
params = name_.substr(bracket_pos); // Include everything from '[' onwards
492+
}
493+
494+
// Reconstruct: file_prefix + new_name + params
495+
name_ = file_prefix + name + params;
496+
} else {
497+
name_ = name;
498+
}
499+
#else
500+
name_ = name;
501+
#endif
502+
}
478503

479504
const char* Benchmark::GetName() const { return name_.c_str(); }
480505

0 commit comments

Comments
 (0)