File tree Expand file tree Collapse file tree 6 files changed +74
-19
lines changed Expand file tree Collapse file tree 6 files changed +74
-19
lines changed Original file line number Diff line number Diff line change
1
+ ../google_benchmark_cmake/helper.hpp
Original file line number Diff line number Diff line change
1
+ ../google_benchmark_cmake/multithread_bench.hpp
Original file line number Diff line number Diff line change
1
+ #ifndef HELPER_HPP
2
+ #define HELPER_HPP
3
+
4
+ #include < benchmark/benchmark.h>
5
+
6
+ #include < cstdint>
7
+ #include < numeric>
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
+ #endif // HELPER_HPP
Original file line number Diff line number Diff line change 4
4
5
5
#include " fibonacci_bench.hpp"
6
6
#include " fixture_bench.hpp"
7
+ #include " multithread_bench.hpp"
7
8
#include " pause_timing_bench.hpp"
8
9
#include " sleep_bench.hpp"
9
10
#include " template_bench.hpp"
Original file line number Diff line number Diff line change
1
+ #pragma once
2
+
3
+ #include < benchmark/benchmark.h>
4
+
5
+ #include < atomic>
6
+ #include < thread>
7
+
8
+ #include " helper.hpp"
9
+
10
+ // Simple multithreaded benchmark: spawn thread, do work, join
11
+ static void BM_SimpleMultithread (benchmark::State& state) {
12
+ std::atomic<bool > work_start{false };
13
+ std::atomic<bool > work_done{false };
14
+
15
+ std::thread worker ([&]() {
16
+ // Wait for the signal to start
17
+ while (!work_start.load ()) {
18
+ std::this_thread::yield ();
19
+ }
20
+
21
+ // Do some CPU work
22
+ benchmark::DoNotOptimize (expensive_operation ());
23
+ work_done.store (true );
24
+ });
25
+
26
+ for (auto _ : state) {
27
+ // Signal the worker to start
28
+ work_start.store (true );
29
+
30
+ // Wait for worker to complete
31
+ while (!work_done.load ()) {
32
+ std::this_thread::yield ();
33
+ }
34
+
35
+ worker.join ();
36
+ }
37
+ }
38
+
39
+ // Register the benchmark
40
+ BENCHMARK (BM_SimpleMultithread)->Iterations(5 );
Original file line number Diff line number Diff line change 7
7
#include < thread>
8
8
#include < vector>
9
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
- }
10
+ #include " helper.hpp"
29
11
30
12
__attribute__ ((noinline)) static uint64_t actual_work() {
31
13
std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
You can’t perform that action at this time.
0 commit comments