Skip to content

Commit 660b1f9

Browse files
committed
fix: exclude warmup measurements
1 parent 56e942a commit 660b1f9

File tree

6 files changed

+28
-25
lines changed

6 files changed

+28
-25
lines changed

google_benchmark/include/benchmark/benchmark.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,7 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State {
956956
private:
957957
bool started_;
958958
bool finished_;
959+
bool is_warmup_;
959960
internal::Skipped skipped_;
960961

961962
// items we don't need on the first cache line
@@ -977,6 +978,8 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State {
977978
,
978979
codspeed::CodSpeed* codspeed = NULL
979980
#endif
981+
,
982+
bool is_warmup = false
980983
);
981984

982985
void StartKeepRunning();

google_benchmark/src/benchmark.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ State::State(std::string name, IterationCount max_iters,
190190
,
191191
codspeed::CodSpeed* codspeed
192192
#endif
193+
,
194+
bool is_warmup
193195
)
194196
: total_iterations_(0),
195197
batch_leftover_(0),
@@ -202,6 +204,7 @@ State::State(std::string name, IterationCount max_iters,
202204
#endif
203205
started_(false),
204206
finished_(false),
207+
is_warmup_(is_warmup),
205208
skipped_(internal::NotSkipped),
206209
range_(ranges),
207210
complexity_n_(0),
@@ -275,7 +278,7 @@ void State::PauseTiming() {
275278
timer_->StopTimer();
276279

277280
#ifdef CODSPEED_WALLTIME
278-
if (resume_timestamp_ != 0) {
281+
if (!is_warmup_ && resume_timestamp_ != 0) {
279282
measurement_add_benchmark_timestamps(resume_timestamp_, pause_timestamp);
280283
resume_timestamp_ = 0;
281284
}
@@ -566,8 +569,9 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
566569

567570
#ifdef CODSPEED_WALLTIME
568571
auto* codspeed = codspeed::CodSpeed::getInstance();
569-
if (codspeed != nullptr) {
572+
if (codspeed != nullptr && runner.IsFirstRepetition()) {
570573
codspeed->start_benchmark(runner.GetBenchmarkName());
574+
measurement_start();
571575
}
572576
#endif
573577

@@ -601,6 +605,7 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
601605

602606
#ifdef CODSPEED_WALLTIME
603607
if (codspeed != nullptr) {
608+
measurement_stop();
604609
codspeed->end_benchmark();
605610
}
606611

google_benchmark/src/benchmark_api_internal.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ State BenchmarkInstance::__codspeed_root_frame__Run(
118118
IterationCount iters, int thread_id, internal::ThreadTimer* timer,
119119
internal::ThreadManager* manager,
120120
internal::PerfCountersMeasurement* perf_counters_measurement,
121-
ProfilerManager* profiler_manager) const {
122-
#ifdef CODSPEED_WALLTIME
121+
ProfilerManager* profiler_manager, bool is_warmup) const {
122+
#if defined(CODSPEED_SIMULATION) || defined(CODSPEED_WALLTIME)
123123
State st(name_.function_name, iters, args_, thread_id, threads_, timer,
124-
manager, perf_counters_measurement, profiler_manager, codspeed::CodSpeed::getInstance());
124+
manager, perf_counters_measurement, profiler_manager, codspeed::CodSpeed::getInstance(), is_warmup);
125125
#else
126126
State st(name_.function_name, iters, args_, thread_id, threads_, timer,
127-
manager, perf_counters_measurement, profiler_manager);
127+
manager, perf_counters_measurement, profiler_manager, is_warmup);
128128
#endif
129129
benchmark_.Run(st);
130130
return st;

google_benchmark/src/benchmark_api_internal.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ class BenchmarkInstance {
5353
void Teardown() const;
5454

5555
State __codspeed_root_frame__Run(IterationCount iters, int thread_id, internal::ThreadTimer* timer,
56-
internal::ThreadManager* manager,
57-
internal::PerfCountersMeasurement* perf_counters_measurement,
58-
ProfilerManager* profiler_manager) const;
56+
internal::ThreadManager* manager,
57+
internal::PerfCountersMeasurement* perf_counters_measurement,
58+
ProfilerManager* profiler_manager, bool is_warmup = false) const;
5959

6060
#ifdef CODSPEED_SIMULATION
6161
State RunSimulation(

google_benchmark/src/benchmark_runner.cc

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ BenchmarkReporter::Run CreateRunReport(
135135
void RunInThread(const BenchmarkInstance* b, IterationCount iters,
136136
int thread_id, ThreadManager* manager,
137137
PerfCountersMeasurement* perf_counters_measurement,
138-
ProfilerManager* profiler_manager_) {
138+
ProfilerManager* profiler_manager_, bool is_warmup = false) {
139139
internal::ThreadTimer timer(
140140
b->measure_process_cpu_time()
141141
? internal::ThreadTimer::CreateProcessCpuTime()
142142
: internal::ThreadTimer::Create());
143143

144144
State st = b->__codspeed_root_frame__Run(iters, thread_id, &timer, manager,
145-
perf_counters_measurement, profiler_manager_);
145+
perf_counters_measurement, profiler_manager_, is_warmup);
146146
BM_CHECK(st.skipped() || st.iterations() >= st.max_iterations)
147147
<< "Benchmark returned before State::KeepRunning() returned false!";
148148
{
@@ -285,7 +285,7 @@ BenchmarkRunner::BenchmarkRunner(
285285
}
286286
}
287287

288-
BenchmarkRunner::IterationResults BenchmarkRunner::DoNIterations() {
288+
BenchmarkRunner::IterationResults BenchmarkRunner::DoNIterations(bool is_warmup) {
289289
BM_VLOG(2) << "Running " << b.name().str() << " for " << iters << "\n";
290290

291291
std::unique_ptr<internal::ThreadManager> manager;
@@ -295,13 +295,13 @@ BenchmarkRunner::IterationResults BenchmarkRunner::DoNIterations() {
295295
for (std::size_t ti = 0; ti < pool.size(); ++ti) {
296296
pool[ti] = std::thread(&RunInThread, &b, iters, static_cast<int>(ti + 1),
297297
manager.get(), perf_counters_measurement_ptr,
298-
/*profiler_manager=*/nullptr);
298+
/*profiler_manager=*/nullptr, is_warmup);
299299
}
300300
// And run one thread here directly.
301301
// (If we were asked to run just one thread, we don't create new threads.)
302302
// Yes, we need to do this here *after* we start the separate threads.
303303
RunInThread(&b, iters, 0, manager.get(), perf_counters_measurement_ptr,
304-
/*profiler_manager=*/nullptr);
304+
/*profiler_manager=*/nullptr, is_warmup);
305305

306306
// The main thread has finished. Now let's wait for the other threads.
307307
manager->WaitForAllThreads();
@@ -405,7 +405,7 @@ void BenchmarkRunner::RunWarmUp() {
405405

406406
for (;;) {
407407
b.Setup();
408-
i_warmup = DoNIterations();
408+
i_warmup = DoNIterations(/*is_warmup=*/true);
409409
b.Teardown();
410410

411411
const bool finish = ShouldReportIterationResults(i_warmup);
@@ -486,12 +486,6 @@ void BenchmarkRunner::DoOneRepetition() {
486486
RunWarmUp();
487487
}
488488

489-
// IMPORTANT: We must not sample the warmup otherwise the flamegraph timings will be incorrect since we
490-
// divide by the iteration count.
491-
#ifdef CODSPEED_WALLTIME
492-
measurement_start();
493-
#endif
494-
495489
IterationResults i;
496490
// We *may* be gradually increasing the length (iteration count)
497491
// of the benchmark until we decide the results are significant.
@@ -525,9 +519,6 @@ void BenchmarkRunner::DoOneRepetition() {
525519
"if we did more iterations than we want to do the next time, "
526520
"then we should have accepted the current iteration run.");
527521
}
528-
#ifdef CODSPEED_WALLTIME
529-
measurement_stop();
530-
#endif
531522

532523
// Produce memory measurements if requested.
533524
MemoryManager::Result* memory_result = nullptr;

google_benchmark/src/benchmark_runner.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class BenchmarkRunner {
6060
return GetNumRepeats() != num_repetitions_done;
6161
}
6262

63+
bool IsFirstRepetition() const {
64+
return num_repetitions_done == 0;
65+
}
66+
6367
void DoOneRepetition();
6468

6569
RunResults&& GetResults();
@@ -104,7 +108,7 @@ class BenchmarkRunner {
104108
IterationCount iters;
105109
double seconds;
106110
};
107-
IterationResults DoNIterations();
111+
IterationResults DoNIterations(bool is_warmup = false);
108112

109113
MemoryManager::Result* RunMemoryManager(IterationCount memory_iterations);
110114

0 commit comments

Comments
 (0)