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

source code of third-party/benchmark/test/user_counters_test.cc