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
+
16
22
void MyBusySpinwait () {
17
23
const auto start = benchmark::ChronoClockNow ();
18
24
@@ -26,6 +32,53 @@ void MyBusySpinwait() {
26
32
}
27
33
}
28
34
35
+ class TestReporter : public benchmark ::ConsoleReporter {
36
+ int num_cpus = 0 ;
37
+ public:
38
+ virtual bool ReportContext (const Context& context) {
39
+ num_cpus = context.cpu_info .num_cpus ;
40
+ return ConsoleReporter::ReportContext (context);
41
+ };
42
+
43
+ virtual void ReportRuns (const std::vector<Run>& report) {
44
+ ConsoleReporter::ReportRuns (report);
45
+
46
+ for (auto &run: report) {
47
+ double expected_cpus = 1 ;
48
+ int64_t min_cpus = run.threads ;
49
+
50
+ if (run.run_name .function_name == " BM_WorkerThread" ) {
51
+ if (run.run_name .time_type == " " ||
52
+ run.run_name .time_type == " real_time" ||
53
+ run.run_name .time_type == " manual_time" ) {
54
+ expected_cpus = 0 ;
55
+ }
56
+ }
57
+
58
+ if (run.run_name .function_name == " BM_MainThreadAndWorkerThread" ) {
59
+ min_cpus *= 2 ;
60
+ if (run.run_name .time_type == " process_time" ||
61
+ run.run_name .time_type == " process_time/real_time" ||
62
+ run.run_name .time_type == " process_time/manual_time" ) {
63
+ expected_cpus = 2 ;
64
+ }
65
+ }
66
+
67
+ if (num_cpus < min_cpus) {
68
+ VLOG (0 ) << " Not enough cpus to get valid time\n " ;
69
+ continue ;
70
+ }
71
+
72
+ // -/+ 20%
73
+ CHECK_FLOAT_EQ (run.GetAdjustedRealTime () / time_frame_in_ns, 1.0 , 0.2 );
74
+ CHECK_FLOAT_EQ (run.GetAdjustedCPUTime () / time_frame_in_ns, expected_cpus, 0.2 );
75
+ }
76
+ }
77
+
78
+ TestReporter () {}
79
+ virtual ~TestReporter () {}
80
+ };
81
+
29
82
// ========================================================================= //
30
83
// --------------------------- TEST CASES BEGIN ---------------------------- //
31
84
// ========================================================================= //
@@ -42,32 +95,54 @@ void BM_MainThread(benchmark::State& state) {
42
95
benchmark::Counter{1 , benchmark::Counter::kIsRate };
43
96
}
44
97
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
98
BENCHMARK (BM_MainThread)
50
- ->Iterations(1 )
99
+ ->Iterations(nr_iterations)
100
+ ->Threads(1 );
101
+ BENCHMARK (BM_MainThread)
102
+ ->Iterations(nr_iterations)
103
+ ->Threads(1 )
104
+ ->UseRealTime();
105
+ BENCHMARK (BM_MainThread)
106
+ ->Iterations(nr_iterations)
107
+ ->Threads(1 )
108
+ ->UseManualTime();
109
+ BENCHMARK (BM_MainThread)
110
+ ->Iterations(nr_iterations)
111
+ ->Threads(1 )
112
+ ->MeasureProcessCPUTime();
113
+ BENCHMARK (BM_MainThread)
114
+ ->Iterations(nr_iterations)
51
115
->Threads(1 )
52
116
->MeasureProcessCPUTime()
53
117
->UseRealTime();
54
118
BENCHMARK (BM_MainThread)
55
- ->Iterations(1 )
119
+ ->Iterations(nr_iterations )
56
120
->Threads(1 )
57
121
->MeasureProcessCPUTime()
58
122
->UseManualTime();
59
123
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
124
BENCHMARK (BM_MainThread)
65
- ->Iterations(1 )
125
+ ->Iterations(nr_iterations)
126
+ ->Threads(2 );
127
+ BENCHMARK (BM_MainThread)
128
+ ->Iterations(nr_iterations)
129
+ ->Threads(2 )
130
+ ->UseRealTime();
131
+ BENCHMARK (BM_MainThread)
132
+ ->Iterations(nr_iterations)
133
+ ->Threads(2 )
134
+ ->UseManualTime();
135
+ BENCHMARK (BM_MainThread)
136
+ ->Iterations(nr_iterations)
137
+ ->Threads(2 )
138
+ ->MeasureProcessCPUTime();
139
+ BENCHMARK (BM_MainThread)
140
+ ->Iterations(nr_iterations)
66
141
->Threads(2 )
67
142
->MeasureProcessCPUTime()
68
143
->UseRealTime();
69
144
BENCHMARK (BM_MainThread)
70
- ->Iterations(1 )
145
+ ->Iterations(nr_iterations )
71
146
->Threads(2 )
72
147
->MeasureProcessCPUTime()
73
148
->UseManualTime();
@@ -85,32 +160,54 @@ void BM_WorkerThread(benchmark::State& state) {
85
160
benchmark::Counter{1 , benchmark::Counter::kIsRate };
86
161
}
87
162
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
163
BENCHMARK (BM_WorkerThread)
93
- ->Iterations(1 )
164
+ ->Iterations(nr_iterations)
165
+ ->Threads(1 );
166
+ BENCHMARK (BM_WorkerThread)
167
+ ->Iterations(nr_iterations)
168
+ ->Threads(1 )
169
+ ->UseRealTime();
170
+ BENCHMARK (BM_WorkerThread)
171
+ ->Iterations(nr_iterations)
172
+ ->Threads(1 )
173
+ ->UseManualTime();
174
+ BENCHMARK (BM_WorkerThread)
175
+ ->Iterations(nr_iterations)
176
+ ->Threads(1 )
177
+ ->MeasureProcessCPUTime();
178
+ BENCHMARK (BM_WorkerThread)
179
+ ->Iterations(nr_iterations)
94
180
->Threads(1 )
95
181
->MeasureProcessCPUTime()
96
182
->UseRealTime();
97
183
BENCHMARK (BM_WorkerThread)
98
- ->Iterations(1 )
184
+ ->Iterations(nr_iterations )
99
185
->Threads(1 )
100
186
->MeasureProcessCPUTime()
101
187
->UseManualTime();
102
188
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
189
BENCHMARK (BM_WorkerThread)
108
- ->Iterations(1 )
190
+ ->Iterations(nr_iterations)
191
+ ->Threads(2 );
192
+ BENCHMARK (BM_WorkerThread)
193
+ ->Iterations(nr_iterations)
194
+ ->Threads(2 )
195
+ ->UseRealTime();
196
+ BENCHMARK (BM_WorkerThread)
197
+ ->Iterations(nr_iterations)
198
+ ->Threads(2 )
199
+ ->UseManualTime();
200
+ BENCHMARK (BM_WorkerThread)
201
+ ->Iterations(nr_iterations)
202
+ ->Threads(2 )
203
+ ->MeasureProcessCPUTime();
204
+ BENCHMARK (BM_WorkerThread)
205
+ ->Iterations(nr_iterations)
109
206
->Threads(2 )
110
207
->MeasureProcessCPUTime()
111
208
->UseRealTime();
112
209
BENCHMARK (BM_WorkerThread)
113
- ->Iterations(1 )
210
+ ->Iterations(nr_iterations )
114
211
->Threads(2 )
115
212
->MeasureProcessCPUTime()
116
213
->UseManualTime();
@@ -129,50 +226,54 @@ void BM_MainThreadAndWorkerThread(benchmark::State& state) {
129
226
benchmark::Counter{1 , benchmark::Counter::kIsRate };
130
227
}
131
228
132
- BENCHMARK (BM_MainThreadAndWorkerThread)->Iterations(1 )->Threads(1 );
133
229
BENCHMARK (BM_MainThreadAndWorkerThread)
134
- ->Iterations(1 )
230
+ ->Iterations(nr_iterations)
231
+ ->Threads(1 );
232
+ BENCHMARK (BM_MainThreadAndWorkerThread)
233
+ ->Iterations(nr_iterations)
135
234
->Threads(1 )
136
235
->UseRealTime();
137
236
BENCHMARK (BM_MainThreadAndWorkerThread)
138
- ->Iterations(1 )
237
+ ->Iterations(nr_iterations )
139
238
->Threads(1 )
140
239
->UseManualTime();
141
240
BENCHMARK (BM_MainThreadAndWorkerThread)
142
- ->Iterations(1 )
241
+ ->Iterations(nr_iterations )
143
242
->Threads(1 )
144
243
->MeasureProcessCPUTime();
145
244
BENCHMARK (BM_MainThreadAndWorkerThread)
146
- ->Iterations(1 )
245
+ ->Iterations(nr_iterations )
147
246
->Threads(1 )
148
247
->MeasureProcessCPUTime()
149
248
->UseRealTime();
150
249
BENCHMARK (BM_MainThreadAndWorkerThread)
151
- ->Iterations(1 )
250
+ ->Iterations(nr_iterations )
152
251
->Threads(1 )
153
252
->MeasureProcessCPUTime()
154
253
->UseManualTime();
155
254
156
- BENCHMARK (BM_MainThreadAndWorkerThread)->Iterations(1 )->Threads(2 );
157
255
BENCHMARK (BM_MainThreadAndWorkerThread)
158
- ->Iterations(1 )
256
+ ->Iterations(nr_iterations)
257
+ ->Threads(2 );
258
+ BENCHMARK (BM_MainThreadAndWorkerThread)
259
+ ->Iterations(nr_iterations)
159
260
->Threads(2 )
160
261
->UseRealTime();
161
262
BENCHMARK (BM_MainThreadAndWorkerThread)
162
- ->Iterations(1 )
263
+ ->Iterations(nr_iterations )
163
264
->Threads(2 )
164
265
->UseManualTime();
165
266
BENCHMARK (BM_MainThreadAndWorkerThread)
166
- ->Iterations(1 )
267
+ ->Iterations(nr_iterations )
167
268
->Threads(2 )
168
269
->MeasureProcessCPUTime();
169
270
BENCHMARK (BM_MainThreadAndWorkerThread)
170
- ->Iterations(1 )
271
+ ->Iterations(nr_iterations )
171
272
->Threads(2 )
172
273
->MeasureProcessCPUTime()
173
274
->UseRealTime();
174
275
BENCHMARK (BM_MainThreadAndWorkerThread)
175
- ->Iterations(1 )
276
+ ->Iterations(nr_iterations )
176
277
->Threads(2 )
177
278
->MeasureProcessCPUTime()
178
279
->UseManualTime();
@@ -181,4 +282,9 @@ BENCHMARK(BM_MainThreadAndWorkerThread)
181
282
// ---------------------------- TEST CASES END ----------------------------- //
182
283
// ========================================================================= //
183
284
184
- int main (int argc, char * argv[]) { RunOutputTests (argc, argv); }
285
+ int main (int argc, char * argv[]) {
286
+ benchmark::Initialize (&argc, argv);
287
+ TestReporter test_reporter;
288
+ benchmark::RunSpecifiedBenchmarks (&test_reporter);
289
+ return 0 ;
290
+ }
0 commit comments