• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // X20-BenchmarkingMacros.cpp
2 // Test that the benchmarking support macros compile properly with the single header
3 
4 #define CATCH_CONFIG_MAIN
5 #include <catch2/catch.hpp>
6 
7 namespace {
factorial(std::uint64_t number)8 std::uint64_t factorial(std::uint64_t number) {
9     if (number < 2) {
10         return 1;
11     }
12     return number * factorial(number - 1);
13 }
14 }
15 
16 TEST_CASE("Benchmark factorial", "[benchmark]") {
17     CHECK(factorial(0) == 1);
18     // some more asserts..
19     CHECK(factorial(10) == 3628800);
20 
21     BENCHMARK("factorial 10") {
22         return factorial(10);
23     };
24 
25     CHECK(factorial(14) == 87178291200ull);
26     BENCHMARK("factorial 14") {
27         return factorial(14);
28     };
29 }
30 
31 TEST_CASE("Benchmark containers", "[.][benchmark]") {
32     static const int size = 100;
33 
34     std::vector<int> v;
35     std::map<int, int> m;
36 
37     SECTION("without generator") {
38         BENCHMARK("Load up a vector") {
39             v = std::vector<int>();
40             for (int i = 0; i < size; ++i)
41                 v.push_back(i);
42         };
43         REQUIRE(v.size() == size);
44 
45         // test optimizer control
46         BENCHMARK("Add up a vector's content") {
47             uint64_t add = 0;
48             for (int i = 0; i < size; ++i)
49                 add += v[i];
50             return add;
51         };
52 
53         BENCHMARK("Load up a map") {
54             m = std::map<int, int>();
55             for (int i = 0; i < size; ++i)
56                 m.insert({ i, i + 1 });
57         };
58         REQUIRE(m.size() == size);
59 
60         BENCHMARK("Reserved vector") {
61             v = std::vector<int>();
62             v.reserve(size);
63             for (int i = 0; i < size; ++i)
64                 v.push_back(i);
65         };
66         REQUIRE(v.size() == size);
67 
68         BENCHMARK("Resized vector") {
69             v = std::vector<int>();
70             v.resize(size);
71             for (int i = 0; i < size; ++i)
72                 v[i] = i;
73         };
74         REQUIRE(v.size() == size);
75 
76         int array[size];
77         BENCHMARK("A fixed size array that should require no allocations") {
78             for (int i = 0; i < size; ++i)
79                 array[i] = i;
80         };
81         int sum = 0;
82         for (int i = 0; i < size; ++i)
83             sum += array[i];
84         REQUIRE(sum > size);
85 
86         SECTION("XYZ") {
87 
88             BENCHMARK_ADVANCED("Load up vector with chronometer")(Catch::Benchmark::Chronometer meter) {
89                 std::vector<int> k;
__anonf838a93f0202(int idx) 90                 meter.measure([&](int idx) {
91                     k = std::vector<int>();
92                     for (int i = 0; i < size; ++i)
93                         k.push_back(idx);
94                 });
95                 REQUIRE(k.size() == size);
96             };
97 
98             int runs = 0;
99             BENCHMARK("Fill vector indexed", benchmarkIndex) {
100                 v = std::vector<int>();
101                 v.resize(size);
102                 for (int i = 0; i < size; ++i)
103                     v[i] = benchmarkIndex;
104                 runs = benchmarkIndex;
105             };
106 
107             for (size_t i = 0; i < v.size(); ++i) {
108                 REQUIRE(v[i] == runs);
109             }
110         }
111     }
112 
113     SECTION("with generator") {
114         auto generated = GENERATE(range(0, 10));
115         BENCHMARK("Fill vector generated") {
116             v = std::vector<int>();
117             v.resize(size);
118             for (int i = 0; i < size; ++i)
119                 v[i] = generated;
120         };
121         for (size_t i = 0; i < v.size(); ++i) {
122             REQUIRE(v[i] == generated);
123         }
124     }
125 }
126