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 
MyBusySpinwait()16 void MyBusySpinwait() {
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 
BM_MainThread(benchmark::State & state)36 void BM_MainThread(benchmark::State& state) {
37   for (auto _ : state) {
38     MyBusySpinwait();
39     state.SetIterationTime(time_frame_in_sec);
40   }
41   state.counters["invtime"] =
42       benchmark::Counter{1, benchmark::Counter::kIsRate};
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();
49 BENCHMARK(BM_MainThread)
50     ->Iterations(1)
51     ->Threads(1)
52     ->MeasureProcessCPUTime()
53     ->UseRealTime();
54 BENCHMARK(BM_MainThread)
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();
64 BENCHMARK(BM_MainThread)
65     ->Iterations(1)
66     ->Threads(2)
67     ->MeasureProcessCPUTime()
68     ->UseRealTime();
69 BENCHMARK(BM_MainThread)
70     ->Iterations(1)
71     ->Threads(2)
72     ->MeasureProcessCPUTime()
73     ->UseManualTime();
74 
75 // ========================================================================= //
76 // BM_WorkerThread
77 
BM_WorkerThread(benchmark::State & state)78 void BM_WorkerThread(benchmark::State& state) {
79   for (auto _ : state) {
80     std::thread Worker(&MyBusySpinwait);
81     Worker.join();
82     state.SetIterationTime(time_frame_in_sec);
83   }
84   state.counters["invtime"] =
85       benchmark::Counter{1, benchmark::Counter::kIsRate};
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();
92 BENCHMARK(BM_WorkerThread)
93     ->Iterations(1)
94     ->Threads(1)
95     ->MeasureProcessCPUTime()
96     ->UseRealTime();
97 BENCHMARK(BM_WorkerThread)
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();
107 BENCHMARK(BM_WorkerThread)
108     ->Iterations(1)
109     ->Threads(2)
110     ->MeasureProcessCPUTime()
111     ->UseRealTime();
112 BENCHMARK(BM_WorkerThread)
113     ->Iterations(1)
114     ->Threads(2)
115     ->MeasureProcessCPUTime()
116     ->UseManualTime();
117 
118 // ========================================================================= //
119 // BM_MainThreadAndWorkerThread
120 
BM_MainThreadAndWorkerThread(benchmark::State & state)121 void BM_MainThreadAndWorkerThread(benchmark::State& state) {
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"] =
129       benchmark::Counter{1, benchmark::Counter::kIsRate};
130 }
131 
132 BENCHMARK(BM_MainThreadAndWorkerThread)->Iterations(1)->Threads(1);
133 BENCHMARK(BM_MainThreadAndWorkerThread)
134     ->Iterations(1)
135     ->Threads(1)
136     ->UseRealTime();
137 BENCHMARK(BM_MainThreadAndWorkerThread)
138     ->Iterations(1)
139     ->Threads(1)
140     ->UseManualTime();
141 BENCHMARK(BM_MainThreadAndWorkerThread)
142     ->Iterations(1)
143     ->Threads(1)
144     ->MeasureProcessCPUTime();
145 BENCHMARK(BM_MainThreadAndWorkerThread)
146     ->Iterations(1)
147     ->Threads(1)
148     ->MeasureProcessCPUTime()
149     ->UseRealTime();
150 BENCHMARK(BM_MainThreadAndWorkerThread)
151     ->Iterations(1)
152     ->Threads(1)
153     ->MeasureProcessCPUTime()
154     ->UseManualTime();
155 
156 BENCHMARK(BM_MainThreadAndWorkerThread)->Iterations(1)->Threads(2);
157 BENCHMARK(BM_MainThreadAndWorkerThread)
158     ->Iterations(1)
159     ->Threads(2)
160     ->UseRealTime();
161 BENCHMARK(BM_MainThreadAndWorkerThread)
162     ->Iterations(1)
163     ->Threads(2)
164     ->UseManualTime();
165 BENCHMARK(BM_MainThreadAndWorkerThread)
166     ->Iterations(1)
167     ->Threads(2)
168     ->MeasureProcessCPUTime();
169 BENCHMARK(BM_MainThreadAndWorkerThread)
170     ->Iterations(1)
171     ->Threads(2)
172     ->MeasureProcessCPUTime()
173     ->UseRealTime();
174 BENCHMARK(BM_MainThreadAndWorkerThread)
175     ->Iterations(1)
176     ->Threads(2)
177     ->MeasureProcessCPUTime()
178     ->UseManualTime();
179 
180 // ========================================================================= //
181 // ---------------------------- TEST CASES END ----------------------------- //
182 // ========================================================================= //
183 
main(int argc,char * argv[])184 int main(int argc, char* argv[]) { RunOutputTests(argc, argv); }
185