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