diff --git a/examples/google_benchmark_cmake/main.cpp b/examples/google_benchmark_cmake/main.cpp index 525cebd..0ebd56c 100644 --- a/examples/google_benchmark_cmake/main.cpp +++ b/examples/google_benchmark_cmake/main.cpp @@ -56,7 +56,68 @@ static void BM_memcpy(benchmark::State &state) { delete[] src; delete[] dst; } - BENCHMARK(BM_memcpy)->Range(8, 8 << 10); +static void BM_some_super_long_name_that_we_dont_want_to_use( + benchmark::State &state) { + for (auto _ : state) { + benchmark::DoNotOptimize(42); + benchmark::ClobberMemory(); + } +} +BENCHMARK(BM_some_super_long_name_that_we_dont_want_to_use) + ->Name("BM_short_name"); + +template +void BM_Capture_custom_name(benchmark::State &state, Args &&...args) { + auto args_tuple = std::make_tuple(std::move(args)...); + for (auto _ : state) { + benchmark::DoNotOptimize(args_tuple); + benchmark::ClobberMemory(); + } +} +BENCHMARK_CAPTURE(BM_Capture_custom_name, int_string_test, 42, + std::string("abc::def")) + ->Name("BM_Capture_int_string"); +BENCHMARK_CAPTURE(BM_Capture_custom_name, int_test, 42, 43) + ->Name("BM_Capture_int"); + +namespace one { +namespace two { +namespace three { +static void BM_nested_namespaces_with_custom_name(benchmark::State &state) { + for (auto _ : state) { + benchmark::DoNotOptimize(42); + benchmark::ClobberMemory(); + } +} +BENCHMARK(BM_nested_namespaces_with_custom_name) + ->Name("BM_custom_name_in_namespace"); +} // namespace three +} // namespace two +} // namespace one + +// Test with actual numeric parameters +static void BM_with_args(benchmark::State &state) { + for (auto _ : state) { + benchmark::DoNotOptimize(state.range(0)); + benchmark::ClobberMemory(); + } +} +BENCHMARK(BM_with_args)->Arg(100)->Arg(1000); +BENCHMARK(BM_with_args)->Name("BM_custom_args")->Arg(100)->Arg(1000); + +// Test with multiple arguments +static void BM_with_multiple_args(benchmark::State &state) { + for (auto _ : state) { + benchmark::DoNotOptimize(state.range(0) + state.range(1)); + benchmark::ClobberMemory(); + } +} +BENCHMARK(BM_with_multiple_args)->Args({10, 20})->Args({100, 200}); +BENCHMARK(BM_with_multiple_args) + ->Name("BM_custom_multi") + ->Args({10, 20}) + ->Args({100, 200}); + BENCHMARK_MAIN(); diff --git a/google_benchmark/src/benchmark_register.cc b/google_benchmark/src/benchmark_register.cc index 28336a1..9904b9d 100644 --- a/google_benchmark/src/benchmark_register.cc +++ b/google_benchmark/src/benchmark_register.cc @@ -474,7 +474,32 @@ Benchmark* Benchmark::ThreadPerCpu() { return this; } -void Benchmark::SetName(const std::string& name) { name_ = name; } +void Benchmark::SetName(const std::string& name) { +#ifdef CODSPEED_ENABLED + // Ensure that we sanitize the URI to not run into issues when searching for `::`. + codspeed::sanitize_bench_args(name_); + + // Preserve file path and namespace prefix if present + size_t separator_pos = name_.rfind("::"); + if (separator_pos != std::string::npos) { + std::string file_prefix = name_.substr(0, separator_pos + 2); + + // Extract any parameters from the old name (format: "file::namespace::name[param1/param2/...]") + size_t bracket_pos = name_.find('[', separator_pos); + std::string params; + if (bracket_pos != std::string::npos) { + params = name_.substr(bracket_pos); // Include everything from '[' onwards + } + + // Reconstruct: file_prefix + new_name + params + name_ = file_prefix + name + params; + } else { + name_ = name; + } +#else + name_ = name; +#endif +} const char* Benchmark::GetName() const { return name_.c_str(); }