complexity_test.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #undef NDEBUG
  2. #include <algorithm>
  3. #include <cassert>
  4. #include <cmath>
  5. #include <cstdlib>
  6. #include <vector>
  7. #include "benchmark/benchmark.h"
  8. #include "output_test.h"
  9. namespace {
  10. #define ADD_COMPLEXITY_CASES(...) \
  11. int CONCAT(dummy, __LINE__) = AddComplexityTest(__VA_ARGS__)
  12. int AddComplexityTest(std::string test_name, std::string big_o_test_name,
  13. std::string rms_test_name, std::string big_o,
  14. int family_index) {
  15. SetSubstitutions({{"%name", test_name},
  16. {"%bigo_name", big_o_test_name},
  17. {"%rms_name", rms_test_name},
  18. {"%bigo_str", "[ ]* %float " + big_o},
  19. {"%bigo", big_o},
  20. {"%rms", "[ ]*[0-9]+ %"}});
  21. AddCases(
  22. TC_ConsoleOut,
  23. {{"^%bigo_name %bigo_str %bigo_str[ ]*$"},
  24. {"^%bigo_name", MR_Not}, // Assert we we didn't only matched a name.
  25. {"^%rms_name %rms %rms[ ]*$", MR_Next}});
  26. AddCases(
  27. TC_JSONOut,
  28. {{"\"name\": \"%bigo_name\",$"},
  29. {"\"family_index\": " + std::to_string(family_index) + ",$", MR_Next},
  30. {"\"per_family_instance_index\": 0,$", MR_Next},
  31. {"\"run_name\": \"%name\",$", MR_Next},
  32. {"\"run_type\": \"aggregate\",$", MR_Next},
  33. {"\"repetitions\": %int,$", MR_Next},
  34. {"\"threads\": 1,$", MR_Next},
  35. {"\"aggregate_name\": \"BigO\",$", MR_Next},
  36. {"\"aggregate_unit\": \"time\",$", MR_Next},
  37. {"\"cpu_coefficient\": %float,$", MR_Next},
  38. {"\"real_coefficient\": %float,$", MR_Next},
  39. {"\"big_o\": \"%bigo\",$", MR_Next},
  40. {"\"time_unit\": \"ns\"$", MR_Next},
  41. {"}", MR_Next},
  42. {"\"name\": \"%rms_name\",$"},
  43. {"\"family_index\": " + std::to_string(family_index) + ",$", MR_Next},
  44. {"\"per_family_instance_index\": 0,$", MR_Next},
  45. {"\"run_name\": \"%name\",$", MR_Next},
  46. {"\"run_type\": \"aggregate\",$", MR_Next},
  47. {"\"repetitions\": %int,$", MR_Next},
  48. {"\"threads\": 1,$", MR_Next},
  49. {"\"aggregate_name\": \"RMS\",$", MR_Next},
  50. {"\"aggregate_unit\": \"percentage\",$", MR_Next},
  51. {"\"rms\": %float$", MR_Next},
  52. {"}", MR_Next}});
  53. AddCases(TC_CSVOut, {{"^\"%bigo_name\",,%float,%float,%bigo,,,,,$"},
  54. {"^\"%bigo_name\"", MR_Not},
  55. {"^\"%rms_name\",,%float,%float,,,,,,$", MR_Next}});
  56. return 0;
  57. }
  58. } // end namespace
  59. // ========================================================================= //
  60. // --------------------------- Testing BigO O(1) --------------------------- //
  61. // ========================================================================= //
  62. void BM_Complexity_O1(benchmark::State& state) {
  63. for (auto _ : state) {
  64. for (int i = 0; i < 1024; ++i) {
  65. benchmark::DoNotOptimize(&i);
  66. }
  67. }
  68. state.SetComplexityN(state.range(0));
  69. }
  70. BENCHMARK(BM_Complexity_O1)->Range(1, 1 << 18)->Complexity(benchmark::o1);
  71. BENCHMARK(BM_Complexity_O1)->Range(1, 1 << 18)->Complexity();
  72. BENCHMARK(BM_Complexity_O1)
  73. ->Range(1, 1 << 18)
  74. ->Complexity([](benchmark::IterationCount) { return 1.0; });
  75. const char *one_test_name = "BM_Complexity_O1";
  76. const char *big_o_1_test_name = "BM_Complexity_O1_BigO";
  77. const char *rms_o_1_test_name = "BM_Complexity_O1_RMS";
  78. const char *enum_big_o_1 = "\\([0-9]+\\)";
  79. // FIXME: Tolerate both '(1)' and 'lgN' as output when the complexity is auto
  80. // deduced.
  81. // See https://github.com/google/benchmark/issues/272
  82. const char *auto_big_o_1 = "(\\([0-9]+\\))|(lgN)";
  83. const char *lambda_big_o_1 = "f\\(N\\)";
  84. // Add enum tests
  85. ADD_COMPLEXITY_CASES(one_test_name, big_o_1_test_name, rms_o_1_test_name,
  86. enum_big_o_1, /*family_index=*/0);
  87. // Add auto enum tests
  88. ADD_COMPLEXITY_CASES(one_test_name, big_o_1_test_name, rms_o_1_test_name,
  89. auto_big_o_1, /*family_index=*/1);
  90. // Add lambda tests
  91. ADD_COMPLEXITY_CASES(one_test_name, big_o_1_test_name, rms_o_1_test_name,
  92. lambda_big_o_1, /*family_index=*/2);
  93. // ========================================================================= //
  94. // --------------------------- Testing BigO O(N) --------------------------- //
  95. // ========================================================================= //
  96. std::vector<int> ConstructRandomVector(int64_t size) {
  97. std::vector<int> v;
  98. v.reserve(static_cast<int>(size));
  99. for (int i = 0; i < size; ++i) {
  100. v.push_back(static_cast<int>(std::rand() % size));
  101. }
  102. return v;
  103. }
  104. void BM_Complexity_O_N(benchmark::State& state) {
  105. auto v = ConstructRandomVector(state.range(0));
  106. // Test worst case scenario (item not in vector)
  107. const int64_t item_not_in_vector = state.range(0) * 2;
  108. for (auto _ : state) {
  109. benchmark::DoNotOptimize(std::find(v.begin(), v.end(), item_not_in_vector));
  110. }
  111. state.SetComplexityN(state.range(0));
  112. }
  113. BENCHMARK(BM_Complexity_O_N)
  114. ->RangeMultiplier(2)
  115. ->Range(1 << 10, 1 << 16)
  116. ->Complexity(benchmark::oN);
  117. BENCHMARK(BM_Complexity_O_N)
  118. ->RangeMultiplier(2)
  119. ->Range(1 << 10, 1 << 16)
  120. ->Complexity([](benchmark::IterationCount n) -> double {
  121. return static_cast<double>(n);
  122. });
  123. BENCHMARK(BM_Complexity_O_N)
  124. ->RangeMultiplier(2)
  125. ->Range(1 << 10, 1 << 16)
  126. ->Complexity();
  127. const char *n_test_name = "BM_Complexity_O_N";
  128. const char *big_o_n_test_name = "BM_Complexity_O_N_BigO";
  129. const char *rms_o_n_test_name = "BM_Complexity_O_N_RMS";
  130. const char *enum_auto_big_o_n = "N";
  131. const char *lambda_big_o_n = "f\\(N\\)";
  132. // Add enum tests
  133. ADD_COMPLEXITY_CASES(n_test_name, big_o_n_test_name, rms_o_n_test_name,
  134. enum_auto_big_o_n, /*family_index=*/3);
  135. // Add lambda tests
  136. ADD_COMPLEXITY_CASES(n_test_name, big_o_n_test_name, rms_o_n_test_name,
  137. lambda_big_o_n, /*family_index=*/4);
  138. // ========================================================================= //
  139. // ------------------------- Testing BigO O(N*lgN) ------------------------- //
  140. // ========================================================================= //
  141. static void BM_Complexity_O_N_log_N(benchmark::State& state) {
  142. auto v = ConstructRandomVector(state.range(0));
  143. for (auto _ : state) {
  144. std::sort(v.begin(), v.end());
  145. }
  146. state.SetComplexityN(state.range(0));
  147. }
  148. static const double kLog2E = 1.44269504088896340736;
  149. BENCHMARK(BM_Complexity_O_N_log_N)
  150. ->RangeMultiplier(2)
  151. ->Range(1 << 10, 1 << 16)
  152. ->Complexity(benchmark::oNLogN);
  153. BENCHMARK(BM_Complexity_O_N_log_N)
  154. ->RangeMultiplier(2)
  155. ->Range(1 << 10, 1 << 16)
  156. ->Complexity([](benchmark::IterationCount n) {
  157. return kLog2E * n * log(static_cast<double>(n));
  158. });
  159. BENCHMARK(BM_Complexity_O_N_log_N)
  160. ->RangeMultiplier(2)
  161. ->Range(1 << 10, 1 << 16)
  162. ->Complexity();
  163. const char *n_lg_n_test_name = "BM_Complexity_O_N_log_N";
  164. const char *big_o_n_lg_n_test_name = "BM_Complexity_O_N_log_N_BigO";
  165. const char *rms_o_n_lg_n_test_name = "BM_Complexity_O_N_log_N_RMS";
  166. const char *enum_auto_big_o_n_lg_n = "NlgN";
  167. const char *lambda_big_o_n_lg_n = "f\\(N\\)";
  168. // Add enum tests
  169. ADD_COMPLEXITY_CASES(n_lg_n_test_name, big_o_n_lg_n_test_name,
  170. rms_o_n_lg_n_test_name, enum_auto_big_o_n_lg_n,
  171. /*family_index=*/6);
  172. // Add lambda tests
  173. ADD_COMPLEXITY_CASES(n_lg_n_test_name, big_o_n_lg_n_test_name,
  174. rms_o_n_lg_n_test_name, lambda_big_o_n_lg_n,
  175. /*family_index=*/7);
  176. // ========================================================================= //
  177. // -------- Testing formatting of Complexity with captured args ------------ //
  178. // ========================================================================= //
  179. void BM_ComplexityCaptureArgs(benchmark::State& state, int n) {
  180. for (auto _ : state) {
  181. // This test requires a non-zero CPU time to avoid divide-by-zero
  182. benchmark::DoNotOptimize(state.iterations());
  183. }
  184. state.SetComplexityN(n);
  185. }
  186. BENCHMARK_CAPTURE(BM_ComplexityCaptureArgs, capture_test, 100)
  187. ->Complexity(benchmark::oN)
  188. ->Ranges({{1, 2}, {3, 4}});
  189. const std::string complexity_capture_name =
  190. "BM_ComplexityCaptureArgs/capture_test";
  191. ADD_COMPLEXITY_CASES(complexity_capture_name, complexity_capture_name + "_BigO",
  192. complexity_capture_name + "_RMS", "N", /*family_index=*/9);
  193. // ========================================================================= //
  194. // --------------------------- TEST CASES END ------------------------------ //
  195. // ========================================================================= //
  196. int main(int argc, char *argv[]) { RunOutputTests(argc, argv); }