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
26namespace toolbox::bm {
27using namespace std;
28namespace {
29class BenchmarkStore {
30 public:
31 ~BenchmarkStore() noexcept = default;
32 // Copy.
33 BenchmarkStore(const BenchmarkStore&) = delete;
34 BenchmarkStore& operator=(const BenchmarkStore&) = delete;
35
36 // Move.
37 BenchmarkStore(BenchmarkStore&&) = delete;
38 BenchmarkStore& operator=(BenchmarkStore&&) = delete;
39
40 static BenchmarkStore& instance()
41 {
42 static BenchmarkStore store;
43 return store;
44 }
45 void list(ostream& os) const
46 {
47 for (const auto& runnable : store_) {
48 os << runnable.first << '\n';
49 }
50 }
51 void run(ostream& os, const string& regex_str, bool randomise)
52 {
54
56 for (const auto& bm : store_) {
57 if (regex_search(bm.first, regex)) {
58 filtered.push_back(bm.second);
59 }
60 }
61
62 if (randomise) {
64 shuffle(begin(filtered), end(filtered), rng);
65 }
66
67 if (!filtered.empty()) {
68 BenchmarkSuite suite{os, 1000.0};
69 for (auto* bm : filtered) {
70 suite.run(bm->name, bm->fn);
71 }
72 }
73 }
74 void store(const char* name, Benchmark& bm) { store_.insert_or_assign(name, &bm); }
75
76 private:
77 BenchmarkStore() = default;
78
80};
81} // namespace
82
83Benchmark::Benchmark(const char* name, void (*fn)(Context&))
84: name{name}
85, fn{fn}
86{
87 BenchmarkStore::instance().store(name, *this);
88}
89
90namespace detail {
91int main(int argc, char* argv[])
92{
93 int ret = 1;
94 try {
95 string regex;
96 bool list{false};
97 bool randomise{false};
98
99 Options opts{"benchmark options [options]"};
100 // clang-format off
101 opts('f', "filter", Value{regex}, "run benchmarks matching regex")
102 ('l', "list", Switch{list}, "list available benchmarks")
103 ('h', "help", Help{})
104 ('r', "random", Switch{randomise}, "run benchmarks in random order")
105 ;
106 // clang-format on
107
109
110 auto& store = BenchmarkStore::instance();
111 if (list) {
112 store.list(cout);
113 return 0;
114 }
115 store.run(cout, regex, randomise);
116 ret = 0;
117 } catch (const exception& e) {
118 cerr << "error: " << e.what();
119 }
120 return ret;
121}
122} // namespace detail
123} // namespace toolbox::bm
int main()
Definition EchoClnt.cpp:184
Command-line options parser.
void parse(int argc, const char *const argv[])
Definition Options.cpp:44
STL namespace.
constexpr auto bind() noexcept
Definition Slot.hpp:92
const char *const name
Definition Benchmark.hpp:26
Benchmark(const char *name, void(*fn)(Context &))
Definition Benchmark.cpp:83