Skip to content

Commit 3c5fc6b

Browse files
committed
feat: add support for setting a custom benchmark name
1 parent 810d6b4 commit 3c5fc6b

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-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"))
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: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,29 @@ 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+
// Preserve file path and namespace prefix if present
480+
size_t separator_pos = name_.rfind("::");
481+
if (separator_pos != std::string::npos) {
482+
std::string file_prefix = name_.substr(0, separator_pos + 2);
483+
484+
// Extract any parameters from the old name (format: "file::namespace::name[param1/param2/...]")
485+
size_t bracket_pos = name_.find('[', separator_pos);
486+
std::string params;
487+
if (bracket_pos != std::string::npos) {
488+
params = name_.substr(bracket_pos); // Include everything from '[' onwards
489+
}
490+
491+
// Reconstruct: file_prefix + new_name + params
492+
name_ = file_prefix + name + params;
493+
} else {
494+
name_ = name;
495+
}
496+
#else
497+
name_ = name;
498+
#endif
499+
}
478500

479501
const char* Benchmark::GetName() const { return name_.c_str(); }
480502

0 commit comments

Comments
 (0)