7 #include "benchmark/benchmark.h"
8 #include "output_test.h"
12 #define ADD_COMPLEXITY_CASES(...) \
13 int CONCAT(dummy, __LINE__) = AddComplexityTest(__VA_ARGS__)
15 int AddComplexityTest(std::string test_name
, std::string big_o_test_name
,
16 std::string rms_test_name
, std::string big_o
) {
17 SetSubstitutions({{"%name", test_name
},
18 {"%bigo_name", big_o_test_name
},
19 {"%rms_name", rms_test_name
},
20 {"%bigo_str", "[ ]* %float " + big_o
},
22 {"%rms", "[ ]*[0-9]+ %"}});
25 {{"^%bigo_name %bigo_str %bigo_str[ ]*$"},
26 {"^%bigo_name", MR_Not
}, // Assert we we didn't only matched a name.
27 {"^%rms_name %rms %rms[ ]*$", MR_Next
}});
28 AddCases(TC_JSONOut
, {{"\"name\": \"%bigo_name\",$"},
29 {"\"run_name\": \"%name\",$", MR_Next
},
30 {"\"run_type\": \"aggregate\",$", MR_Next
},
31 {"\"repetitions\": %int,$", MR_Next
},
32 {"\"threads\": 1,$", MR_Next
},
33 {"\"aggregate_name\": \"BigO\",$", MR_Next
},
34 {"\"cpu_coefficient\": %float,$", MR_Next
},
35 {"\"real_coefficient\": %float,$", MR_Next
},
36 {"\"big_o\": \"%bigo\",$", MR_Next
},
37 {"\"time_unit\": \"ns\"$", MR_Next
},
39 {"\"name\": \"%rms_name\",$"},
40 {"\"run_name\": \"%name\",$", MR_Next
},
41 {"\"run_type\": \"aggregate\",$", MR_Next
},
42 {"\"repetitions\": %int,$", MR_Next
},
43 {"\"threads\": 1,$", MR_Next
},
44 {"\"aggregate_name\": \"RMS\",$", MR_Next
},
45 {"\"rms\": %float$", MR_Next
},
47 AddCases(TC_CSVOut
, {{"^\"%bigo_name\",,%float,%float,%bigo,,,,,$"},
48 {"^\"%bigo_name\"", MR_Not
},
49 {"^\"%rms_name\",,%float,%float,,,,,,$", MR_Next
}});
55 // ========================================================================= //
56 // --------------------------- Testing BigO O(1) --------------------------- //
57 // ========================================================================= //
59 void BM_Complexity_O1(benchmark::State
& state
) {
60 for (auto _
: state
) {
61 for (int i
= 0; i
< 1024; ++i
) {
62 benchmark::DoNotOptimize(&i
);
65 state
.SetComplexityN(state
.range(0));
67 BENCHMARK(BM_Complexity_O1
)->Range(1, 1 << 18)->Complexity(benchmark::o1
);
68 BENCHMARK(BM_Complexity_O1
)->Range(1, 1 << 18)->Complexity();
69 BENCHMARK(BM_Complexity_O1
)
71 ->Complexity([](benchmark::IterationCount
) { return 1.0; });
73 const char *one_test_name
= "BM_Complexity_O1";
74 const char *big_o_1_test_name
= "BM_Complexity_O1_BigO";
75 const char *rms_o_1_test_name
= "BM_Complexity_O1_RMS";
76 const char *enum_big_o_1
= "\\([0-9]+\\)";
77 // FIXME: Tolerate both '(1)' and 'lgN' as output when the complexity is auto
79 // See https://github.com/google/benchmark/issues/272
80 const char *auto_big_o_1
= "(\\([0-9]+\\))|(lgN)";
81 const char *lambda_big_o_1
= "f\\(N\\)";
84 ADD_COMPLEXITY_CASES(one_test_name
, big_o_1_test_name
, rms_o_1_test_name
,
87 // Add auto enum tests
88 ADD_COMPLEXITY_CASES(one_test_name
, big_o_1_test_name
, rms_o_1_test_name
,
92 ADD_COMPLEXITY_CASES(one_test_name
, big_o_1_test_name
, rms_o_1_test_name
,
95 // ========================================================================= //
96 // --------------------------- Testing BigO O(N) --------------------------- //
97 // ========================================================================= //
99 std::vector
<int> ConstructRandomVector(int64_t size
) {
101 v
.reserve(static_cast<int>(size
));
102 for (int i
= 0; i
< size
; ++i
) {
103 v
.push_back(static_cast<int>(std::rand() % size
));
108 void BM_Complexity_O_N(benchmark::State
& state
) {
109 auto v
= ConstructRandomVector(state
.range(0));
110 // Test worst case scenario (item not in vector)
111 const int64_t item_not_in_vector
= state
.range(0) * 2;
112 for (auto _
: state
) {
113 benchmark::DoNotOptimize(std::find(v
.begin(), v
.end(), item_not_in_vector
));
115 state
.SetComplexityN(state
.range(0));
117 BENCHMARK(BM_Complexity_O_N
)
119 ->Range(1 << 10, 1 << 16)
120 ->Complexity(benchmark::oN
);
121 BENCHMARK(BM_Complexity_O_N
)
123 ->Range(1 << 10, 1 << 16)
124 ->Complexity([](benchmark::IterationCount n
) -> double {
125 return static_cast<double>(n
);
127 BENCHMARK(BM_Complexity_O_N
)
129 ->Range(1 << 10, 1 << 16)
132 const char *n_test_name
= "BM_Complexity_O_N";
133 const char *big_o_n_test_name
= "BM_Complexity_O_N_BigO";
134 const char *rms_o_n_test_name
= "BM_Complexity_O_N_RMS";
135 const char *enum_auto_big_o_n
= "N";
136 const char *lambda_big_o_n
= "f\\(N\\)";
139 ADD_COMPLEXITY_CASES(n_test_name
, big_o_n_test_name
, rms_o_n_test_name
,
143 ADD_COMPLEXITY_CASES(n_test_name
, big_o_n_test_name
, rms_o_n_test_name
,
146 // ========================================================================= //
147 // ------------------------- Testing BigO O(N*lgN) ------------------------- //
148 // ========================================================================= //
150 static void BM_Complexity_O_N_log_N(benchmark::State
& state
) {
151 auto v
= ConstructRandomVector(state
.range(0));
152 for (auto _
: state
) {
153 std::sort(v
.begin(), v
.end());
155 state
.SetComplexityN(state
.range(0));
157 static const double kLog2E
= 1.44269504088896340736;
158 BENCHMARK(BM_Complexity_O_N_log_N
)
160 ->Range(1 << 10, 1 << 16)
161 ->Complexity(benchmark::oNLogN
);
162 BENCHMARK(BM_Complexity_O_N_log_N
)
164 ->Range(1 << 10, 1 << 16)
165 ->Complexity([](benchmark::IterationCount n
) {
166 return kLog2E
* n
* log(static_cast<double>(n
));
168 BENCHMARK(BM_Complexity_O_N_log_N
)
170 ->Range(1 << 10, 1 << 16)
173 const char *n_lg_n_test_name
= "BM_Complexity_O_N_log_N";
174 const char *big_o_n_lg_n_test_name
= "BM_Complexity_O_N_log_N_BigO";
175 const char *rms_o_n_lg_n_test_name
= "BM_Complexity_O_N_log_N_RMS";
176 const char *enum_auto_big_o_n_lg_n
= "NlgN";
177 const char *lambda_big_o_n_lg_n
= "f\\(N\\)";
180 ADD_COMPLEXITY_CASES(n_lg_n_test_name
, big_o_n_lg_n_test_name
,
181 rms_o_n_lg_n_test_name
, enum_auto_big_o_n_lg_n
);
184 ADD_COMPLEXITY_CASES(n_lg_n_test_name
, big_o_n_lg_n_test_name
,
185 rms_o_n_lg_n_test_name
, lambda_big_o_n_lg_n
);
187 // ========================================================================= //
188 // -------- Testing formatting of Complexity with captured args ------------ //
189 // ========================================================================= //
191 void BM_ComplexityCaptureArgs(benchmark::State
& state
, int n
) {
192 for (auto _
: state
) {
193 // This test requires a non-zero CPU time to avoid divide-by-zero
194 benchmark::DoNotOptimize(state
.iterations());
196 state
.SetComplexityN(n
);
199 BENCHMARK_CAPTURE(BM_ComplexityCaptureArgs
, capture_test
, 100)
200 ->Complexity(benchmark::oN
)
201 ->Ranges({{1, 2}, {3, 4}});
203 const std::string complexity_capture_name
=
204 "BM_ComplexityCaptureArgs/capture_test";
206 ADD_COMPLEXITY_CASES(complexity_capture_name
, complexity_capture_name
+ "_BigO",
207 complexity_capture_name
+ "_RMS", "N");
209 // ========================================================================= //
210 // --------------------------- TEST CASES END ------------------------------ //
211 // ========================================================================= //
213 int main(int argc
, char *argv
[]) { RunOutputTests(argc
, argv
); }