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.

228 lines
8.5 KiB

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