You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

561 lines
26 KiB

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