Skip to content

Commit 0591e59

Browse files
committed
Add increment benchmark as baseline
1 parent bf66633 commit 0591e59

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

core/tests/benchmark/counter_bench.cc

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
#include <benchmark/benchmark.h>
22
#include <prometheus/registry.h>
33

4+
static void BM_Counter_IncrementBaseline(benchmark::State& state) {
5+
struct {
6+
void Increment() { v += 1.0; }
7+
double v;
8+
} counter;
9+
10+
for (auto _ : state) {
11+
counter.Increment();
12+
}
13+
benchmark::DoNotOptimize(counter.v);
14+
}
15+
BENCHMARK(BM_Counter_IncrementBaseline);
16+
417
static void BM_Counter_Increment(benchmark::State& state) {
518
using prometheus::BuildCounter;
619
using prometheus::Counter;
@@ -10,9 +23,10 @@ static void BM_Counter_Increment(benchmark::State& state) {
1023
BuildCounter().Name("benchmark_counter").Help("").Register(registry);
1124
auto& counter = counter_family.Add({});
1225

13-
while (state.KeepRunning()) {
26+
for (auto _ : state) {
1427
counter.Increment();
1528
}
29+
benchmark::DoNotOptimize(counter.Value());
1630
}
1731
BENCHMARK(BM_Counter_Increment);
1832

@@ -28,6 +42,7 @@ BENCHMARK_F(BM_Counter, ConcurrentIncrement)
2842
for (auto _ : state) {
2943
counter.Increment();
3044
}
45+
benchmark::DoNotOptimize(counter.Value());
3146
}
3247

3348
static void BM_Counter_Collect(benchmark::State& state) {

0 commit comments

Comments
 (0)