1 
2 #undef NDEBUG
3 
4 #include "benchmark/benchmark.h"
5 #include "output_test.h"
6 
7 // ========================================================================= //
8 // ---------------------- Testing Prologue Output -------------------------- //
9 // ========================================================================= //
10 
11 // clang-format off
12 
13 ADD_CASES(TC_ConsoleOut,
14           {{"^[-]+$", MR_Next},
15            {"^Benchmark %s Time %s CPU %s Iterations UserCounters...$", MR_Next},
16            {"^[-]+$", MR_Next}});
17 ADD_CASES(TC_CSVOut, {{"%csv_header,\"bar\",\"foo\""}});
18 
19 // clang-format on
20 
21 // ========================================================================= //
22 // ------------------------- Simple Counters Output ------------------------ //
23 // ========================================================================= //
24 
BM_Counters_Simple(benchmark::State & state)25 void BM_Counters_Simple(benchmark::State& state) {
26   for (auto _ : state) {
27   }
28   state.counters["foo"] = 1;
29   state.counters["bar"] = 2 * (double)state.iterations();
30 }
31 BENCHMARK(BM_Counters_Simple);
32 ADD_CASES(TC_ConsoleOut,
33           {{"^BM_Counters_Simple %console_report bar=%hrfloat foo=%hrfloat$"}});
34 ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_Counters_Simple\",$"},
35                        {"\"run_name\": \"BM_Counters_Simple\",$", MR_Next},
36                        {"\"run_type\": \"iteration\",$", MR_Next},
37                        {"\"repetitions\": 0,$", MR_Next},
38                        {"\"repetition_index\": 0,$", MR_Next},
39                        {"\"threads\": 1,$", MR_Next},
40                        {"\"iterations\": %int,$", MR_Next},
41                        {"\"real_time\": %float,$", MR_Next},
42                        {"\"cpu_time\": %float,$", MR_Next},
43                        {"\"time_unit\": \"ns\",$", MR_Next},
44                        {"\"bar\": %float,$", MR_Next},
45                        {"\"foo\": %float$", MR_Next},
46                        {"}", MR_Next}});
47 ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_Simple\",%csv_report,%float,%float$"}});
48 // VS2013 does not allow this function to be passed as a lambda argument
49 // to CHECK_BENCHMARK_RESULTS()
CheckSimple(Results const & e)50 void CheckSimple(Results const& e) {
51   double its = e.NumIterations();
52   CHECK_COUNTER_VALUE(e, int, "foo", EQ, 1);
53   // check that the value of bar is within 0.1% of the expected value
54   CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 2. * its, 0.001);
55 }
56 CHECK_BENCHMARK_RESULTS("BM_Counters_Simple", &CheckSimple);
57 
58 // ========================================================================= //
59 // --------------------- Counters+Items+Bytes/s Output --------------------- //
60 // ========================================================================= //
61 
62 namespace {
63 int num_calls1 = 0;
64 }
BM_Counters_WithBytesAndItemsPSec(benchmark::State & state)65 void BM_Counters_WithBytesAndItemsPSec(benchmark::State& state) {
66   for (auto _ : state) {
67     // This test requires a non-zero CPU time to avoid divide-by-zero
68     benchmark::DoNotOptimize(state.iterations());
69   }
70   state.counters["foo"] = 1;
71   state.counters["bar"] = ++num_calls1;
72   state.SetBytesProcessed(364);
73   state.SetItemsProcessed(150);
74 }
75 BENCHMARK(BM_Counters_WithBytesAndItemsPSec);
76 ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_WithBytesAndItemsPSec %console_report "
77                            "bar=%hrfloat bytes_per_second=%hrfloat/s "
78                            "foo=%hrfloat items_per_second=%hrfloat/s$"}});
79 ADD_CASES(TC_JSONOut,
80           {{"\"name\": \"BM_Counters_WithBytesAndItemsPSec\",$"},
81            {"\"run_name\": \"BM_Counters_WithBytesAndItemsPSec\",$", MR_Next},
82            {"\"run_type\": \"iteration\",$", MR_Next},
83            {"\"repetitions\": 0,$", MR_Next},
84            {"\"repetition_index\": 0,$", MR_Next},
85            {"\"threads\": 1,$", MR_Next},
86            {"\"iterations\": %int,$", MR_Next},
87            {"\"real_time\": %float,$", MR_Next},
88            {"\"cpu_time\": %float,$", MR_Next},
89            {"\"time_unit\": \"ns\",$", MR_Next},
90            {"\"bar\": %float,$", MR_Next},
91            {"\"bytes_per_second\": %float,$", MR_Next},
92            {"\"foo\": %float,$", MR_Next},
93            {"\"items_per_second\": %float$", MR_Next},
94            {"}", MR_Next}});
95 ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_WithBytesAndItemsPSec\","
96                        "%csv_bytes_items_report,%float,%float$"}});
97 // VS2013 does not allow this function to be passed as a lambda argument
98 // to CHECK_BENCHMARK_RESULTS()
CheckBytesAndItemsPSec(Results const & e)99 void CheckBytesAndItemsPSec(Results const& e) {
100   double t = e.DurationCPUTime();  // this (and not real time) is the time used
101   CHECK_COUNTER_VALUE(e, int, "foo", EQ, 1);
102   CHECK_COUNTER_VALUE(e, int, "bar", EQ, num_calls1);
103   // check that the values are within 0.1% of the expected values
104   CHECK_FLOAT_RESULT_VALUE(e, "bytes_per_second", EQ, 364. / t, 0.001);
105   CHECK_FLOAT_RESULT_VALUE(e, "items_per_second", EQ, 150. / t, 0.001);
106 }
107 CHECK_BENCHMARK_RESULTS("BM_Counters_WithBytesAndItemsPSec",
108                         &CheckBytesAndItemsPSec);
109 
110 // ========================================================================= //
111 // ------------------------- Rate Counters Output -------------------------- //
112 // ========================================================================= //
113 
BM_Counters_Rate(benchmark::State & state)114 void BM_Counters_Rate(benchmark::State& state) {
115   for (auto _ : state) {
116     // This test requires a non-zero CPU time to avoid divide-by-zero
117     benchmark::DoNotOptimize(state.iterations());
118   }
119   namespace bm = benchmark;
120   state.counters["foo"] = bm::Counter{1, bm::Counter::kIsRate};
121   state.counters["bar"] = bm::Counter{2, bm::Counter::kIsRate};
122 }
123 BENCHMARK(BM_Counters_Rate);
124 ADD_CASES(
125     TC_ConsoleOut,
126     {{"^BM_Counters_Rate %console_report bar=%hrfloat/s foo=%hrfloat/s$"}});
127 ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_Counters_Rate\",$"},
128                        {"\"run_name\": \"BM_Counters_Rate\",$", MR_Next},
129                        {"\"run_type\": \"iteration\",$", MR_Next},
130                        {"\"repetitions\": 0,$", MR_Next},
131                        {"\"repetition_index\": 0,$", MR_Next},
132                        {"\"threads\": 1,$", MR_Next},
133                        {"\"iterations\": %int,$", MR_Next},
134                        {"\"real_time\": %float,$", MR_Next},
135                        {"\"cpu_time\": %float,$", MR_Next},
136                        {"\"time_unit\": \"ns\",$", MR_Next},
137                        {"\"bar\": %float,$", MR_Next},
138                        {"\"foo\": %float$", MR_Next},
139                        {"}", MR_Next}});
140 ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_Rate\",%csv_report,%float,%float$"}});
141 // VS2013 does not allow this function to be passed as a lambda argument
142 // to CHECK_BENCHMARK_RESULTS()
CheckRate(Results const & e)143 void CheckRate(Results const& e) {
144   double t = e.DurationCPUTime();  // this (and not real time) is the time used
145   // check that the values are within 0.1% of the expected values
146   CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, 1. / t, 0.001);
147   CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 2. / t, 0.001);
148 }
149 CHECK_BENCHMARK_RESULTS("BM_Counters_Rate", &CheckRate);
150 
151 // ========================================================================= //
152 // ----------------------- Inverted Counters Output ------------------------ //
153 // ========================================================================= //
154 
BM_Invert(benchmark::State & state)155 void BM_Invert(benchmark::State& state) {
156   for (auto _ : state) {
157     // This test requires a non-zero CPU time to avoid divide-by-zero
158     benchmark::DoNotOptimize(state.iterations());
159   }
160   namespace bm = benchmark;
161   state.counters["foo"] = bm::Counter{0.0001, bm::Counter::kInvert};
162   state.counters["bar"] = bm::Counter{10000, bm::Counter::kInvert};
163 }
164 BENCHMARK(BM_Invert);
165 ADD_CASES(TC_ConsoleOut,
166           {{"^BM_Invert %console_report bar=%hrfloatu foo=%hrfloatk$"}});
167 ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_Invert\",$"},
168                        {"\"run_name\": \"BM_Invert\",$", MR_Next},
169                        {"\"run_type\": \"iteration\",$", MR_Next},
170                        {"\"repetitions\": 0,$", MR_Next},
171                        {"\"repetition_index\": 0,$", MR_Next},
172                        {"\"threads\": 1,$", MR_Next},
173                        {"\"iterations\": %int,$", MR_Next},
174                        {"\"real_time\": %float,$", MR_Next},
175                        {"\"cpu_time\": %float,$", MR_Next},
176                        {"\"time_unit\": \"ns\",$", MR_Next},
177                        {"\"bar\": %float,$", MR_Next},
178                        {"\"foo\": %float$", MR_Next},
179                        {"}", MR_Next}});
180 ADD_CASES(TC_CSVOut, {{"^\"BM_Invert\",%csv_report,%float,%float$"}});
181 // VS2013 does not allow this function to be passed as a lambda argument
182 // to CHECK_BENCHMARK_RESULTS()
CheckInvert(Results const & e)183 void CheckInvert(Results const& e) {
184   CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, 10000, 0.0001);
185   CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 0.0001, 0.0001);
186 }
187 CHECK_BENCHMARK_RESULTS("BM_Invert", &CheckInvert);
188 
189 // ========================================================================= //
190 // ------------------------- InvertedRate Counters Output
191 // -------------------------- //
192 // ========================================================================= //
193 
BM_Counters_InvertedRate(benchmark::State & state)194 void BM_Counters_InvertedRate(benchmark::State& state) {
195   for (auto _ : state) {
196     // This test requires a non-zero CPU time to avoid divide-by-zero
197     benchmark::DoNotOptimize(state.iterations());
198   }
199   namespace bm = benchmark;
200   state.counters["foo"] =
201       bm::Counter{1, bm::Counter::kIsRate | bm::Counter::kInvert};
202   state.counters["bar"] =
203       bm::Counter{8192, bm::Counter::kIsRate | bm::Counter::kInvert};
204 }
205 BENCHMARK(BM_Counters_InvertedRate);
206 ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_InvertedRate %console_report "
207                            "bar=%hrfloats foo=%hrfloats$"}});
208 ADD_CASES(TC_JSONOut,
209           {{"\"name\": \"BM_Counters_InvertedRate\",$"},
210            {"\"run_name\": \"BM_Counters_InvertedRate\",$", MR_Next},
211            {"\"run_type\": \"iteration\",$", MR_Next},
212            {"\"repetitions\": 0,$", MR_Next},
213            {"\"repetition_index\": 0,$", MR_Next},
214            {"\"threads\": 1,$", MR_Next},
215            {"\"iterations\": %int,$", MR_Next},
216            {"\"real_time\": %float,$", MR_Next},
217            {"\"cpu_time\": %float,$", MR_Next},
218            {"\"time_unit\": \"ns\",$", MR_Next},
219            {"\"bar\": %float,$", MR_Next},
220            {"\"foo\": %float$", MR_Next},
221            {"}", MR_Next}});
222 ADD_CASES(TC_CSVOut,
223           {{"^\"BM_Counters_InvertedRate\",%csv_report,%float,%float$"}});
224 // VS2013 does not allow this function to be passed as a lambda argument
225 // to CHECK_BENCHMARK_RESULTS()
CheckInvertedRate(Results const & e)226 void CheckInvertedRate(Results const& e) {
227   double t = e.DurationCPUTime();  // this (and not real time) is the time used
228   // check that the values are within 0.1% of the expected values
229   CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, t, 0.001);
230   CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, t / 8192.0, 0.001);
231 }
232 CHECK_BENCHMARK_RESULTS("BM_Counters_InvertedRate", &CheckInvertedRate);
233 
234 // ========================================================================= //
235 // ------------------------- Thread Counters Output ------------------------ //
236 // ========================================================================= //
237 
BM_Counters_Threads(benchmark::State & state)238 void BM_Counters_Threads(benchmark::State& state) {
239   for (auto _ : state) {
240   }
241   state.counters["foo"] = 1;
242   state.counters["bar"] = 2;
243 }
244 BENCHMARK(BM_Counters_Threads)->ThreadRange(1, 8);
245 ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_Threads/threads:%int %console_report "
246                            "bar=%hrfloat foo=%hrfloat$"}});
247 ADD_CASES(TC_JSONOut,
248           {{"\"name\": \"BM_Counters_Threads/threads:%int\",$"},
249            {"\"run_name\": \"BM_Counters_Threads/threads:%int\",$", MR_Next},
250            {"\"run_type\": \"iteration\",$", MR_Next},
251            {"\"repetitions\": 0,$", MR_Next},
252            {"\"repetition_index\": 0,$", MR_Next},
253            {"\"threads\": 1,$", MR_Next},
254            {"\"iterations\": %int,$", MR_Next},
255            {"\"real_time\": %float,$", MR_Next},
256            {"\"cpu_time\": %float,$", MR_Next},
257            {"\"time_unit\": \"ns\",$", MR_Next},
258            {"\"bar\": %float,$", MR_Next},
259            {"\"foo\": %float$", MR_Next},
260            {"}", MR_Next}});
261 ADD_CASES(
262     TC_CSVOut,
263     {{"^\"BM_Counters_Threads/threads:%int\",%csv_report,%float,%float$"}});
264 // VS2013 does not allow this function to be passed as a lambda argument
265 // to CHECK_BENCHMARK_RESULTS()
CheckThreads(Results const & e)266 void CheckThreads(Results const& e) {
267   CHECK_COUNTER_VALUE(e, int, "foo", EQ, e.NumThreads());
268   CHECK_COUNTER_VALUE(e, int, "bar", EQ, 2 * e.NumThreads());
269 }
270 CHECK_BENCHMARK_RESULTS("BM_Counters_Threads/threads:%int", &CheckThreads);
271 
272 // ========================================================================= //
273 // ---------------------- ThreadAvg Counters Output ------------------------ //
274 // ========================================================================= //
275 
BM_Counters_AvgThreads(benchmark::State & state)276 void BM_Counters_AvgThreads(benchmark::State& state) {
277   for (auto _ : state) {
278   }
279   namespace bm = benchmark;
280   state.counters["foo"] = bm::Counter{1, bm::Counter::kAvgThreads};
281   state.counters["bar"] = bm::Counter{2, bm::Counter::kAvgThreads};
282 }
283 BENCHMARK(BM_Counters_AvgThreads)->ThreadRange(1, 8);
284 ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_AvgThreads/threads:%int "
285                            "%console_report bar=%hrfloat foo=%hrfloat$"}});
286 ADD_CASES(TC_JSONOut,
287           {{"\"name\": \"BM_Counters_AvgThreads/threads:%int\",$"},
288            {"\"run_name\": \"BM_Counters_AvgThreads/threads:%int\",$", MR_Next},
289            {"\"run_type\": \"iteration\",$", MR_Next},
290            {"\"repetitions\": 0,$", MR_Next},
291            {"\"repetition_index\": 0,$", MR_Next},
292            {"\"threads\": 1,$", MR_Next},
293            {"\"iterations\": %int,$", MR_Next},
294            {"\"real_time\": %float,$", MR_Next},
295            {"\"cpu_time\": %float,$", MR_Next},
296            {"\"time_unit\": \"ns\",$", MR_Next},
297            {"\"bar\": %float,$", MR_Next},
298            {"\"foo\": %float$", MR_Next},
299            {"}", MR_Next}});
300 ADD_CASES(
301     TC_CSVOut,
302     {{"^\"BM_Counters_AvgThreads/threads:%int\",%csv_report,%float,%float$"}});
303 // VS2013 does not allow this function to be passed as a lambda argument
304 // to CHECK_BENCHMARK_RESULTS()
CheckAvgThreads(Results const & e)305 void CheckAvgThreads(Results const& e) {
306   CHECK_COUNTER_VALUE(e, int, "foo", EQ, 1);
307   CHECK_COUNTER_VALUE(e, int, "bar", EQ, 2);
308 }
309 CHECK_BENCHMARK_RESULTS("BM_Counters_AvgThreads/threads:%int",
310                         &CheckAvgThreads);
311 
312 // ========================================================================= //
313 // ---------------------- ThreadAvg Counters Output ------------------------ //
314 // ========================================================================= //
315 
BM_Counters_AvgThreadsRate(benchmark::State & state)316 void BM_Counters_AvgThreadsRate(benchmark::State& state) {
317   for (auto _ : state) {
318     // This test requires a non-zero CPU time to avoid divide-by-zero
319     benchmark::DoNotOptimize(state.iterations());
320   }
321   namespace bm = benchmark;
322   state.counters["foo"] = bm::Counter{1, bm::Counter::kAvgThreadsRate};
323   state.counters["bar"] = bm::Counter{2, bm::Counter::kAvgThreadsRate};
324 }
325 BENCHMARK(BM_Counters_AvgThreadsRate)->ThreadRange(1, 8);
326 ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_AvgThreadsRate/threads:%int "
327                            "%console_report bar=%hrfloat/s foo=%hrfloat/s$"}});
328 ADD_CASES(TC_JSONOut,
329           {{"\"name\": \"BM_Counters_AvgThreadsRate/threads:%int\",$"},
330            {"\"run_name\": \"BM_Counters_AvgThreadsRate/threads:%int\",$",
331             MR_Next},
332            {"\"run_type\": \"iteration\",$", MR_Next},
333            {"\"repetitions\": 0,$", MR_Next},
334            {"\"repetition_index\": 0,$", MR_Next},
335            {"\"threads\": 1,$", MR_Next},
336            {"\"iterations\": %int,$", MR_Next},
337            {"\"real_time\": %float,$", MR_Next},
338            {"\"cpu_time\": %float,$", MR_Next},
339            {"\"time_unit\": \"ns\",$", MR_Next},
340            {"\"bar\": %float,$", MR_Next},
341            {"\"foo\": %float$", MR_Next},
342            {"}", MR_Next}});
343 ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_AvgThreadsRate/"
344                        "threads:%int\",%csv_report,%float,%float$"}});
345 // VS2013 does not allow this function to be passed as a lambda argument
346 // to CHECK_BENCHMARK_RESULTS()
CheckAvgThreadsRate(Results const & e)347 void CheckAvgThreadsRate(Results const& e) {
348   CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, 1. / e.DurationCPUTime(), 0.001);
349   CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 2. / e.DurationCPUTime(), 0.001);
350 }
351 CHECK_BENCHMARK_RESULTS("BM_Counters_AvgThreadsRate/threads:%int",
352                         &CheckAvgThreadsRate);
353 
354 // ========================================================================= //
355 // ------------------- IterationInvariant Counters Output ------------------ //
356 // ========================================================================= //
357 
BM_Counters_IterationInvariant(benchmark::State & state)358 void BM_Counters_IterationInvariant(benchmark::State& state) {
359   for (auto _ : state) {
360   }
361   namespace bm = benchmark;
362   state.counters["foo"] = bm::Counter{1, bm::Counter::kIsIterationInvariant};
363   state.counters["bar"] = bm::Counter{2, bm::Counter::kIsIterationInvariant};
364 }
365 BENCHMARK(BM_Counters_IterationInvariant);
366 ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_IterationInvariant %console_report "
367                            "bar=%hrfloat foo=%hrfloat$"}});
368 ADD_CASES(TC_JSONOut,
369           {{"\"name\": \"BM_Counters_IterationInvariant\",$"},
370            {"\"run_name\": \"BM_Counters_IterationInvariant\",$", MR_Next},
371            {"\"run_type\": \"iteration\",$", MR_Next},
372            {"\"repetitions\": 0,$", MR_Next},
373            {"\"repetition_index\": 0,$", MR_Next},
374            {"\"threads\": 1,$", MR_Next},
375            {"\"iterations\": %int,$", MR_Next},
376            {"\"real_time\": %float,$", MR_Next},
377            {"\"cpu_time\": %float,$", MR_Next},
378            {"\"time_unit\": \"ns\",$", MR_Next},
379            {"\"bar\": %float,$", MR_Next},
380            {"\"foo\": %float$", MR_Next},
381            {"}", MR_Next}});
382 ADD_CASES(TC_CSVOut,
383           {{"^\"BM_Counters_IterationInvariant\",%csv_report,%float,%float$"}});
384 // VS2013 does not allow this function to be passed as a lambda argument
385 // to CHECK_BENCHMARK_RESULTS()
CheckIterationInvariant(Results const & e)386 void CheckIterationInvariant(Results const& e) {
387   double its = e.NumIterations();
388   // check that the values are within 0.1% of the expected value
389   CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, its, 0.001);
390   CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 2. * its, 0.001);
391 }
392 CHECK_BENCHMARK_RESULTS("BM_Counters_IterationInvariant",
393                         &CheckIterationInvariant);
394 
395 // ========================================================================= //
396 // ----------------- IterationInvariantRate Counters Output ---------------- //
397 // ========================================================================= //
398 
BM_Counters_kIsIterationInvariantRate(benchmark::State & state)399 void BM_Counters_kIsIterationInvariantRate(benchmark::State& state) {
400   for (auto _ : state) {
401     // This test requires a non-zero CPU time to avoid divide-by-zero
402     benchmark::DoNotOptimize(state.iterations());
403   }
404   namespace bm = benchmark;
405   state.counters["foo"] =
406       bm::Counter{1, bm::Counter::kIsIterationInvariantRate};
407   state.counters["bar"] =
408       bm::Counter{2, bm::Counter::kIsRate | bm::Counter::kIsIterationInvariant};
409 }
410 BENCHMARK(BM_Counters_kIsIterationInvariantRate);
411 ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_kIsIterationInvariantRate "
412                            "%console_report bar=%hrfloat/s foo=%hrfloat/s$"}});
413 ADD_CASES(TC_JSONOut,
414           {{"\"name\": \"BM_Counters_kIsIterationInvariantRate\",$"},
415            {"\"run_name\": \"BM_Counters_kIsIterationInvariantRate\",$",
416             MR_Next},
417            {"\"run_type\": \"iteration\",$", MR_Next},
418            {"\"repetitions\": 0,$", MR_Next},
419            {"\"repetition_index\": 0,$", MR_Next},
420            {"\"threads\": 1,$", MR_Next},
421            {"\"iterations\": %int,$", MR_Next},
422            {"\"real_time\": %float,$", MR_Next},
423            {"\"cpu_time\": %float,$", MR_Next},
424            {"\"time_unit\": \"ns\",$", MR_Next},
425            {"\"bar\": %float,$", MR_Next},
426            {"\"foo\": %float$", MR_Next},
427            {"}", MR_Next}});
428 ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_kIsIterationInvariantRate\",%csv_report,"
429                        "%float,%float$"}});
430 // VS2013 does not allow this function to be passed as a lambda argument
431 // to CHECK_BENCHMARK_RESULTS()
CheckIsIterationInvariantRate(Results const & e)432 void CheckIsIterationInvariantRate(Results const& e) {
433   double its = e.NumIterations();
434   double t = e.DurationCPUTime();  // this (and not real time) is the time used
435   // check that the values are within 0.1% of the expected values
436   CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, its * 1. / t, 0.001);
437   CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, its * 2. / t, 0.001);
438 }
439 CHECK_BENCHMARK_RESULTS("BM_Counters_kIsIterationInvariantRate",
440                         &CheckIsIterationInvariantRate);
441 
442 // ========================================================================= //
443 // ------------------- AvgIterations Counters Output ------------------ //
444 // ========================================================================= //
445 
BM_Counters_AvgIterations(benchmark::State & state)446 void BM_Counters_AvgIterations(benchmark::State& state) {
447   for (auto _ : state) {
448   }
449   namespace bm = benchmark;
450   state.counters["foo"] = bm::Counter{1, bm::Counter::kAvgIterations};
451   state.counters["bar"] = bm::Counter{2, bm::Counter::kAvgIterations};
452 }
453 BENCHMARK(BM_Counters_AvgIterations);
454 ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_AvgIterations %console_report "
455                            "bar=%hrfloat foo=%hrfloat$"}});
456 ADD_CASES(TC_JSONOut,
457           {{"\"name\": \"BM_Counters_AvgIterations\",$"},
458            {"\"run_name\": \"BM_Counters_AvgIterations\",$", MR_Next},
459            {"\"run_type\": \"iteration\",$", MR_Next},
460            {"\"repetitions\": 0,$", MR_Next},
461            {"\"repetition_index\": 0,$", MR_Next},
462            {"\"threads\": 1,$", MR_Next},
463            {"\"iterations\": %int,$", MR_Next},
464            {"\"real_time\": %float,$", MR_Next},
465            {"\"cpu_time\": %float,$", MR_Next},
466            {"\"time_unit\": \"ns\",$", MR_Next},
467            {"\"bar\": %float,$", MR_Next},
468            {"\"foo\": %float$", MR_Next},
469            {"}", MR_Next}});
470 ADD_CASES(TC_CSVOut,
471           {{"^\"BM_Counters_AvgIterations\",%csv_report,%float,%float$"}});
472 // VS2013 does not allow this function to be passed as a lambda argument
473 // to CHECK_BENCHMARK_RESULTS()
CheckAvgIterations(Results const & e)474 void CheckAvgIterations(Results const& e) {
475   double its = e.NumIterations();
476   // check that the values are within 0.1% of the expected value
477   CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, 1. / its, 0.001);
478   CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 2. / its, 0.001);
479 }
480 CHECK_BENCHMARK_RESULTS("BM_Counters_AvgIterations", &CheckAvgIterations);
481 
482 // ========================================================================= //
483 // ----------------- AvgIterationsRate Counters Output ---------------- //
484 // ========================================================================= //
485 
BM_Counters_kAvgIterationsRate(benchmark::State & state)486 void BM_Counters_kAvgIterationsRate(benchmark::State& state) {
487   for (auto _ : state) {
488     // This test requires a non-zero CPU time to avoid divide-by-zero
489     benchmark::DoNotOptimize(state.iterations());
490   }
491   namespace bm = benchmark;
492   state.counters["foo"] = bm::Counter{1, bm::Counter::kAvgIterationsRate};
493   state.counters["bar"] =
494       bm::Counter{2, bm::Counter::kIsRate | bm::Counter::kAvgIterations};
495 }
496 BENCHMARK(BM_Counters_kAvgIterationsRate);
497 ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_kAvgIterationsRate "
498                            "%console_report bar=%hrfloat/s foo=%hrfloat/s$"}});
499 ADD_CASES(TC_JSONOut,
500           {{"\"name\": \"BM_Counters_kAvgIterationsRate\",$"},
501            {"\"run_name\": \"BM_Counters_kAvgIterationsRate\",$", MR_Next},
502            {"\"run_type\": \"iteration\",$", MR_Next},
503            {"\"repetitions\": 0,$", MR_Next},
504            {"\"repetition_index\": 0,$", MR_Next},
505            {"\"threads\": 1,$", MR_Next},
506            {"\"iterations\": %int,$", MR_Next},
507            {"\"real_time\": %float,$", MR_Next},
508            {"\"cpu_time\": %float,$", MR_Next},
509            {"\"time_unit\": \"ns\",$", MR_Next},
510            {"\"bar\": %float,$", MR_Next},
511            {"\"foo\": %float$", MR_Next},
512            {"}", MR_Next}});
513 ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_kAvgIterationsRate\",%csv_report,"
514                        "%float,%float$"}});
515 // VS2013 does not allow this function to be passed as a lambda argument
516 // to CHECK_BENCHMARK_RESULTS()
CheckAvgIterationsRate(Results const & e)517 void CheckAvgIterationsRate(Results const& e) {
518   double its = e.NumIterations();
519   double t = e.DurationCPUTime();  // this (and not real time) is the time used
520   // check that the values are within 0.1% of the expected values
521   CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, 1. / its / t, 0.001);
522   CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 2. / its / t, 0.001);
523 }
524 CHECK_BENCHMARK_RESULTS("BM_Counters_kAvgIterationsRate",
525                         &CheckAvgIterationsRate);
526 
527 // ========================================================================= //
528 // --------------------------- TEST CASES END ------------------------------ //
529 // ========================================================================= //
530 
main(int argc,char * argv[])531 int main(int argc, char* argv[]) { RunOutputTests(argc, argv); }
532