user_counters_tabular_test.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #undef NDEBUG
  2. #include "benchmark/benchmark.h"
  3. #include "output_test.h"
  4. // @todo: <jpmag> this checks the full output at once; the rule for
  5. // CounterSet1 was failing because it was not matching "^[-]+$".
  6. // @todo: <jpmag> check that the counters are vertically aligned.
  7. ADD_CASES(
  8. TC_ConsoleOut,
  9. {
  10. // keeping these lines long improves readability, so:
  11. // clang-format off
  12. {"^[-]+$", MR_Next},
  13. {"^Benchmark %s Time %s CPU %s Iterations %s Bar %s Bat %s Baz %s Foo %s Frob %s Lob$", MR_Next},
  14. {"^[-]+$", MR_Next},
  15. {"^BM_Counters_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  16. {"^BM_Counters_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  17. {"^BM_Counters_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  18. {"^BM_Counters_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  19. {"^BM_Counters_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  20. {"^BM_CounterRates_Tabular/threads:%int %console_report [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s$", MR_Next},
  21. {"^BM_CounterRates_Tabular/threads:%int %console_report [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s$", MR_Next},
  22. {"^BM_CounterRates_Tabular/threads:%int %console_report [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s$", MR_Next},
  23. {"^BM_CounterRates_Tabular/threads:%int %console_report [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s$", MR_Next},
  24. {"^BM_CounterRates_Tabular/threads:%int %console_report [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s [ ]*%hrfloat/s$", MR_Next},
  25. {"^[-]+$", MR_Next},
  26. {"^Benchmark %s Time %s CPU %s Iterations %s Bar %s Baz %s Foo$", MR_Next},
  27. {"^[-]+$", MR_Next},
  28. {"^BM_CounterSet0_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  29. {"^BM_CounterSet0_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  30. {"^BM_CounterSet0_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  31. {"^BM_CounterSet0_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  32. {"^BM_CounterSet0_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  33. {"^BM_CounterSet1_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  34. {"^BM_CounterSet1_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  35. {"^BM_CounterSet1_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  36. {"^BM_CounterSet1_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  37. {"^BM_CounterSet1_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  38. {"^[-]+$", MR_Next},
  39. {"^Benchmark %s Time %s CPU %s Iterations %s Bat %s Baz %s Foo$", MR_Next},
  40. {"^[-]+$", MR_Next},
  41. {"^BM_CounterSet2_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  42. {"^BM_CounterSet2_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  43. {"^BM_CounterSet2_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  44. {"^BM_CounterSet2_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
  45. {"^BM_CounterSet2_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$"},
  46. // clang-format on
  47. });
  48. ADD_CASES(TC_CSVOut, {{"%csv_header,"
  49. "\"Bar\",\"Bat\",\"Baz\",\"Foo\",\"Frob\",\"Lob\""}});
  50. // ========================================================================= //
  51. // ------------------------- Tabular Counters Output ----------------------- //
  52. // ========================================================================= //
  53. void BM_Counters_Tabular(benchmark::State& state) {
  54. for (auto _ : state) {
  55. }
  56. namespace bm = benchmark;
  57. state.counters.insert({
  58. {"Foo", {1, bm::Counter::kAvgThreads}},
  59. {"Bar", {2, bm::Counter::kAvgThreads}},
  60. {"Baz", {4, bm::Counter::kAvgThreads}},
  61. {"Bat", {8, bm::Counter::kAvgThreads}},
  62. {"Frob", {16, bm::Counter::kAvgThreads}},
  63. {"Lob", {32, bm::Counter::kAvgThreads}},
  64. });
  65. }
  66. BENCHMARK(BM_Counters_Tabular)->ThreadRange(1, 16);
  67. ADD_CASES(TC_JSONOut,
  68. {{"\"name\": \"BM_Counters_Tabular/threads:%int\",$"},
  69. {"\"run_name\": \"BM_Counters_Tabular/threads:%int\",$", MR_Next},
  70. {"\"run_type\": \"iteration\",$", MR_Next},
  71. {"\"iterations\": %int,$", MR_Next},
  72. {"\"real_time\": %float,$", MR_Next},
  73. {"\"cpu_time\": %float,$", MR_Next},
  74. {"\"time_unit\": \"ns\",$", MR_Next},
  75. {"\"Bar\": %float,$", MR_Next},
  76. {"\"Bat\": %float,$", MR_Next},
  77. {"\"Baz\": %float,$", MR_Next},
  78. {"\"Foo\": %float,$", MR_Next},
  79. {"\"Frob\": %float,$", MR_Next},
  80. {"\"Lob\": %float$", MR_Next},
  81. {"}", MR_Next}});
  82. ADD_CASES(TC_CSVOut, {{"^\"BM_Counters_Tabular/threads:%int\",%csv_report,"
  83. "%float,%float,%float,%float,%float,%float$"}});
  84. // VS2013 does not allow this function to be passed as a lambda argument
  85. // to CHECK_BENCHMARK_RESULTS()
  86. void CheckTabular(Results const& e) {
  87. CHECK_COUNTER_VALUE(e, int, "Foo", EQ, 1);
  88. CHECK_COUNTER_VALUE(e, int, "Bar", EQ, 2);
  89. CHECK_COUNTER_VALUE(e, int, "Baz", EQ, 4);
  90. CHECK_COUNTER_VALUE(e, int, "Bat", EQ, 8);
  91. CHECK_COUNTER_VALUE(e, int, "Frob", EQ, 16);
  92. CHECK_COUNTER_VALUE(e, int, "Lob", EQ, 32);
  93. }
  94. CHECK_BENCHMARK_RESULTS("BM_Counters_Tabular/threads:%int", &CheckTabular);
  95. // ========================================================================= //
  96. // -------------------- Tabular+Rate Counters Output ----------------------- //
  97. // ========================================================================= //
  98. void BM_CounterRates_Tabular(benchmark::State& state) {
  99. for (auto _ : state) {
  100. }
  101. namespace bm = benchmark;
  102. state.counters.insert({
  103. {"Foo", {1, bm::Counter::kAvgThreadsRate}},
  104. {"Bar", {2, bm::Counter::kAvgThreadsRate}},
  105. {"Baz", {4, bm::Counter::kAvgThreadsRate}},
  106. {"Bat", {8, bm::Counter::kAvgThreadsRate}},
  107. {"Frob", {16, bm::Counter::kAvgThreadsRate}},
  108. {"Lob", {32, bm::Counter::kAvgThreadsRate}},
  109. });
  110. }
  111. BENCHMARK(BM_CounterRates_Tabular)->ThreadRange(1, 16);
  112. ADD_CASES(TC_JSONOut,
  113. {{"\"name\": \"BM_CounterRates_Tabular/threads:%int\",$"},
  114. {"\"run_name\": \"BM_CounterRates_Tabular/threads:%int\",$",
  115. MR_Next},
  116. {"\"run_type\": \"iteration\",$", MR_Next},
  117. {"\"iterations\": %int,$", MR_Next},
  118. {"\"real_time\": %float,$", MR_Next},
  119. {"\"cpu_time\": %float,$", MR_Next},
  120. {"\"time_unit\": \"ns\",$", MR_Next},
  121. {"\"Bar\": %float,$", MR_Next},
  122. {"\"Bat\": %float,$", MR_Next},
  123. {"\"Baz\": %float,$", MR_Next},
  124. {"\"Foo\": %float,$", MR_Next},
  125. {"\"Frob\": %float,$", MR_Next},
  126. {"\"Lob\": %float$", MR_Next},
  127. {"}", MR_Next}});
  128. ADD_CASES(TC_CSVOut, {{"^\"BM_CounterRates_Tabular/threads:%int\",%csv_report,"
  129. "%float,%float,%float,%float,%float,%float$"}});
  130. // VS2013 does not allow this function to be passed as a lambda argument
  131. // to CHECK_BENCHMARK_RESULTS()
  132. void CheckTabularRate(Results const& e) {
  133. double t = e.DurationCPUTime();
  134. CHECK_FLOAT_COUNTER_VALUE(e, "Foo", EQ, 1. / t, 0.001);
  135. CHECK_FLOAT_COUNTER_VALUE(e, "Bar", EQ, 2. / t, 0.001);
  136. CHECK_FLOAT_COUNTER_VALUE(e, "Baz", EQ, 4. / t, 0.001);
  137. CHECK_FLOAT_COUNTER_VALUE(e, "Bat", EQ, 8. / t, 0.001);
  138. CHECK_FLOAT_COUNTER_VALUE(e, "Frob", EQ, 16. / t, 0.001);
  139. CHECK_FLOAT_COUNTER_VALUE(e, "Lob", EQ, 32. / t, 0.001);
  140. }
  141. CHECK_BENCHMARK_RESULTS("BM_CounterRates_Tabular/threads:%int",
  142. &CheckTabularRate);
  143. // ========================================================================= //
  144. // ------------------------- Tabular Counters Output ----------------------- //
  145. // ========================================================================= //
  146. // set only some of the counters
  147. void BM_CounterSet0_Tabular(benchmark::State& state) {
  148. for (auto _ : state) {
  149. }
  150. namespace bm = benchmark;
  151. state.counters.insert({
  152. {"Foo", {10, bm::Counter::kAvgThreads}},
  153. {"Bar", {20, bm::Counter::kAvgThreads}},
  154. {"Baz", {40, bm::Counter::kAvgThreads}},
  155. });
  156. }
  157. BENCHMARK(BM_CounterSet0_Tabular)->ThreadRange(1, 16);
  158. ADD_CASES(TC_JSONOut,
  159. {{"\"name\": \"BM_CounterSet0_Tabular/threads:%int\",$"},
  160. {"\"run_name\": \"BM_CounterSet0_Tabular/threads:%int\",$", MR_Next},
  161. {"\"run_type\": \"iteration\",$", MR_Next},
  162. {"\"iterations\": %int,$", MR_Next},
  163. {"\"real_time\": %float,$", MR_Next},
  164. {"\"cpu_time\": %float,$", MR_Next},
  165. {"\"time_unit\": \"ns\",$", MR_Next},
  166. {"\"Bar\": %float,$", MR_Next},
  167. {"\"Baz\": %float,$", MR_Next},
  168. {"\"Foo\": %float$", MR_Next},
  169. {"}", MR_Next}});
  170. ADD_CASES(TC_CSVOut, {{"^\"BM_CounterSet0_Tabular/threads:%int\",%csv_report,"
  171. "%float,,%float,%float,,"}});
  172. // VS2013 does not allow this function to be passed as a lambda argument
  173. // to CHECK_BENCHMARK_RESULTS()
  174. void CheckSet0(Results const& e) {
  175. CHECK_COUNTER_VALUE(e, int, "Foo", EQ, 10);
  176. CHECK_COUNTER_VALUE(e, int, "Bar", EQ, 20);
  177. CHECK_COUNTER_VALUE(e, int, "Baz", EQ, 40);
  178. }
  179. CHECK_BENCHMARK_RESULTS("BM_CounterSet0_Tabular", &CheckSet0);
  180. // again.
  181. void BM_CounterSet1_Tabular(benchmark::State& state) {
  182. for (auto _ : state) {
  183. }
  184. namespace bm = benchmark;
  185. state.counters.insert({
  186. {"Foo", {15, bm::Counter::kAvgThreads}},
  187. {"Bar", {25, bm::Counter::kAvgThreads}},
  188. {"Baz", {45, bm::Counter::kAvgThreads}},
  189. });
  190. }
  191. BENCHMARK(BM_CounterSet1_Tabular)->ThreadRange(1, 16);
  192. ADD_CASES(TC_JSONOut,
  193. {{"\"name\": \"BM_CounterSet1_Tabular/threads:%int\",$"},
  194. {"\"run_name\": \"BM_CounterSet1_Tabular/threads:%int\",$", MR_Next},
  195. {"\"run_type\": \"iteration\",$", MR_Next},
  196. {"\"iterations\": %int,$", MR_Next},
  197. {"\"real_time\": %float,$", MR_Next},
  198. {"\"cpu_time\": %float,$", MR_Next},
  199. {"\"time_unit\": \"ns\",$", MR_Next},
  200. {"\"Bar\": %float,$", MR_Next},
  201. {"\"Baz\": %float,$", MR_Next},
  202. {"\"Foo\": %float$", MR_Next},
  203. {"}", MR_Next}});
  204. ADD_CASES(TC_CSVOut, {{"^\"BM_CounterSet1_Tabular/threads:%int\",%csv_report,"
  205. "%float,,%float,%float,,"}});
  206. // VS2013 does not allow this function to be passed as a lambda argument
  207. // to CHECK_BENCHMARK_RESULTS()
  208. void CheckSet1(Results const& e) {
  209. CHECK_COUNTER_VALUE(e, int, "Foo", EQ, 15);
  210. CHECK_COUNTER_VALUE(e, int, "Bar", EQ, 25);
  211. CHECK_COUNTER_VALUE(e, int, "Baz", EQ, 45);
  212. }
  213. CHECK_BENCHMARK_RESULTS("BM_CounterSet1_Tabular/threads:%int", &CheckSet1);
  214. // ========================================================================= //
  215. // ------------------------- Tabular Counters Output ----------------------- //
  216. // ========================================================================= //
  217. // set only some of the counters, different set now.
  218. void BM_CounterSet2_Tabular(benchmark::State& state) {
  219. for (auto _ : state) {
  220. }
  221. namespace bm = benchmark;
  222. state.counters.insert({
  223. {"Foo", {10, bm::Counter::kAvgThreads}},
  224. {"Bat", {30, bm::Counter::kAvgThreads}},
  225. {"Baz", {40, bm::Counter::kAvgThreads}},
  226. });
  227. }
  228. BENCHMARK(BM_CounterSet2_Tabular)->ThreadRange(1, 16);
  229. ADD_CASES(TC_JSONOut,
  230. {{"\"name\": \"BM_CounterSet2_Tabular/threads:%int\",$"},
  231. {"\"run_name\": \"BM_CounterSet2_Tabular/threads:%int\",$", MR_Next},
  232. {"\"run_type\": \"iteration\",$", MR_Next},
  233. {"\"iterations\": %int,$", MR_Next},
  234. {"\"real_time\": %float,$", MR_Next},
  235. {"\"cpu_time\": %float,$", MR_Next},
  236. {"\"time_unit\": \"ns\",$", MR_Next},
  237. {"\"Bat\": %float,$", MR_Next},
  238. {"\"Baz\": %float,$", MR_Next},
  239. {"\"Foo\": %float$", MR_Next},
  240. {"}", MR_Next}});
  241. ADD_CASES(TC_CSVOut, {{"^\"BM_CounterSet2_Tabular/threads:%int\",%csv_report,"
  242. ",%float,%float,%float,,"}});
  243. // VS2013 does not allow this function to be passed as a lambda argument
  244. // to CHECK_BENCHMARK_RESULTS()
  245. void CheckSet2(Results const& e) {
  246. CHECK_COUNTER_VALUE(e, int, "Foo", EQ, 10);
  247. CHECK_COUNTER_VALUE(e, int, "Bat", EQ, 30);
  248. CHECK_COUNTER_VALUE(e, int, "Baz", EQ, 40);
  249. }
  250. CHECK_BENCHMARK_RESULTS("BM_CounterSet2_Tabular", &CheckSet2);
  251. // ========================================================================= //
  252. // --------------------------- TEST CASES END ------------------------------ //
  253. // ========================================================================= //
  254. int main(int argc, char* argv[]) { RunOutputTests(argc, argv); }