Skip to content

Commit be4f1b0

Browse files
committed
feat: add bench with PauseTiming
1 parent 5c0abf0 commit be4f1b0

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../google_benchmark_cmake/pause_timing_bench.hpp

examples/google_benchmark_cmake/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "fibonacci_bench.hpp"
66
#include "fixture_bench.hpp"
77
#include "multithread_bench.hpp"
8+
#include "pause_timing_bench.hpp"
89
#include "sleep_bench.hpp"
910
#include "template_bench.hpp"
1011

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#pragma once
2+
3+
#include <benchmark/benchmark.h>
4+
5+
#include <chrono>
6+
#include <numeric>
7+
#include <thread>
8+
#include <vector>
9+
10+
__attribute__((noinline)) static uint64_t recursive_fib(int n) {
11+
if (n <= 1) return n;
12+
return recursive_fib(n - 1) + recursive_fib(n - 2);
13+
}
14+
15+
__attribute__((noinline)) static uint64_t expensive_operation() {
16+
// Large memory allocation
17+
std::vector<uint64_t> data(1024 * 1024, 42); // 8 MiB allocation
18+
19+
// Expensive recursive computation that will dominate flamegraph
20+
uint64_t fib_result = recursive_fib(30);
21+
22+
// More expensive work
23+
uint64_t sum = std::accumulate(data.begin(), data.end(), uint64_t(0));
24+
benchmark::DoNotOptimize(sum);
25+
benchmark::DoNotOptimize(fib_result);
26+
27+
return sum + fib_result;
28+
}
29+
30+
NOINLINE static uint64_t actual_work() {
31+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
32+
return 42;
33+
}
34+
35+
static void BM_large_drop(benchmark::State& state) {
36+
for (auto _ : state) {
37+
state.PauseTiming();
38+
benchmark::DoNotOptimize(expensive_operation());
39+
state.ResumeTiming();
40+
41+
benchmark::DoNotOptimize(actual_work());
42+
43+
state.PauseTiming();
44+
benchmark::DoNotOptimize(expensive_operation());
45+
state.ResumeTiming();
46+
}
47+
}
48+
BENCHMARK(BM_large_drop);
49+
50+
static void BM_large_setup(benchmark::State& state) {
51+
for (auto _ : state) {
52+
state.PauseTiming();
53+
benchmark::DoNotOptimize(expensive_operation());
54+
state.ResumeTiming();
55+
56+
benchmark::DoNotOptimize(actual_work());
57+
}
58+
}
59+
BENCHMARK(BM_large_setup);

0 commit comments

Comments
 (0)