Skip to content
Open
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();
4 changes: 0 additions & 4 deletions google_benchmark/src/benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,6 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
if (codspeed != nullptr) {
codspeed->start_benchmark(runner.GetBenchmarkName());
}

measurement_start();
#endif

runner.DoOneRepetition();
Expand Down Expand Up @@ -605,8 +603,6 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
}

#ifdef CODSPEED_WALLTIME
measurement_stop();

if (codspeed != nullptr) {
codspeed->end_benchmark();
}
Expand Down
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
11 changes: 11 additions & 0 deletions google_benchmark/src/benchmark_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,10 @@ void BenchmarkRunner::DoOneRepetition() {
manager.reset(new internal::ThreadManager(b.threads()));
internal::ThreadTimer timer = internal::ThreadTimer::Create();
b.Setup();
measurement_start();
State st = b.RunInstrumented(codspeed::CodSpeed::getInstance(), &timer,
manager.get(), nullptr, nullptr);
measurement_stop();
b.Teardown();

return;
Expand All @@ -486,6 +488,12 @@ void BenchmarkRunner::DoOneRepetition() {
RunWarmUp();
}

// IMPORTANT: We must not sample the warmup otherwise the flamegraph timings will be incorrect since we
// divide by the iteration count.
#ifdef CODSPEED_WALLTIME
measurement_start();
#endif

IterationResults i;
// We *may* be gradually increasing the length (iteration count)
// of the benchmark until we decide the results are significant.
Expand Down Expand Up @@ -519,6 +527,9 @@ void BenchmarkRunner::DoOneRepetition() {
"if we did more iterations than we want to do the next time, "
"then we should have accepted the current iteration run.");
}
#ifdef CODSPEED_WALLTIME
measurement_stop();
#endif

// Produce memory measurements if requested.
MemoryManager::Result* memory_result = nullptr;
Expand Down