• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <cassert>
2 #include <iostream>
3 #include <set>
4 #include <vector>
5 
6 #include "benchmark/benchmark.h"
7 
8 class ArgsProductFixture : public ::benchmark::Fixture {
9  public:
ArgsProductFixture()10   ArgsProductFixture()
11       : expectedValues({{0, 100, 2000, 30000},
12                         {1, 15, 3, 8},
13                         {1, 15, 3, 9},
14                         {1, 15, 7, 8},
15                         {1, 15, 7, 9},
16                         {1, 15, 10, 8},
17                         {1, 15, 10, 9},
18                         {2, 15, 3, 8},
19                         {2, 15, 3, 9},
20                         {2, 15, 7, 8},
21                         {2, 15, 7, 9},
22                         {2, 15, 10, 8},
23                         {2, 15, 10, 9},
24                         {4, 5, 6, 11}}) {}
25 
SetUp(const::benchmark::State & state)26   void SetUp(const ::benchmark::State& state) BENCHMARK_OVERRIDE {
27     std::vector<int64_t> ranges = {state.range(0), state.range(1),
28                                    state.range(2), state.range(3)};
29 
30     assert(expectedValues.find(ranges) != expectedValues.end());
31 
32     actualValues.insert(ranges);
33   }
34 
35   // NOTE: This is not TearDown as we want to check after _all_ runs are
36   // complete.
~ArgsProductFixture()37   virtual ~ArgsProductFixture() {
38     if (actualValues != expectedValues) {
39       std::cout << "EXPECTED\n";
40       for (const auto& v : expectedValues) {
41         std::cout << "{";
42         for (int64_t iv : v) {
43           std::cout << iv << ", ";
44         }
45         std::cout << "}\n";
46       }
47       std::cout << "ACTUAL\n";
48       for (const auto& v : actualValues) {
49         std::cout << "{";
50         for (int64_t iv : v) {
51           std::cout << iv << ", ";
52         }
53         std::cout << "}\n";
54       }
55     }
56   }
57 
58   std::set<std::vector<int64_t>> expectedValues;
59   std::set<std::vector<int64_t>> actualValues;
60 };
61 
BENCHMARK_DEFINE_F(ArgsProductFixture,Empty)62 BENCHMARK_DEFINE_F(ArgsProductFixture, Empty)(benchmark::State& state) {
63   for (auto _ : state) {
64     int64_t product =
65         state.range(0) * state.range(1) * state.range(2) * state.range(3);
66     for (int64_t x = 0; x < product; x++) {
67       benchmark::DoNotOptimize(x);
68     }
69   }
70 }
71 
72 BENCHMARK_REGISTER_F(ArgsProductFixture, Empty)
73     ->Args({0, 100, 2000, 30000})
74     ->ArgsProduct({{1, 2}, {15}, {3, 7, 10}, {8, 9}})
75     ->Args({4, 5, 6, 11});
76 
77 BENCHMARK_MAIN();
78