4
4
#include < chrono>
5
5
#include < thread>
6
6
#include " ../src/timers.h"
7
+ #include " ../src/check.h"
7
8
#include " benchmark/benchmark.h"
8
- #include " output_test.h"
9
9
10
10
static const std::chrono::duration<double , std::milli> time_frame (50 );
11
+ static const double time_frame_in_ns (
12
+ std::chrono::duration_cast<std::chrono::duration<double , std::nano>>(
13
+ time_frame)
14
+ .count());
11
15
static const double time_frame_in_sec (
12
16
std::chrono::duration_cast<std::chrono::duration<double , std::ratio<1 , 1 >>>(
13
17
time_frame)
14
18
.count());
15
19
20
+ static const int nr_iterations = 10 ;
21
+
22
+ // Waste exact amount of CPU time in busy-loop
16
23
void MyBusySpinwait () {
17
- const auto start = benchmark::ChronoClockNow ();
24
+ const auto start = benchmark::ThreadCPUUsage ();
18
25
19
26
while (true ) {
20
- const auto now = benchmark::ChronoClockNow ();
27
+ const auto now = benchmark::ThreadCPUUsage ();
21
28
const auto elapsed = now - start;
22
29
23
30
if (std::chrono::duration<double , std::chrono::seconds::period>(elapsed) >=
@@ -26,6 +33,77 @@ void MyBusySpinwait() {
26
33
}
27
34
}
28
35
36
+ class TestReporter : public benchmark ::ConsoleReporter {
37
+ int num_cpus = 0 ;
38
+ public:
39
+ virtual bool ReportContext (const Context& context) {
40
+ num_cpus = context.cpu_info .num_cpus ;
41
+ return ConsoleReporter::ReportContext (context);
42
+ };
43
+
44
+ virtual void ReportRuns (const std::vector<Run>& report) {
45
+ ConsoleReporter::ReportRuns (report);
46
+
47
+ for (auto &run: report) {
48
+ double expected_cpus = 1 ;
49
+ int64_t min_cpus = run.threads ;
50
+
51
+ if (run.run_name .function_name == " BM_WorkerThread" ) {
52
+ if (run.run_name .time_type == " " ||
53
+ run.run_name .time_type == " real_time" ||
54
+ run.run_name .time_type == " manual_time" ) {
55
+ expected_cpus = 0 ;
56
+ }
57
+ }
58
+
59
+ if (run.run_name .function_name == " BM_MainThreadAndWorkerThread" ) {
60
+ min_cpus *= 2 ;
61
+ if (run.run_name .time_type == " process_time" ||
62
+ run.run_name .time_type == " process_time/real_time" ||
63
+ run.run_name .time_type == " process_time/manual_time" ) {
64
+ expected_cpus = 2 ;
65
+ }
66
+ }
67
+
68
+ if (run.run_name .time_type == " manual_time" ||
69
+ run.run_name .time_type == " process_time/manual_time" ) {
70
+ min_cpus = 0 ;
71
+ }
72
+
73
+ double cpus = run.GetAdjustedCPUTime () / time_frame_in_ns;
74
+ double real = run.GetAdjustedRealTime () / time_frame_in_ns;
75
+
76
+ // Check that result >= expected (accuracy: relative=0.1, absolute=0.1)
77
+ CHECK_FLOAT_GE (cpus, expected_cpus, expected_cpus * 0.1 + 0.1 );
78
+ CHECK_FLOAT_GE (real, 1.0 , 0.2 );
79
+
80
+ // Warn if cpu time is bigger than expected.
81
+ if (cpus > expected_cpus * 1.1 + 0.1 ) {
82
+ VLOG (0 ) << " CPU time bigger than expected, might be cpu overload\n " ;
83
+ }
84
+
85
+ // Warn if real time is bigger than expected.
86
+ if (real > 1.2 ) {
87
+ VLOG (0 ) << " Real time bigger than expected, might be cpu overload\n " ;
88
+ }
89
+
90
+ // Check that cpu time <= expected * 2 + 20%
91
+ CHECK_FLOAT_LE (cpus, expected_cpus, expected_cpus + 0.2 );
92
+
93
+ // For checking real time require one more CPU for infrastructure
94
+ if (num_cpus < min_cpus + 1 ) {
95
+ VLOG (0 ) << " Not enough cpus to get valid real time\n " ;
96
+ } else {
97
+ // Measurements in CI are noisy, check real time <= expected * 4 + 20%
98
+ CHECK_FLOAT_LE (real, 4.0 , 0.2 );
99
+ }
100
+ }
101
+ }
102
+
103
+ TestReporter () {}
104
+ virtual ~TestReporter () {}
105
+ };
106
+
29
107
// ========================================================================= //
30
108
// --------------------------- TEST CASES BEGIN ---------------------------- //
31
109
// ========================================================================= //
@@ -42,32 +120,54 @@ void BM_MainThread(benchmark::State& state) {
42
120
benchmark::Counter{1 , benchmark::Counter::kIsRate };
43
121
}
44
122
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
123
BENCHMARK (BM_MainThread)
50
- ->Iterations(1 )
124
+ ->Iterations(nr_iterations)
125
+ ->Threads(1 );
126
+ BENCHMARK (BM_MainThread)
127
+ ->Iterations(nr_iterations)
128
+ ->Threads(1 )
129
+ ->UseRealTime();
130
+ BENCHMARK (BM_MainThread)
131
+ ->Iterations(nr_iterations)
132
+ ->Threads(1 )
133
+ ->UseManualTime();
134
+ BENCHMARK (BM_MainThread)
135
+ ->Iterations(nr_iterations)
136
+ ->Threads(1 )
137
+ ->MeasureProcessCPUTime();
138
+ BENCHMARK (BM_MainThread)
139
+ ->Iterations(nr_iterations)
51
140
->Threads(1 )
52
141
->MeasureProcessCPUTime()
53
142
->UseRealTime();
54
143
BENCHMARK (BM_MainThread)
55
- ->Iterations(1 )
144
+ ->Iterations(nr_iterations )
56
145
->Threads(1 )
57
146
->MeasureProcessCPUTime()
58
147
->UseManualTime();
59
148
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
149
BENCHMARK (BM_MainThread)
65
- ->Iterations(1 )
150
+ ->Iterations(nr_iterations)
151
+ ->Threads(2 );
152
+ BENCHMARK (BM_MainThread)
153
+ ->Iterations(nr_iterations)
154
+ ->Threads(2 )
155
+ ->UseRealTime();
156
+ BENCHMARK (BM_MainThread)
157
+ ->Iterations(nr_iterations)
158
+ ->Threads(2 )
159
+ ->UseManualTime();
160
+ BENCHMARK (BM_MainThread)
161
+ ->Iterations(nr_iterations)
162
+ ->Threads(2 )
163
+ ->MeasureProcessCPUTime();
164
+ BENCHMARK (BM_MainThread)
165
+ ->Iterations(nr_iterations)
66
166
->Threads(2 )
67
167
->MeasureProcessCPUTime()
68
168
->UseRealTime();
69
169
BENCHMARK (BM_MainThread)
70
- ->Iterations(1 )
170
+ ->Iterations(nr_iterations )
71
171
->Threads(2 )
72
172
->MeasureProcessCPUTime()
73
173
->UseManualTime();
@@ -85,32 +185,54 @@ void BM_WorkerThread(benchmark::State& state) {
85
185
benchmark::Counter{1 , benchmark::Counter::kIsRate };
86
186
}
87
187
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
188
BENCHMARK (BM_WorkerThread)
93
- ->Iterations(1 )
189
+ ->Iterations(nr_iterations)
190
+ ->Threads(1 );
191
+ BENCHMARK (BM_WorkerThread)
192
+ ->Iterations(nr_iterations)
193
+ ->Threads(1 )
194
+ ->UseRealTime();
195
+ BENCHMARK (BM_WorkerThread)
196
+ ->Iterations(nr_iterations)
197
+ ->Threads(1 )
198
+ ->UseManualTime();
199
+ BENCHMARK (BM_WorkerThread)
200
+ ->Iterations(nr_iterations)
201
+ ->Threads(1 )
202
+ ->MeasureProcessCPUTime();
203
+ BENCHMARK (BM_WorkerThread)
204
+ ->Iterations(nr_iterations)
94
205
->Threads(1 )
95
206
->MeasureProcessCPUTime()
96
207
->UseRealTime();
97
208
BENCHMARK (BM_WorkerThread)
98
- ->Iterations(1 )
209
+ ->Iterations(nr_iterations )
99
210
->Threads(1 )
100
211
->MeasureProcessCPUTime()
101
212
->UseManualTime();
102
213
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
214
BENCHMARK (BM_WorkerThread)
108
- ->Iterations(1 )
215
+ ->Iterations(nr_iterations)
216
+ ->Threads(2 );
217
+ BENCHMARK (BM_WorkerThread)
218
+ ->Iterations(nr_iterations)
219
+ ->Threads(2 )
220
+ ->UseRealTime();
221
+ BENCHMARK (BM_WorkerThread)
222
+ ->Iterations(nr_iterations)
223
+ ->Threads(2 )
224
+ ->UseManualTime();
225
+ BENCHMARK (BM_WorkerThread)
226
+ ->Iterations(nr_iterations)
227
+ ->Threads(2 )
228
+ ->MeasureProcessCPUTime();
229
+ BENCHMARK (BM_WorkerThread)
230
+ ->Iterations(nr_iterations)
109
231
->Threads(2 )
110
232
->MeasureProcessCPUTime()
111
233
->UseRealTime();
112
234
BENCHMARK (BM_WorkerThread)
113
- ->Iterations(1 )
235
+ ->Iterations(nr_iterations )
114
236
->Threads(2 )
115
237
->MeasureProcessCPUTime()
116
238
->UseManualTime();
@@ -129,50 +251,54 @@ void BM_MainThreadAndWorkerThread(benchmark::State& state) {
129
251
benchmark::Counter{1 , benchmark::Counter::kIsRate };
130
252
}
131
253
132
- BENCHMARK (BM_MainThreadAndWorkerThread)->Iterations(1 )->Threads(1 );
133
254
BENCHMARK (BM_MainThreadAndWorkerThread)
134
- ->Iterations(1 )
255
+ ->Iterations(nr_iterations)
256
+ ->Threads(1 );
257
+ BENCHMARK (BM_MainThreadAndWorkerThread)
258
+ ->Iterations(nr_iterations)
135
259
->Threads(1 )
136
260
->UseRealTime();
137
261
BENCHMARK (BM_MainThreadAndWorkerThread)
138
- ->Iterations(1 )
262
+ ->Iterations(nr_iterations )
139
263
->Threads(1 )
140
264
->UseManualTime();
141
265
BENCHMARK (BM_MainThreadAndWorkerThread)
142
- ->Iterations(1 )
266
+ ->Iterations(nr_iterations )
143
267
->Threads(1 )
144
268
->MeasureProcessCPUTime();
145
269
BENCHMARK (BM_MainThreadAndWorkerThread)
146
- ->Iterations(1 )
270
+ ->Iterations(nr_iterations )
147
271
->Threads(1 )
148
272
->MeasureProcessCPUTime()
149
273
->UseRealTime();
150
274
BENCHMARK (BM_MainThreadAndWorkerThread)
151
- ->Iterations(1 )
275
+ ->Iterations(nr_iterations )
152
276
->Threads(1 )
153
277
->MeasureProcessCPUTime()
154
278
->UseManualTime();
155
279
156
- BENCHMARK (BM_MainThreadAndWorkerThread)->Iterations(1 )->Threads(2 );
157
280
BENCHMARK (BM_MainThreadAndWorkerThread)
158
- ->Iterations(1 )
281
+ ->Iterations(nr_iterations)
282
+ ->Threads(2 );
283
+ BENCHMARK (BM_MainThreadAndWorkerThread)
284
+ ->Iterations(nr_iterations)
159
285
->Threads(2 )
160
286
->UseRealTime();
161
287
BENCHMARK (BM_MainThreadAndWorkerThread)
162
- ->Iterations(1 )
288
+ ->Iterations(nr_iterations )
163
289
->Threads(2 )
164
290
->UseManualTime();
165
291
BENCHMARK (BM_MainThreadAndWorkerThread)
166
- ->Iterations(1 )
292
+ ->Iterations(nr_iterations )
167
293
->Threads(2 )
168
294
->MeasureProcessCPUTime();
169
295
BENCHMARK (BM_MainThreadAndWorkerThread)
170
- ->Iterations(1 )
296
+ ->Iterations(nr_iterations )
171
297
->Threads(2 )
172
298
->MeasureProcessCPUTime()
173
299
->UseRealTime();
174
300
BENCHMARK (BM_MainThreadAndWorkerThread)
175
- ->Iterations(1 )
301
+ ->Iterations(nr_iterations )
176
302
->Threads(2 )
177
303
->MeasureProcessCPUTime()
178
304
->UseManualTime();
@@ -181,4 +307,9 @@ BENCHMARK(BM_MainThreadAndWorkerThread)
181
307
// ---------------------------- TEST CASES END ----------------------------- //
182
308
// ========================================================================= //
183
309
184
- int main (int argc, char * argv[]) { RunOutputTests (argc, argv); }
310
+ int main (int argc, char * argv[]) {
311
+ benchmark::Initialize (&argc, argv);
312
+ TestReporter test_reporter;
313
+ benchmark::RunSpecifiedBenchmarks (&test_reporter);
314
+ return 0 ;
315
+ }
0 commit comments