internal_threading_test.cc
Go to the documentation of this file.
1 
2 #undef NDEBUG
3 
4 #include <chrono>
5 #include <thread>
6 #include "../src/timers.h"
7 #include "benchmark/benchmark.h"
8 #include "output_test.h"
9 
10 static const std::chrono::duration<double, std::milli> time_frame(50);
11 static const double time_frame_in_sec(
12  std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1, 1>>>(
13  time_frame)
14  .count());
15 
17  const auto start = benchmark::ChronoClockNow();
18 
19  while (true) {
20  const auto now = benchmark::ChronoClockNow();
21  const auto elapsed = now - start;
22 
23  if (std::chrono::duration<double, std::chrono::seconds::period>(elapsed) >=
24  time_frame)
25  return;
26  }
27 }
28 
29 // ========================================================================= //
30 // --------------------------- TEST CASES BEGIN ---------------------------- //
31 // ========================================================================= //
32 
33 // ========================================================================= //
34 // BM_MainThread
35 
37  for (auto _ : state) {
39  state.SetIterationTime(time_frame_in_sec);
40  }
41  state.counters["invtime"] =
43 }
44 
45 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1);
46 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->UseRealTime();
47 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->UseManualTime();
48 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->MeasureProcessCPUTime();
50  ->Iterations(1)
51  ->Threads(1)
52  ->MeasureProcessCPUTime()
53  ->UseRealTime();
55  ->Iterations(1)
56  ->Threads(1)
57  ->MeasureProcessCPUTime()
58  ->UseManualTime();
59 
60 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2);
61 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->UseRealTime();
62 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->UseManualTime();
63 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->MeasureProcessCPUTime();
65  ->Iterations(1)
66  ->Threads(2)
67  ->MeasureProcessCPUTime()
68  ->UseRealTime();
70  ->Iterations(1)
71  ->Threads(2)
72  ->MeasureProcessCPUTime()
73  ->UseManualTime();
74 
75 // ========================================================================= //
76 // BM_WorkerThread
77 
79  for (auto _ : state) {
80  std::thread Worker(&MyBusySpinwait);
81  Worker.join();
82  state.SetIterationTime(time_frame_in_sec);
83  }
84  state.counters["invtime"] =
86 }
87 
88 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1);
89 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->UseRealTime();
90 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->UseManualTime();
91 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->MeasureProcessCPUTime();
93  ->Iterations(1)
94  ->Threads(1)
95  ->MeasureProcessCPUTime()
96  ->UseRealTime();
98  ->Iterations(1)
99  ->Threads(1)
100  ->MeasureProcessCPUTime()
101  ->UseManualTime();
102 
103 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2);
104 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->UseRealTime();
105 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->UseManualTime();
106 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->MeasureProcessCPUTime();
108  ->Iterations(1)
109  ->Threads(2)
110  ->MeasureProcessCPUTime()
111  ->UseRealTime();
113  ->Iterations(1)
114  ->Threads(2)
115  ->MeasureProcessCPUTime()
116  ->UseManualTime();
117 
118 // ========================================================================= //
119 // BM_MainThreadAndWorkerThread
120 
122  for (auto _ : state) {
123  std::thread Worker(&MyBusySpinwait);
124  MyBusySpinwait();
125  Worker.join();
126  state.SetIterationTime(time_frame_in_sec);
127  }
128  state.counters["invtime"] =
130 }
131 
132 BENCHMARK(BM_MainThreadAndWorkerThread)->Iterations(1)->Threads(1);
134  ->Iterations(1)
135  ->Threads(1)
136  ->UseRealTime();
138  ->Iterations(1)
139  ->Threads(1)
140  ->UseManualTime();
142  ->Iterations(1)
143  ->Threads(1)
144  ->MeasureProcessCPUTime();
146  ->Iterations(1)
147  ->Threads(1)
148  ->MeasureProcessCPUTime()
149  ->UseRealTime();
151  ->Iterations(1)
152  ->Threads(1)
153  ->MeasureProcessCPUTime()
154  ->UseManualTime();
155 
156 BENCHMARK(BM_MainThreadAndWorkerThread)->Iterations(1)->Threads(2);
158  ->Iterations(1)
159  ->Threads(2)
160  ->UseRealTime();
162  ->Iterations(1)
163  ->Threads(2)
164  ->UseManualTime();
166  ->Iterations(1)
167  ->Threads(2)
168  ->MeasureProcessCPUTime();
170  ->Iterations(1)
171  ->Threads(2)
172  ->MeasureProcessCPUTime()
173  ->UseRealTime();
175  ->Iterations(1)
176  ->Threads(2)
177  ->MeasureProcessCPUTime()
178  ->UseManualTime();
179 
180 // ========================================================================= //
181 // ---------------------------- TEST CASES END ----------------------------- //
182 // ========================================================================= //
183 
184 int main(int argc, char* argv[]) { RunOutputTests(argc, argv); }
MyBusySpinwait
void MyBusySpinwait()
Definition: internal_threading_test.cc:16
now
static double now(void)
Definition: test/core/fling/client.cc:130
benchmark::Counter::kIsRate
@ kIsRate
Definition: benchmark/include/benchmark/benchmark.h:388
time_frame
static const std::chrono::duration< double, std::milli > time_frame(50)
BENCHMARK
BENCHMARK(BM_MainThread) -> Iterations(1) ->Threads(1)
output_test.h
start
static uint64_t start
Definition: benchmark-pound.c:74
BM_MainThread
void BM_MainThread(benchmark::State &state)
Definition: internal_threading_test.cc:36
benchmark::ChronoClockNow
double ChronoClockNow()
Definition: third_party/benchmark/src/timers.h:38
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
benchmark::Counter
Definition: benchmark/include/benchmark/benchmark.h:382
main
int main(int argc, char *argv[])
Definition: internal_threading_test.cc:184
RunOutputTests
void RunOutputTests(int argc, char *argv[])
Definition: benchmark/test/output_test_helper.cc:391
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
BM_MainThreadAndWorkerThread
void BM_MainThreadAndWorkerThread(benchmark::State &state)
Definition: internal_threading_test.cc:121
BM_WorkerThread
void BM_WorkerThread(benchmark::State &state)
Definition: internal_threading_test.cc:78
time_frame_in_sec
static const double time_frame_in_sec(std::chrono::duration_cast< std::chrono::duration< double, std::ratio< 1, 1 >>>(time_frame) .count())
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:21