Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion examples/google_benchmark_cmake/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class... Args>
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();
27 changes: 26 additions & 1 deletion google_benchmark/src/benchmark_register.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }

Expand Down
Loading