Toolbox
snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
toolbox
bm
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
20
#include <
toolbox/util/Options.hpp
>
21
22
#include <iostream>
23
#include <random>
24
#include <regex>
25
26
namespace
toolbox::bm
{
27
using namespace
std
;
28
namespace
{
29
class
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
{
53
vector<Benchmark*>
filtered
;
54
55
regex
regex
{
regex_str
};
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
) {
63
default_random_engine
rng
{};
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
79
map<const char*, Benchmark*>
store_;
80
};
81
}
// namespace
82
83
Benchmark::Benchmark
(
const
char
* name,
void
(*fn)(
Context
&))
84
: name{name}
85
, fn{fn}
86
{
87
BenchmarkStore::instance().store(
name
, *
this
);
88
}
89
90
namespace
detail {
91
int
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
108
opts
.
parse
(
argc
,
argv
);
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
Benchmark.hpp
main
int main()
Definition
EchoClnt.cpp:184
Options.hpp
Command-line options parser.
Suite.hpp
toolbox::bm::Context
Definition
Context.hpp:24
toolbox::Help
Definition
Options.hpp:38
toolbox::Options
Definition
Options.hpp:119
toolbox::util::Options::parse
void parse(int argc, const char *const argv[])
Definition
Options.cpp:44
toolbox::Switch
Definition
Options.hpp:104
toolbox::Value
Definition
Options.hpp:57
std
STL namespace.
toolbox::bm
Definition
Benchmark.cpp:26
toolbox::util::bind
constexpr auto bind() noexcept
Definition
Slot.hpp:92
toolbox::bm::Benchmark::name
const char *const name
Definition
Benchmark.hpp:26
toolbox::bm::Benchmark::Benchmark
Benchmark(const char *name, void(*fn)(Context &))
Definition
Benchmark.cpp:83
Generated by
1.9.8