user_counters_test.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 * (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. {"\"run_name\": \"BM_Counters_Simple\",$", MR_Next},
  28. {"\"run_type\": \"iteration\",$", MR_Next},
  29. {"\"iterations\": %int,$", MR_Next},
  30. {"\"real_time\": %float,$", MR_Next},
  31. {"\"cpu_time\": %float,$", MR_Next},
  32. {"\"time_unit\": \"ns\",$", MR_Next},
  33. {"\"bar\": %float,$", MR_Next},
  34. {"\"foo\": %float$", MR_Next},
  35. {"}", MR_Next}});
  36. ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_Simple\",%csv_report,%float,%float$"}});
  37. // VS2013 does not allow this function to be passed as a lambda argument
  38. // to CHECK_BENCHMARK_RESULTS()
  39. void CheckSimple(Results const& e) {
  40. double its = e.NumIterations();
  41. CHECK_COUNTER_VALUE(e, int, "foo", EQ, 1);
  42. // check that the value of bar is within 0.1% of the expected value
  43. CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 2. * its, 0.001);
  44. }
  45. CHECK_BENCHMARK_RESULTS("BM_Counters_Simple", &CheckSimple);
  46. // ========================================================================= //
  47. // --------------------- Counters+Items+Bytes/s Output --------------------- //
  48. // ========================================================================= //
  49. namespace {
  50. int num_calls1 = 0;
  51. }
  52. void BM_Counters_WithBytesAndItemsPSec(benchmark::State& state) {
  53. for (auto _ : state) {
  54. }
  55. state.counters["foo"] = 1;
  56. state.counters["bar"] = ++num_calls1;
  57. state.SetBytesProcessed(364);
  58. state.SetItemsProcessed(150);
  59. }
  60. BENCHMARK(BM_Counters_WithBytesAndItemsPSec);
  61. ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_WithBytesAndItemsPSec %console_report "
  62. "bar=%hrfloat bytes_per_second=%hrfloat/s "
  63. "foo=%hrfloat items_per_second=%hrfloat/s$"}});
  64. ADD_CASES(TC_JSONOut,
  65. {{"\"name\": \"BM_Counters_WithBytesAndItemsPSec\",$"},
  66. {"\"run_name\": \"BM_Counters_WithBytesAndItemsPSec\",$", MR_Next},
  67. {"\"run_type\": \"iteration\",$", MR_Next},
  68. {"\"iterations\": %int,$", MR_Next},
  69. {"\"real_time\": %float,$", MR_Next},
  70. {"\"cpu_time\": %float,$", MR_Next},
  71. {"\"time_unit\": \"ns\",$", MR_Next},
  72. {"\"bar\": %float,$", MR_Next},
  73. {"\"bytes_per_second\": %float,$", MR_Next},
  74. {"\"foo\": %float,$", MR_Next},
  75. {"\"items_per_second\": %float$", MR_Next},
  76. {"}", MR_Next}});
  77. ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_WithBytesAndItemsPSec\","
  78. "%csv_bytes_items_report,%float,%float$"}});
  79. // VS2013 does not allow this function to be passed as a lambda argument
  80. // to CHECK_BENCHMARK_RESULTS()
  81. void CheckBytesAndItemsPSec(Results const& e) {
  82. double t = e.DurationCPUTime(); // this (and not real time) is the time used
  83. CHECK_COUNTER_VALUE(e, int, "foo", EQ, 1);
  84. CHECK_COUNTER_VALUE(e, int, "bar", EQ, num_calls1);
  85. // check that the values are within 0.1% of the expected values
  86. CHECK_FLOAT_RESULT_VALUE(e, "bytes_per_second", EQ, 364. / t, 0.001);
  87. CHECK_FLOAT_RESULT_VALUE(e, "items_per_second", EQ, 150. / t, 0.001);
  88. }
  89. CHECK_BENCHMARK_RESULTS("BM_Counters_WithBytesAndItemsPSec",
  90. &CheckBytesAndItemsPSec);
  91. // ========================================================================= //
  92. // ------------------------- Rate Counters Output -------------------------- //
  93. // ========================================================================= //
  94. void BM_Counters_Rate(benchmark::State& state) {
  95. for (auto _ : state) {
  96. }
  97. namespace bm = benchmark;
  98. state.counters["foo"] = bm::Counter{1, bm::Counter::kIsRate};
  99. state.counters["bar"] = bm::Counter{2, bm::Counter::kIsRate};
  100. }
  101. BENCHMARK(BM_Counters_Rate);
  102. ADD_CASES(
  103. TC_ConsoleOut,
  104. {{"^BM_Counters_Rate %console_report bar=%hrfloat/s foo=%hrfloat/s$"}});
  105. ADD_CASES(TC_JSONOut, {{"\"name\": \"BM_Counters_Rate\",$"},
  106. {"\"run_name\": \"BM_Counters_Rate\",$", MR_Next},
  107. {"\"run_type\": \"iteration\",$", MR_Next},
  108. {"\"iterations\": %int,$", MR_Next},
  109. {"\"real_time\": %float,$", MR_Next},
  110. {"\"cpu_time\": %float,$", MR_Next},
  111. {"\"time_unit\": \"ns\",$", MR_Next},
  112. {"\"bar\": %float,$", MR_Next},
  113. {"\"foo\": %float$", MR_Next},
  114. {"}", MR_Next}});
  115. ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_Rate\",%csv_report,%float,%float$"}});
  116. // VS2013 does not allow this function to be passed as a lambda argument
  117. // to CHECK_BENCHMARK_RESULTS()
  118. void CheckRate(Results const& e) {
  119. double t = e.DurationCPUTime(); // this (and not real time) is the time used
  120. // check that the values are within 0.1% of the expected values
  121. CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, 1. / t, 0.001);
  122. CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 2. / t, 0.001);
  123. }
  124. CHECK_BENCHMARK_RESULTS("BM_Counters_Rate", &CheckRate);
  125. // ========================================================================= //
  126. // ------------------------- Thread Counters Output ------------------------ //
  127. // ========================================================================= //
  128. void BM_Counters_Threads(benchmark::State& state) {
  129. for (auto _ : state) {
  130. }
  131. state.counters["foo"] = 1;
  132. state.counters["bar"] = 2;
  133. }
  134. BENCHMARK(BM_Counters_Threads)->ThreadRange(1, 8);
  135. ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_Threads/threads:%int %console_report "
  136. "bar=%hrfloat foo=%hrfloat$"}});
  137. ADD_CASES(TC_JSONOut,
  138. {{"\"name\": \"BM_Counters_Threads/threads:%int\",$"},
  139. {"\"run_name\": \"BM_Counters_Threads/threads:%int\",$", MR_Next},
  140. {"\"run_type\": \"iteration\",$", 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}});
  148. ADD_CASES(
  149. TC_CSVOut,
  150. {{"^\"BM_Counters_Threads/threads:%int\",%csv_report,%float,%float$"}});
  151. // VS2013 does not allow this function to be passed as a lambda argument
  152. // to CHECK_BENCHMARK_RESULTS()
  153. void CheckThreads(Results const& e) {
  154. CHECK_COUNTER_VALUE(e, int, "foo", EQ, e.NumThreads());
  155. CHECK_COUNTER_VALUE(e, int, "bar", EQ, 2 * e.NumThreads());
  156. }
  157. CHECK_BENCHMARK_RESULTS("BM_Counters_Threads/threads:%int", &CheckThreads);
  158. // ========================================================================= //
  159. // ---------------------- ThreadAvg Counters Output ------------------------ //
  160. // ========================================================================= //
  161. void BM_Counters_AvgThreads(benchmark::State& state) {
  162. for (auto _ : state) {
  163. }
  164. namespace bm = benchmark;
  165. state.counters["foo"] = bm::Counter{1, bm::Counter::kAvgThreads};
  166. state.counters["bar"] = bm::Counter{2, bm::Counter::kAvgThreads};
  167. }
  168. BENCHMARK(BM_Counters_AvgThreads)->ThreadRange(1, 8);
  169. ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_AvgThreads/threads:%int "
  170. "%console_report bar=%hrfloat foo=%hrfloat$"}});
  171. ADD_CASES(TC_JSONOut,
  172. {{"\"name\": \"BM_Counters_AvgThreads/threads:%int\",$"},
  173. {"\"run_name\": \"BM_Counters_AvgThreads/threads:%int\",$", MR_Next},
  174. {"\"run_type\": \"iteration\",$", MR_Next},
  175. {"\"iterations\": %int,$", MR_Next},
  176. {"\"real_time\": %float,$", MR_Next},
  177. {"\"cpu_time\": %float,$", MR_Next},
  178. {"\"time_unit\": \"ns\",$", MR_Next},
  179. {"\"bar\": %float,$", MR_Next},
  180. {"\"foo\": %float$", MR_Next},
  181. {"}", MR_Next}});
  182. ADD_CASES(
  183. TC_CSVOut,
  184. {{"^\"BM_Counters_AvgThreads/threads:%int\",%csv_report,%float,%float$"}});
  185. // VS2013 does not allow this function to be passed as a lambda argument
  186. // to CHECK_BENCHMARK_RESULTS()
  187. void CheckAvgThreads(Results const& e) {
  188. CHECK_COUNTER_VALUE(e, int, "foo", EQ, 1);
  189. CHECK_COUNTER_VALUE(e, int, "bar", EQ, 2);
  190. }
  191. CHECK_BENCHMARK_RESULTS("BM_Counters_AvgThreads/threads:%int",
  192. &CheckAvgThreads);
  193. // ========================================================================= //
  194. // ---------------------- ThreadAvg Counters Output ------------------------ //
  195. // ========================================================================= //
  196. void BM_Counters_AvgThreadsRate(benchmark::State& state) {
  197. for (auto _ : state) {
  198. }
  199. namespace bm = benchmark;
  200. state.counters["foo"] = bm::Counter{1, bm::Counter::kAvgThreadsRate};
  201. state.counters["bar"] = bm::Counter{2, bm::Counter::kAvgThreadsRate};
  202. }
  203. BENCHMARK(BM_Counters_AvgThreadsRate)->ThreadRange(1, 8);
  204. ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_AvgThreadsRate/threads:%int "
  205. "%console_report bar=%hrfloat/s foo=%hrfloat/s$"}});
  206. ADD_CASES(TC_JSONOut,
  207. {{"\"name\": \"BM_Counters_AvgThreadsRate/threads:%int\",$"},
  208. {"\"run_name\": \"BM_Counters_AvgThreadsRate/threads:%int\",$",
  209. MR_Next},
  210. {"\"run_type\": \"iteration\",$", MR_Next},
  211. {"\"iterations\": %int,$", MR_Next},
  212. {"\"real_time\": %float,$", MR_Next},
  213. {"\"cpu_time\": %float,$", MR_Next},
  214. {"\"time_unit\": \"ns\",$", MR_Next},
  215. {"\"bar\": %float,$", MR_Next},
  216. {"\"foo\": %float$", MR_Next},
  217. {"}", MR_Next}});
  218. ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_AvgThreadsRate/"
  219. "threads:%int\",%csv_report,%float,%float$"}});
  220. // VS2013 does not allow this function to be passed as a lambda argument
  221. // to CHECK_BENCHMARK_RESULTS()
  222. void CheckAvgThreadsRate(Results const& e) {
  223. CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, 1. / e.DurationCPUTime(), 0.001);
  224. CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 2. / e.DurationCPUTime(), 0.001);
  225. }
  226. CHECK_BENCHMARK_RESULTS("BM_Counters_AvgThreadsRate/threads:%int",
  227. &CheckAvgThreadsRate);
  228. // ========================================================================= //
  229. // ------------------- IterationInvariant Counters Output ------------------ //
  230. // ========================================================================= //
  231. void BM_Counters_IterationInvariant(benchmark::State& state) {
  232. for (auto _ : state) {
  233. }
  234. namespace bm = benchmark;
  235. state.counters["foo"] = bm::Counter{1, bm::Counter::kIsIterationInvariant};
  236. state.counters["bar"] = bm::Counter{2, bm::Counter::kIsIterationInvariant};
  237. }
  238. BENCHMARK(BM_Counters_IterationInvariant);
  239. ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_IterationInvariant %console_report "
  240. "bar=%hrfloat foo=%hrfloat$"}});
  241. ADD_CASES(TC_JSONOut,
  242. {{"\"name\": \"BM_Counters_IterationInvariant\",$"},
  243. {"\"run_name\": \"BM_Counters_IterationInvariant\",$", MR_Next},
  244. {"\"run_type\": \"iteration\",$", MR_Next},
  245. {"\"iterations\": %int,$", MR_Next},
  246. {"\"real_time\": %float,$", MR_Next},
  247. {"\"cpu_time\": %float,$", MR_Next},
  248. {"\"time_unit\": \"ns\",$", MR_Next},
  249. {"\"bar\": %float,$", MR_Next},
  250. {"\"foo\": %float$", MR_Next},
  251. {"}", MR_Next}});
  252. ADD_CASES(TC_CSVOut,
  253. {{"^\"BM_Counters_IterationInvariant\",%csv_report,%float,%float$"}});
  254. // VS2013 does not allow this function to be passed as a lambda argument
  255. // to CHECK_BENCHMARK_RESULTS()
  256. void CheckIterationInvariant(Results const& e) {
  257. double its = e.NumIterations();
  258. // check that the values are within 0.1% of the expected value
  259. CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, its, 0.001);
  260. CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 2. * its, 0.001);
  261. }
  262. CHECK_BENCHMARK_RESULTS("BM_Counters_IterationInvariant",
  263. &CheckIterationInvariant);
  264. // ========================================================================= //
  265. // ----------------- IterationInvariantRate Counters Output ---------------- //
  266. // ========================================================================= //
  267. void BM_Counters_kIsIterationInvariantRate(benchmark::State& state) {
  268. for (auto _ : state) {
  269. }
  270. namespace bm = benchmark;
  271. state.counters["foo"] =
  272. bm::Counter{1, bm::Counter::kIsIterationInvariantRate};
  273. state.counters["bar"] =
  274. bm::Counter{2, bm::Counter::kIsRate | bm::Counter::kIsIterationInvariant};
  275. }
  276. BENCHMARK(BM_Counters_kIsIterationInvariantRate);
  277. ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_kIsIterationInvariantRate "
  278. "%console_report bar=%hrfloat/s foo=%hrfloat/s$"}});
  279. ADD_CASES(TC_JSONOut,
  280. {{"\"name\": \"BM_Counters_kIsIterationInvariantRate\",$"},
  281. {"\"run_name\": \"BM_Counters_kIsIterationInvariantRate\",$",
  282. MR_Next},
  283. {"\"run_type\": \"iteration\",$", MR_Next},
  284. {"\"iterations\": %int,$", MR_Next},
  285. {"\"real_time\": %float,$", MR_Next},
  286. {"\"cpu_time\": %float,$", MR_Next},
  287. {"\"time_unit\": \"ns\",$", MR_Next},
  288. {"\"bar\": %float,$", MR_Next},
  289. {"\"foo\": %float$", MR_Next},
  290. {"}", MR_Next}});
  291. ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_kIsIterationInvariantRate\",%csv_report,"
  292. "%float,%float$"}});
  293. // VS2013 does not allow this function to be passed as a lambda argument
  294. // to CHECK_BENCHMARK_RESULTS()
  295. void CheckIsIterationInvariantRate(Results const& e) {
  296. double its = e.NumIterations();
  297. double t = e.DurationCPUTime(); // this (and not real time) is the time used
  298. // check that the values are within 0.1% of the expected values
  299. CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, its * 1. / t, 0.001);
  300. CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, its * 2. / t, 0.001);
  301. }
  302. CHECK_BENCHMARK_RESULTS("BM_Counters_kIsIterationInvariantRate",
  303. &CheckIsIterationInvariantRate);
  304. // ========================================================================= //
  305. // ------------------- AvgIterations Counters Output ------------------ //
  306. // ========================================================================= //
  307. void BM_Counters_AvgIterations(benchmark::State& state) {
  308. for (auto _ : state) {
  309. }
  310. namespace bm = benchmark;
  311. state.counters["foo"] = bm::Counter{1, bm::Counter::kAvgIterations};
  312. state.counters["bar"] = bm::Counter{2, bm::Counter::kAvgIterations};
  313. }
  314. BENCHMARK(BM_Counters_AvgIterations);
  315. ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_AvgIterations %console_report "
  316. "bar=%hrfloat foo=%hrfloat$"}});
  317. ADD_CASES(TC_JSONOut,
  318. {{"\"name\": \"BM_Counters_AvgIterations\",$"},
  319. {"\"run_name\": \"BM_Counters_AvgIterations\",$", MR_Next},
  320. {"\"run_type\": \"iteration\",$", MR_Next},
  321. {"\"iterations\": %int,$", MR_Next},
  322. {"\"real_time\": %float,$", MR_Next},
  323. {"\"cpu_time\": %float,$", MR_Next},
  324. {"\"time_unit\": \"ns\",$", MR_Next},
  325. {"\"bar\": %float,$", MR_Next},
  326. {"\"foo\": %float$", MR_Next},
  327. {"}", MR_Next}});
  328. ADD_CASES(TC_CSVOut,
  329. {{"^\"BM_Counters_AvgIterations\",%csv_report,%float,%float$"}});
  330. // VS2013 does not allow this function to be passed as a lambda argument
  331. // to CHECK_BENCHMARK_RESULTS()
  332. void CheckAvgIterations(Results const& e) {
  333. double its = e.NumIterations();
  334. // check that the values are within 0.1% of the expected value
  335. CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, 1. / its, 0.001);
  336. CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 2. / its, 0.001);
  337. }
  338. CHECK_BENCHMARK_RESULTS("BM_Counters_AvgIterations", &CheckAvgIterations);
  339. // ========================================================================= //
  340. // ----------------- AvgIterationsRate Counters Output ---------------- //
  341. // ========================================================================= //
  342. void BM_Counters_kAvgIterationsRate(benchmark::State& state) {
  343. for (auto _ : state) {
  344. }
  345. namespace bm = benchmark;
  346. state.counters["foo"] = bm::Counter{1, bm::Counter::kAvgIterationsRate};
  347. state.counters["bar"] =
  348. bm::Counter{2, bm::Counter::kIsRate | bm::Counter::kAvgIterations};
  349. }
  350. BENCHMARK(BM_Counters_kAvgIterationsRate);
  351. ADD_CASES(TC_ConsoleOut, {{"^BM_Counters_kAvgIterationsRate "
  352. "%console_report bar=%hrfloat/s foo=%hrfloat/s$"}});
  353. ADD_CASES(TC_JSONOut,
  354. {{"\"name\": \"BM_Counters_kAvgIterationsRate\",$"},
  355. {"\"run_name\": \"BM_Counters_kAvgIterationsRate\",$", MR_Next},
  356. {"\"run_type\": \"iteration\",$", MR_Next},
  357. {"\"iterations\": %int,$", MR_Next},
  358. {"\"real_time\": %float,$", MR_Next},
  359. {"\"cpu_time\": %float,$", MR_Next},
  360. {"\"time_unit\": \"ns\",$", MR_Next},
  361. {"\"bar\": %float,$", MR_Next},
  362. {"\"foo\": %float$", MR_Next},
  363. {"}", MR_Next}});
  364. ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_kAvgIterationsRate\",%csv_report,"
  365. "%float,%float$"}});
  366. // VS2013 does not allow this function to be passed as a lambda argument
  367. // to CHECK_BENCHMARK_RESULTS()
  368. void CheckAvgIterationsRate(Results const& e) {
  369. double its = e.NumIterations();
  370. double t = e.DurationCPUTime(); // this (and not real time) is the time used
  371. // check that the values are within 0.1% of the expected values
  372. CHECK_FLOAT_COUNTER_VALUE(e, "foo", EQ, 1. / its / t, 0.001);
  373. CHECK_FLOAT_COUNTER_VALUE(e, "bar", EQ, 2. / its / t, 0.001);
  374. }
  375. CHECK_BENCHMARK_RESULTS("BM_Counters_kAvgIterationsRate",
  376. &CheckAvgIterationsRate);
  377. // ========================================================================= //
  378. // --------------------------- TEST CASES END ------------------------------ //
  379. // ========================================================================= //
  380. int main(int argc, char* argv[]) { RunOutputTests(argc, argv); }