Skip to content

Commit 1aeba2b

Browse files
fixup! feat(google_benchmark): only run benchmark once in instrumentation mode
1 parent 651190d commit 1aeba2b

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

google_benchmark/cmake/Codspeed.cmake

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,21 @@ target_compile_definitions(codspeed
1010

1111
# Step 1: Check if CODSPEED_MODE is set via the command line
1212

13-
# CMake cache kind of breaks this mechanism, keeping it for first time
14-
# defaulting
15-
if(NOT DEFINED CODSPEED_MODE)
16-
# Step 2: Check the environment variable CODSPEED_MODE
17-
if(DEFINED $ENV{CODSPEED_RUNNER_MODE})
18-
set(CODSPEED_MODE $ENV{CODSPEED_RUNNER_MODE} FORCE)
13+
if(DEFINED CODSPEED_MODE)
14+
# Define a preprocessor macro based on the build mode
15+
if(CODSPEED_MODE STREQUAL "instrumentation")
16+
target_compile_definitions(
17+
codspeed
18+
INTERFACE -DCODSPEED_INSTRUMENTATION
19+
)
20+
elseif(CODSPEED_MODE STREQUAL "walltime")
21+
target_compile_definitions(codspeed INTERFACE -DCODSPEED_WALLTIME)
1922
else()
20-
# Step 3: Default to "instrumentation" if no value is provided
21-
set(CODSPEED_MODE "instrumentation")
23+
message(
24+
FATAL_ERROR
25+
"Invalid build mode: ${CODSPEED_MODE}. Use 'instrumentation' or 'walltime'."
26+
)
2227
endif()
2328
endif()
2429

25-
# Define a preprocessor macro based on the build mode
26-
if(CODSPEED_MODE STREQUAL "instrumentation")
27-
target_compile_definitions(codspeed INTERFACE -DCODSPEED_INSTRUMENTATION)
28-
elseif(CODSPEED_MODE STREQUAL "walltime")
29-
target_compile_definitions(codspeed INTERFACE -DCODSPEED_WALLTIME)
30-
else()
31-
message(
32-
FATAL_ERROR
33-
"Invalid build mode: ${CODSPEED_MODE}. Use 'instrumentation' or 'walltime'."
34-
)
35-
endif()
36-
3730
message(STATUS "Build mode set to: ${CODSPEED_MODE}")

google_benchmark/src/benchmark.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,9 +384,9 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
384384

385385
// Determine the width of the name field using a minimum width of 10.
386386
#ifdef CODSPEED_INSTRUMENTATION
387-
std::cout << "Codspeed mode: instrumentation" << "\n";
387+
Err << "Codspeed mode: instrumentation" << "\n";
388388
#elif defined(CODSPEED_WALLTIME)
389-
std::cout << "Codspeed mode walltime" << "\n";
389+
Err << "Codspeed mode: walltime" << "\n";
390390
#endif
391391

392392
bool might_have_aggregates = FLAGS_benchmark_repetitions > 1;

google_benchmark/src/benchmark_api_internal.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ State BenchmarkInstance::RunInstrumented(
9898
ProfilerManager* profiler_manager) const {
9999
State st(name_.function_name, 1, args_, 0, 1, timer, manager,
100100
perf_counters_measurement, profiler_manager);
101+
// Do one repetition to avoid flakiness due to inconcistencies in CPU cache
102+
// from execution order
103+
benchmark_.Run(st);
101104
codspeed->start_benchmark(name().str());
102105
benchmark_.Run(st);
103106
codspeed->end_benchmark();

google_benchmark/src/benchmark_runner.cc

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,20 +461,22 @@ void BenchmarkRunner::RunProfilerManager(IterationCount profile_iterations) {
461461
}
462462

463463
void BenchmarkRunner::DoOneRepetition() {
464-
assert(HasRepeatsRemaining() && "Already done all repetitions?");
465-
466-
const bool is_the_first_repetition = num_repetitions_done == 0;
467-
468464
#ifdef CODSPEED_INSTRUMENTATION
469465
std::unique_ptr<internal::ThreadManager> manager;
470466
manager.reset(new internal::ThreadManager(b.threads()));
471467
internal::ThreadTimer timer = internal::ThreadTimer::Create();
468+
b.Setup();
472469
State st = b.RunInstrumented(CodSpeed::getInstance(), &timer, manager.get(),
473470
nullptr, nullptr);
471+
b.Teardown();
474472

475473
return;
476474
#endif
477475

476+
assert(HasRepeatsRemaining() && "Already done all repetitions?");
477+
478+
const bool is_the_first_repetition = num_repetitions_done == 0;
479+
478480
// In case a warmup phase is requested by the benchmark, run it now.
479481
// After running the warmup phase the BenchmarkRunner should be in a state as
480482
// this warmup never happened except the fact that warmup_done is set. Every

0 commit comments

Comments
 (0)