• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #undef NDEBUG
3 
4 #include <chrono>
5 #include <thread>
6 
7 #include "../src/timers.h"
8 #include "benchmark/benchmark.h"
9 #include "output_test.h"
10 
11 static const std::chrono::duration<double, std::milli> time_frame(50);
12 static const double time_frame_in_sec(
13     std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1, 1>>>(
14         time_frame)
15         .count());
16 
MyBusySpinwait()17 void MyBusySpinwait() {
18   const auto start = benchmark::ChronoClockNow();
19 
20   while (true) {
21     const auto now = benchmark::ChronoClockNow();
22     const auto elapsed = now - start;
23 
24     if (std::chrono::duration<double, std::chrono::seconds::period>(elapsed) >=
25         time_frame)
26       return;
27   }
28 }
29 
30 // ========================================================================= //
31 // --------------------------- TEST CASES BEGIN ---------------------------- //
32 // ========================================================================= //
33 
34 // ========================================================================= //
35 // BM_MainThread
36 
BM_MainThread(benchmark::State & state)37 void BM_MainThread(benchmark::State& state) {
38   for (auto _ : state) {
39     MyBusySpinwait();
40     state.SetIterationTime(time_frame_in_sec);
41   }
42   state.counters["invtime"] =
43       benchmark::Counter{1, benchmark::Counter::kIsRate};
44 }
45 
46 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1);
47 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->UseRealTime();
48 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->UseManualTime();
49 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(1)->MeasureProcessCPUTime();
50 BENCHMARK(BM_MainThread)
51     ->Iterations(1)
52     ->Threads(1)
53     ->MeasureProcessCPUTime()
54     ->UseRealTime();
55 BENCHMARK(BM_MainThread)
56     ->Iterations(1)
57     ->Threads(1)
58     ->MeasureProcessCPUTime()
59     ->UseManualTime();
60 
61 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2);
62 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->UseRealTime();
63 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->UseManualTime();
64 BENCHMARK(BM_MainThread)->Iterations(1)->Threads(2)->MeasureProcessCPUTime();
65 BENCHMARK(BM_MainThread)
66     ->Iterations(1)
67     ->Threads(2)
68     ->MeasureProcessCPUTime()
69     ->UseRealTime();
70 BENCHMARK(BM_MainThread)
71     ->Iterations(1)
72     ->Threads(2)
73     ->MeasureProcessCPUTime()
74     ->UseManualTime();
75 
76 // ========================================================================= //
77 // BM_WorkerThread
78 
BM_WorkerThread(benchmark::State & state)79 void BM_WorkerThread(benchmark::State& state) {
80   for (auto _ : state) {
81     std::thread Worker(&MyBusySpinwait);
82     Worker.join();
83     state.SetIterationTime(time_frame_in_sec);
84   }
85   state.counters["invtime"] =
86       benchmark::Counter{1, benchmark::Counter::kIsRate};
87 }
88 
89 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1);
90 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->UseRealTime();
91 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->UseManualTime();
92 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(1)->MeasureProcessCPUTime();
93 BENCHMARK(BM_WorkerThread)
94     ->Iterations(1)
95     ->Threads(1)
96     ->MeasureProcessCPUTime()
97     ->UseRealTime();
98 BENCHMARK(BM_WorkerThread)
99     ->Iterations(1)
100     ->Threads(1)
101     ->MeasureProcessCPUTime()
102     ->UseManualTime();
103 
104 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2);
105 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->UseRealTime();
106 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->UseManualTime();
107 BENCHMARK(BM_WorkerThread)->Iterations(1)->Threads(2)->MeasureProcessCPUTime();
108 BENCHMARK(BM_WorkerThread)
109     ->Iterations(1)
110     ->Threads(2)
111     ->MeasureProcessCPUTime()
112     ->UseRealTime();
113 BENCHMARK(BM_WorkerThread)
114     ->Iterations(1)
115     ->Threads(2)
116     ->MeasureProcessCPUTime()
117     ->UseManualTime();
118 
119 // ========================================================================= //
120 // BM_MainThreadAndWorkerThread
121 
BM_MainThreadAndWorkerThread(benchmark::State & state)122 void BM_MainThreadAndWorkerThread(benchmark::State& state) {
123   for (auto _ : state) {
124     std::thread Worker(&MyBusySpinwait);
125     MyBusySpinwait();
126     Worker.join();
127     state.SetIterationTime(time_frame_in_sec);
128   }
129   state.counters["invtime"] =
130       benchmark::Counter{1, benchmark::Counter::kIsRate};
131 }
132 
133 BENCHMARK(BM_MainThreadAndWorkerThread)->Iterations(1)->Threads(1);
134 BENCHMARK(BM_MainThreadAndWorkerThread)
135     ->Iterations(1)
136     ->Threads(1)
137     ->UseRealTime();
138 BENCHMARK(BM_MainThreadAndWorkerThread)
139     ->Iterations(1)
140     ->Threads(1)
141     ->UseManualTime();
142 BENCHMARK(BM_MainThreadAndWorkerThread)
143     ->Iterations(1)
144     ->Threads(1)
145     ->MeasureProcessCPUTime();
146 BENCHMARK(BM_MainThreadAndWorkerThread)
147     ->Iterations(1)
148     ->Threads(1)
149     ->MeasureProcessCPUTime()
150     ->UseRealTime();
151 BENCHMARK(BM_MainThreadAndWorkerThread)
152     ->Iterations(1)
153     ->Threads(1)
154     ->MeasureProcessCPUTime()
155     ->UseManualTime();
156 
157 BENCHMARK(BM_MainThreadAndWorkerThread)->Iterations(1)->Threads(2);
158 BENCHMARK(BM_MainThreadAndWorkerThread)
159     ->Iterations(1)
160     ->Threads(2)
161     ->UseRealTime();
162 BENCHMARK(BM_MainThreadAndWorkerThread)
163     ->Iterations(1)
164     ->Threads(2)
165     ->UseManualTime();
166 BENCHMARK(BM_MainThreadAndWorkerThread)
167     ->Iterations(1)
168     ->Threads(2)
169     ->MeasureProcessCPUTime();
170 BENCHMARK(BM_MainThreadAndWorkerThread)
171     ->Iterations(1)
172     ->Threads(2)
173     ->MeasureProcessCPUTime()
174     ->UseRealTime();
175 BENCHMARK(BM_MainThreadAndWorkerThread)
176     ->Iterations(1)
177     ->Threads(2)
178     ->MeasureProcessCPUTime()
179     ->UseManualTime();
180 
181 // ========================================================================= //
182 // ---------------------------- TEST CASES END ----------------------------- //
183 // ========================================================================= //
184 
main(int argc,char * argv[])185 int main(int argc, char* argv[]) { RunOutputTests(argc, argv); }
186