Skip to content

Commit 6c8a1aa

Browse files
committed
feat: add walltime support to google benchmark
1 parent 74f6096 commit 6c8a1aa

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

google_benchmark/include/benchmark/benchmark.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State {
946946

947947
public:
948948
const IterationCount max_iterations;
949-
#ifdef CODSPEED_INSTRUMENTATION
949+
#if defined(CODSPEED_INSTRUMENTATION) || defined(CODSPEED_WALLTIME)
950950
codspeed::CodSpeed* codspeed_;
951951
#endif
952952

@@ -970,7 +970,7 @@ class BENCHMARK_EXPORT BENCHMARK_INTERNAL_CACHELINE_ALIGNED State {
970970
internal::ThreadTimer* timer, internal::ThreadManager* manager,
971971
internal::PerfCountersMeasurement* perf_counters_measurement,
972972
ProfilerManager* profiler_manager
973-
#ifdef CODSPEED_INSTRUMENTATION
973+
#if defined(CODSPEED_INSTRUMENTATION) || defined(CODSPEED_WALLTIME)
974974
,
975975
codspeed::CodSpeed* codspeed = NULL
976976
#endif
@@ -1064,12 +1064,9 @@ struct State::StateIterator {
10641064
BENCHMARK_ALWAYS_INLINE
10651065
bool operator!=(StateIterator const&) const {
10661066
if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0, true)) return true;
1067-
#ifdef CODSPEED_INSTRUMENTATION
1068-
measurement_stop();
10691067

1070-
if (parent_->codspeed_ != NULL) {
1071-
parent_->codspeed_->end_benchmark();
1072-
}
1068+
#if defined(CODSPEED_INSTRUMENTATION) || defined(CODSPEED_WALLTIME)
1069+
measurement_stop();
10731070
#endif
10741071
parent_->FinishKeepRunning();
10751072
return false;
@@ -1085,13 +1082,11 @@ inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::begin() {
10851082
}
10861083
inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::end() {
10871084
StartKeepRunning();
1088-
#ifdef CODSPEED_INSTRUMENTATION
1089-
if (this->codspeed_ != NULL) {
1090-
this->codspeed_->start_benchmark(name_);
1091-
}
10921085

1086+
#if defined(CODSPEED_INSTRUMENTATION) || defined(CODSPEED_WALLTIME)
10931087
measurement_start();
10941088
#endif
1089+
10951090
return StateIterator();
10961091
}
10971092

google_benchmark/src/benchmark.cc

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,15 @@ State::State(std::string name, IterationCount max_iters,
175175
internal::ThreadTimer* timer, internal::ThreadManager* manager,
176176
internal::PerfCountersMeasurement* perf_counters_measurement,
177177
ProfilerManager* profiler_manager
178-
#ifdef CODSPEED_INSTRUMENTATION
178+
#if defined(CODSPEED_INSTRUMENTATION) || defined(CODSPEED_WALLTIME)
179179
,
180180
codspeed::CodSpeed* codspeed
181181
#endif
182182
)
183183
: total_iterations_(0),
184184
batch_leftover_(0),
185185
max_iterations(max_iters),
186-
#ifdef CODSPEED_INSTRUMENTATION
186+
#if defined(CODSPEED_INSTRUMENTATION) || defined(CODSPEED_WALLTIME)
187187
codspeed_(codspeed),
188188
#endif
189189
started_(false),
@@ -542,6 +542,14 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
542542
#endif
543543
for (size_t repetition_index : repetition_indices) {
544544
internal::BenchmarkRunner& runner = runners[repetition_index];
545+
546+
#ifdef CODSPEED_WALLTIME
547+
auto codspeed = codspeed::CodSpeed::getInstance();
548+
if (codspeed != nullptr) {
549+
codspeed->start_benchmark(runner.GetBenchmarkName());
550+
}
551+
#endif
552+
545553
runner.DoOneRepetition();
546554
if (runner.HasRepeatsRemaining()) {
547555
continue;
@@ -571,6 +579,10 @@ void RunBenchmarks(const std::vector<BenchmarkInstance>& benchmarks,
571579
}
572580

573581
#ifdef CODSPEED_WALLTIME
582+
if (codspeed != nullptr) {
583+
codspeed->end_benchmark();
584+
}
585+
574586
codspeed_walltime_data.push_back(generate_raw_walltime_data(run_results));
575587
#endif
576588

google_benchmark/src/benchmark_api_internal.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,14 @@ State BenchmarkInstance::Run(
119119
internal::ThreadManager* manager,
120120
internal::PerfCountersMeasurement* perf_counters_measurement,
121121
ProfilerManager* profiler_manager) const {
122+
#ifdef CODSPEED_WALLTIME
123+
State st(name_.function_name, iters, args_, thread_id, threads_, timer,
124+
manager, perf_counters_measurement, profiler_manager, codspeed::CodSpeed::getInstance());
125+
#else
122126
State st(name_.function_name, iters, args_, thread_id, threads_, timer,
123127
manager, perf_counters_measurement, profiler_manager);
128+
#endif
129+
124130
benchmark_.Run(st);
125131
return st;
126132
}

google_benchmark/src/benchmark_runner.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ class BenchmarkRunner {
119119
void FinishWarmUp(const IterationCount& i);
120120

121121
void RunWarmUp();
122+
123+
#if defined(CODSPEED_INSTRUMENTATION) || defined(CODSPEED_WALLTIME)
124+
public:
125+
inline std::string GetBenchmarkName() {
126+
return b.name().str();
127+
}
128+
#endif
122129
};
123130

124131
} // namespace internal

0 commit comments

Comments
 (0)