Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Benchmark.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2021 Reactive Markets Limited
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#include "Benchmark.hpp"
17
18#include <toolbox/bm/Suite.hpp>
19
21
22#include <iostream>
23#include <random>
24#include <regex>
25#include <vector>
26
27namespace toolbox::bm {
28using namespace std;
29namespace {
30class BenchmarkStore {
31 public:
32 ~BenchmarkStore() noexcept = default;
33 // Copy.
34 BenchmarkStore(const BenchmarkStore&) = delete;
35 BenchmarkStore& operator=(const BenchmarkStore&) = delete;
36
37 // Move.
38 BenchmarkStore(BenchmarkStore&&) = delete;
39 BenchmarkStore& operator=(BenchmarkStore&&) = delete;
40
41 static BenchmarkStore& instance()
42 {
43 static BenchmarkStore store;
44 return store;
45 }
46 void list(ostream& os) const
47 {
48 for (const auto* bm : store_) {
49 os << bm->name << '\n';
50 }
51 }
52 void run(ostream& os, const string& regex_str, bool randomise)
53 {
55
57 for (auto* bm : store_) {
58 if (regex_search(bm->name, regex)) {
59 filtered.push_back(bm);
60 }
61 }
62
63 if (randomise) {
65 shuffle(begin(filtered), end(filtered), rng);
66 }
67
68 if (!filtered.empty()) {
69 BenchmarkSuite suite{os, 1000.0};
70 for (auto* bm : filtered) {
71 suite.run(bm->name, bm->fn);
72 }
73 }
74 }
75 void store(Benchmark& bm) { store_.push_back(&bm); }
76
77 private:
78 BenchmarkStore() = default;
79
80 vector<Benchmark*> store_;
81};
82} // namespace
83
84Benchmark::Benchmark(const char* name, void (*fn)(Context&))
85: name{name}
86, fn{fn}
87{
88 BenchmarkStore::instance().store(*this);
89}
90
91namespace detail {
92int main(int argc, char* argv[])
93{
94 int ret = 1;
95 try {
96 string regex;
97 bool list{false};
98 bool randomise{false};
99
100 Options opts{"benchmark options [options]"};
101 // clang-format off
102 opts('f', "filter", Value{regex}, "run benchmarks matching regex")
103 ('l', "list", Switch{list}, "list available benchmarks")
104 ('h', "help", Help{})
105 ('r', "random", Switch{randomise}, "run benchmarks in random order")
106 ;
107 // clang-format on
108
110
111 auto& store = BenchmarkStore::instance();
112 if (list) {
113 store.list(cout);
114 return 0;
115 }
116 store.run(cout, regex, randomise);
117 ret = 0;
118 } catch (const exception& e) {
119 cerr << "error: " << e.what();
120 }
121 return ret;
122}
123} // namespace detail
124} // namespace toolbox::bm
int main()
Definition EchoClnt.cpp:184
Command-line options parser.
void parse(int argc, const char *const argv[])
Definition Options.cpp:43
STL namespace.
constexpr auto bind() noexcept
Definition Slot.hpp:97
Benchmark(const char *name, void(*fn)(Context &))
Definition Benchmark.cpp:84