Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 1 | |
| 2 | #undef NDEBUG |
| 3 | #include "benchmark/benchmark.h" |
| 4 | #include "../src/check.h" // NOTE: check.h is for internal use only! |
| 5 | #include "../src/re.h" // NOTE: re.h is for internal use only |
| 6 | #include <cassert> |
| 7 | #include <cstring> |
| 8 | #include <iostream> |
| 9 | #include <sstream> |
| 10 | #include <vector> |
| 11 | #include <utility> |
| 12 | #include <algorithm> |
| 13 | #include <cmath> |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | // ========================================================================= // |
| 18 | // -------------------------- Testing Case --------------------------------- // |
| 19 | // ========================================================================= // |
| 20 | |
| 21 | enum MatchRules { |
| 22 | MR_Default, // Skip non-matching lines until a match is found. |
| 23 | MR_Next // Match must occur on the next line. |
| 24 | }; |
| 25 | |
| 26 | struct TestCase { |
| 27 | std::string regex; |
| 28 | int match_rule; |
| 29 | |
| 30 | TestCase(std::string re, int rule = MR_Default) : regex(re), match_rule(rule) {} |
| 31 | |
| 32 | void Check(std::stringstream& remaining_output) const { |
| 33 | benchmark::Regex r; |
| 34 | std::string err_str; |
| 35 | r.Init(regex, &err_str); |
| 36 | CHECK(err_str.empty()) << "Could not construct regex \"" << regex << "\"" |
| 37 | << " got Error: " << err_str; |
| 38 | |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 39 | std::string near = "<EOF>"; |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 40 | std::string line; |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 41 | bool first = true; |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 42 | while (remaining_output.eof() == false) { |
| 43 | CHECK(remaining_output.good()); |
| 44 | std::getline(remaining_output, line); |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 45 | // Keep the first line as context. |
| 46 | if (first) { |
| 47 | near = line; |
| 48 | first = false; |
| 49 | } |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 50 | if (r.Match(line)) return; |
| 51 | CHECK(match_rule != MR_Next) << "Expected line \"" << line |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 52 | << "\" to match regex \"" << regex << "\"" |
| 53 | << "\nstarted matching at line: \"" << near << "\""; |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | CHECK(remaining_output.eof() == false) |
| 57 | << "End of output reached before match for regex \"" << regex |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 58 | << "\" was found" |
| 59 | << "\nstarted matching at line: \"" << near << "\""; |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 60 | } |
| 61 | }; |
| 62 | |
| 63 | std::vector<TestCase> ConsoleOutputTests; |
| 64 | std::vector<TestCase> JSONOutputTests; |
| 65 | std::vector<TestCase> CSVOutputTests; |
| 66 | |
| 67 | // ========================================================================= // |
| 68 | // -------------------------- Test Helpers --------------------------------- // |
| 69 | // ========================================================================= // |
| 70 | |
| 71 | class TestReporter : public benchmark::BenchmarkReporter { |
| 72 | public: |
| 73 | TestReporter(std::vector<benchmark::BenchmarkReporter*> reps) |
| 74 | : reporters_(reps) {} |
| 75 | |
| 76 | virtual bool ReportContext(const Context& context) { |
| 77 | bool last_ret = false; |
| 78 | bool first = true; |
| 79 | for (auto rep : reporters_) { |
| 80 | bool new_ret = rep->ReportContext(context); |
| 81 | CHECK(first || new_ret == last_ret) |
| 82 | << "Reports return different values for ReportContext"; |
| 83 | first = false; |
| 84 | last_ret = new_ret; |
| 85 | } |
| 86 | return last_ret; |
| 87 | } |
| 88 | |
| 89 | virtual void ReportRuns(const std::vector<Run>& report) { |
| 90 | for (auto rep : reporters_) |
| 91 | rep->ReportRuns(report); |
| 92 | } |
| 93 | |
| 94 | virtual void Finalize() { |
| 95 | for (auto rep : reporters_) |
| 96 | rep->Finalize(); |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | std::vector<benchmark::BenchmarkReporter*> reporters_; |
| 101 | }; |
| 102 | |
| 103 | |
| 104 | #define CONCAT2(x, y) x##y |
| 105 | #define CONCAT(x, y) CONCAT2(x, y) |
| 106 | |
| 107 | #define ADD_CASES(...) \ |
| 108 | int CONCAT(dummy, __LINE__) = AddCases(__VA_ARGS__) |
| 109 | |
| 110 | int AddCases(std::vector<TestCase>* out, std::initializer_list<TestCase> const& v) { |
| 111 | for (auto const& TC : v) |
| 112 | out->push_back(TC); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | template <class First> |
| 117 | std::string join(First f) { return f; } |
| 118 | |
| 119 | template <class First, class ...Args> |
| 120 | std::string join(First f, Args&&... args) { |
| 121 | return std::string(std::move(f)) + "[ ]+" + join(std::forward<Args>(args)...); |
| 122 | } |
| 123 | |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 124 | std::string dec_re = "[0-9]*[.]?[0-9]+([eE][-+][0-9]+)?"; |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 125 | |
| 126 | #define ADD_COMPLEXITY_CASES(...) \ |
| 127 | int CONCAT(dummy, __LINE__) = AddComplexityTest(__VA_ARGS__) |
| 128 | |
| 129 | int AddComplexityTest(std::vector<TestCase>* console_out, std::vector<TestCase>* json_out, |
| 130 | std::vector<TestCase>* csv_out, std::string big_o_test_name, |
| 131 | std::string rms_test_name, std::string big_o) { |
| 132 | std::string big_o_str = dec_re + " " + big_o; |
| 133 | AddCases(console_out, { |
| 134 | {join("^" + big_o_test_name + "", big_o_str, big_o_str) + "[ ]*$"}, |
| 135 | {join("^" + rms_test_name + "", "[0-9]+ %", "[0-9]+ %") + "[ ]*$"} |
| 136 | }); |
| 137 | AddCases(json_out, { |
| 138 | {"\"name\": \"" + big_o_test_name + "\",$"}, |
| 139 | {"\"cpu_coefficient\": [0-9]+,$", MR_Next}, |
| 140 | {"\"real_coefficient\": [0-9]{1,5},$", MR_Next}, |
| 141 | {"\"big_o\": \"" + big_o + "\",$", MR_Next}, |
| 142 | {"\"time_unit\": \"ns\"$", MR_Next}, |
| 143 | {"}", MR_Next}, |
| 144 | {"\"name\": \"" + rms_test_name + "\",$"}, |
| 145 | {"\"rms\": [0-9]+%$", MR_Next}, |
| 146 | {"}", MR_Next} |
| 147 | }); |
| 148 | AddCases(csv_out, { |
| 149 | {"^\"" + big_o_test_name + "\",," + dec_re + "," + dec_re + "," + big_o + ",,,,,$"}, |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 150 | {"^\"" + rms_test_name + "\",," + dec_re + "," + dec_re + ",,,,,,$", MR_Next} |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 151 | }); |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | } // end namespace |
| 156 | |
| 157 | // ========================================================================= // |
| 158 | // --------------------------- Testing BigO O(1) --------------------------- // |
| 159 | // ========================================================================= // |
| 160 | |
| 161 | void BM_Complexity_O1(benchmark::State& state) { |
| 162 | while (state.KeepRunning()) { |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 163 | for (int i=0; i < 1024; ++i) { |
| 164 | benchmark::DoNotOptimize(&i); |
| 165 | } |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 166 | } |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 167 | state.SetComplexityN(state.range(0)); |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 168 | } |
| 169 | BENCHMARK(BM_Complexity_O1) -> Range(1, 1<<18) -> Complexity(benchmark::o1); |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 170 | BENCHMARK(BM_Complexity_O1) -> Range(1, 1<<18) -> Complexity(); |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 171 | BENCHMARK(BM_Complexity_O1) -> Range(1, 1<<18) -> Complexity([](int){return 1.0; }); |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 172 | |
| 173 | const char* big_o_1_test_name = "BM_Complexity_O1_BigO"; |
| 174 | const char* rms_o_1_test_name = "BM_Complexity_O1_RMS"; |
| 175 | const char* enum_auto_big_o_1 = "\\([0-9]+\\)"; |
| 176 | const char* lambda_big_o_1 = "f\\(N\\)"; |
| 177 | |
| 178 | // Add enum tests |
| 179 | ADD_COMPLEXITY_CASES(&ConsoleOutputTests, &JSONOutputTests, &CSVOutputTests, |
| 180 | big_o_1_test_name, rms_o_1_test_name, enum_auto_big_o_1); |
| 181 | |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 182 | // Add auto enum tests |
| 183 | ADD_COMPLEXITY_CASES(&ConsoleOutputTests, &JSONOutputTests, &CSVOutputTests, |
| 184 | big_o_1_test_name, rms_o_1_test_name, enum_auto_big_o_1); |
| 185 | |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 186 | // Add lambda tests |
| 187 | ADD_COMPLEXITY_CASES(&ConsoleOutputTests, &JSONOutputTests, &CSVOutputTests, |
| 188 | big_o_1_test_name, rms_o_1_test_name, lambda_big_o_1); |
| 189 | |
| 190 | // ========================================================================= // |
| 191 | // --------------------------- Testing BigO O(N) --------------------------- // |
| 192 | // ========================================================================= // |
| 193 | |
| 194 | std::vector<int> ConstructRandomVector(int size) { |
| 195 | std::vector<int> v; |
| 196 | v.reserve(size); |
| 197 | for (int i = 0; i < size; ++i) { |
| 198 | v.push_back(rand() % size); |
| 199 | } |
| 200 | return v; |
| 201 | } |
| 202 | |
| 203 | void BM_Complexity_O_N(benchmark::State& state) { |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 204 | auto v = ConstructRandomVector(state.range(0)); |
| 205 | const int item_not_in_vector = state.range(0)*2; // Test worst case scenario (item not in vector) |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 206 | while (state.KeepRunning()) { |
| 207 | benchmark::DoNotOptimize(std::find(v.begin(), v.end(), item_not_in_vector)); |
| 208 | } |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 209 | state.SetComplexityN(state.range(0)); |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 210 | } |
| 211 | BENCHMARK(BM_Complexity_O_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity(benchmark::oN); |
| 212 | BENCHMARK(BM_Complexity_O_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity([](int n) -> double{return n; }); |
| 213 | BENCHMARK(BM_Complexity_O_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity(); |
| 214 | |
| 215 | const char* big_o_n_test_name = "BM_Complexity_O_N_BigO"; |
| 216 | const char* rms_o_n_test_name = "BM_Complexity_O_N_RMS"; |
| 217 | const char* enum_auto_big_o_n = "N"; |
| 218 | const char* lambda_big_o_n = "f\\(N\\)"; |
| 219 | |
| 220 | // Add enum tests |
| 221 | ADD_COMPLEXITY_CASES(&ConsoleOutputTests, &JSONOutputTests, &CSVOutputTests, |
| 222 | big_o_n_test_name, rms_o_n_test_name, enum_auto_big_o_n); |
| 223 | |
| 224 | // Add lambda tests |
| 225 | ADD_COMPLEXITY_CASES(&ConsoleOutputTests, &JSONOutputTests, &CSVOutputTests, |
| 226 | big_o_n_test_name, rms_o_n_test_name, lambda_big_o_n); |
| 227 | |
| 228 | // ========================================================================= // |
| 229 | // ------------------------- Testing BigO O(N*lgN) ------------------------- // |
| 230 | // ========================================================================= // |
| 231 | |
| 232 | static void BM_Complexity_O_N_log_N(benchmark::State& state) { |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 233 | auto v = ConstructRandomVector(state.range(0)); |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 234 | while (state.KeepRunning()) { |
| 235 | std::sort(v.begin(), v.end()); |
| 236 | } |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 237 | state.SetComplexityN(state.range(0)); |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 238 | } |
| 239 | BENCHMARK(BM_Complexity_O_N_log_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity(benchmark::oNLogN); |
| 240 | BENCHMARK(BM_Complexity_O_N_log_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity([](int n) {return n * std::log2(n); }); |
| 241 | BENCHMARK(BM_Complexity_O_N_log_N) -> RangeMultiplier(2) -> Range(1<<10, 1<<16) -> Complexity(); |
| 242 | |
| 243 | const char* big_o_n_lg_n_test_name = "BM_Complexity_O_N_log_N_BigO"; |
| 244 | const char* rms_o_n_lg_n_test_name = "BM_Complexity_O_N_log_N_RMS"; |
| 245 | const char* enum_auto_big_o_n_lg_n = "NlgN"; |
| 246 | const char* lambda_big_o_n_lg_n = "f\\(N\\)"; |
| 247 | |
| 248 | // Add enum tests |
| 249 | ADD_COMPLEXITY_CASES(&ConsoleOutputTests, &JSONOutputTests, &CSVOutputTests, |
| 250 | big_o_n_lg_n_test_name, rms_o_n_lg_n_test_name, enum_auto_big_o_n_lg_n); |
| 251 | |
| 252 | // Add lambda tests |
| 253 | ADD_COMPLEXITY_CASES(&ConsoleOutputTests, &JSONOutputTests, &CSVOutputTests, |
| 254 | big_o_n_lg_n_test_name, rms_o_n_lg_n_test_name, lambda_big_o_n_lg_n); |
| 255 | |
| 256 | |
| 257 | // ========================================================================= // |
| 258 | // --------------------------- TEST CASES END ------------------------------ // |
| 259 | // ========================================================================= // |
| 260 | |
| 261 | |
| 262 | int main(int argc, char* argv[]) { |
Eric Fiselier | f6e09e5 | 2016-08-09 18:56:48 +0000 | [diff] [blame^] | 263 | benchmark::Initialize(&argc, argv); |
| 264 | benchmark::ConsoleReporter CR(benchmark::ConsoleReporter::OO_None); |
Eric Fiselier | b08d8b1 | 2016-07-19 23:07:03 +0000 | [diff] [blame] | 265 | benchmark::JSONReporter JR; |
| 266 | benchmark::CSVReporter CSVR; |
| 267 | struct ReporterTest { |
| 268 | const char* name; |
| 269 | std::vector<TestCase>& output_cases; |
| 270 | benchmark::BenchmarkReporter& reporter; |
| 271 | std::stringstream out_stream; |
| 272 | std::stringstream err_stream; |
| 273 | |
| 274 | ReporterTest(const char* n, |
| 275 | std::vector<TestCase>& out_tc, |
| 276 | benchmark::BenchmarkReporter& br) |
| 277 | : name(n), output_cases(out_tc), reporter(br) { |
| 278 | reporter.SetOutputStream(&out_stream); |
| 279 | reporter.SetErrorStream(&err_stream); |
| 280 | } |
| 281 | } TestCases[] = { |
| 282 | {"ConsoleReporter", ConsoleOutputTests, CR}, |
| 283 | {"JSONReporter", JSONOutputTests, JR}, |
| 284 | {"CSVReporter", CSVOutputTests, CSVR} |
| 285 | }; |
| 286 | |
| 287 | // Create the test reporter and run the benchmarks. |
| 288 | std::cout << "Running benchmarks...\n"; |
| 289 | TestReporter test_rep({&CR, &JR, &CSVR}); |
| 290 | benchmark::RunSpecifiedBenchmarks(&test_rep); |
| 291 | |
| 292 | for (auto& rep_test : TestCases) { |
| 293 | std::string msg = std::string("\nTesting ") + rep_test.name + " Output\n"; |
| 294 | std::string banner(msg.size() - 1, '-'); |
| 295 | std::cout << banner << msg << banner << "\n"; |
| 296 | |
| 297 | std::cerr << rep_test.err_stream.str(); |
| 298 | std::cout << rep_test.out_stream.str(); |
| 299 | |
| 300 | for (const auto& TC : rep_test.output_cases) |
| 301 | TC.Check(rep_test.out_stream); |
| 302 | |
| 303 | std::cout << "\n"; |
| 304 | } |
| 305 | return 0; |
| 306 | } |
| 307 | |