File tree Expand file tree Collapse file tree 5 files changed +80
-0
lines changed Expand file tree Collapse file tree 5 files changed +80
-0
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
+ #ifdef _MSC_VER
5
+ #define NOINLINE __declspec (noinline)
6
+ #else
7
+ #define NOINLINE __attribute__ ((noinline))
8
+ #endif
9
+
10
+ #include < benchmark/benchmark.h>
11
+
12
+ #include < cstdint>
13
+ #include < numeric>
14
+ #include < vector>
15
+
16
+ NOINLINE static uint64_t recursive_fib (int n) {
17
+ if (n <= 1 ) return n;
18
+ return recursive_fib (n - 1 ) + recursive_fib (n - 2 );
19
+ }
20
+
21
+ NOINLINE static uint64_t expensive_operation () {
22
+ // Large memory allocation
23
+ std::vector<uint64_t > data (1024 * 1024 , 42 ); // 8 MiB allocation
24
+
25
+ // Expensive recursive computation that will dominate flamegraph
26
+ uint64_t fib_result = recursive_fib (30 );
27
+
28
+ // More expensive work
29
+ uint64_t sum = std::accumulate (data.begin (), data.end (), uint64_t (0 ));
30
+ benchmark::DoNotOptimize (sum);
31
+ benchmark::DoNotOptimize (fib_result);
32
+
33
+ return sum + fib_result;
34
+ }
35
+
36
+ #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 " sleep_bench.hpp"
8
9
#include " template_bench.hpp"
9
10
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
+ for (auto _ : state) {
13
+ state.PauseTiming ();
14
+ std::atomic<bool > work_start{false };
15
+ std::atomic<bool > work_done{false };
16
+
17
+ std::thread worker ([&]() {
18
+ // Wait for the signal to start
19
+ while (!work_start.load ()) {
20
+ std::this_thread::yield ();
21
+ }
22
+
23
+ // Do some CPU work
24
+ benchmark::DoNotOptimize (expensive_operation ());
25
+ work_done.store (true );
26
+ });
27
+ state.ResumeTiming ();
28
+
29
+ // Signal the worker to start
30
+ work_start.store (true );
31
+
32
+ // Wait for worker to complete
33
+ while (!work_done.load ()) {
34
+ std::this_thread::yield ();
35
+ }
36
+
37
+ worker.join ();
38
+ }
39
+ }
40
+
41
+ BENCHMARK (BM_SimpleMultithread);
You can’t perform that action at this time.
0 commit comments