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: |
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 | |
26 | void SetUp(const ::benchmark::State& state) override { |
27 | std::vector<int64_t> ranges = {state.range(pos: 0), state.range(pos: 1), |
28 | state.range(pos: 2), state.range(pos: 3)}; |
29 | |
30 | assert(expectedValues.find(ranges) != expectedValues.end()); |
31 | |
32 | actualValues.insert(x: ranges); |
33 | } |
34 | |
35 | // NOTE: This is not TearDown as we want to check after _all_ runs are |
36 | // complete. |
37 | ~ArgsProductFixture() override { |
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 | |
62 | BENCHMARK_DEFINE_F(ArgsProductFixture, Empty)(benchmark::State& state) { |
63 | for (auto _ : state) { |
64 | int64_t product = |
65 | state.range(pos: 0) * state.range(pos: 1) * state.range(pos: 2) * state.range(pos: 3); |
66 | for (int64_t x = 0; x < product; x++) { |
67 | benchmark::DoNotOptimize(value&: x); |
68 | } |
69 | } |
70 | } |
71 | |
72 | BENCHMARK_REGISTER_F(ArgsProductFixture, Empty) |
73 | ->Args(args: {0, 100, 2000, 30000}) |
74 | ->ArgsProduct(arglists: {{1, 2}, {15}, {3, 7, 10}, {8, 9}}) |
75 | ->Args(args: {4, 5, 6, 11}); |
76 | |
77 | BENCHMARK_MAIN(); |
78 | |