cpp-stat-bench 0.24.0
Benchmark library with statistics for C++.
Loading...
Searching...
No Matches
runner.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 MusicScience37 (Kenta Kabashima)
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 */
21
22#include <exception>
23#include <memory>
24#include <string>
25
44
45namespace stat_bench {
46namespace runner {
47
49 const Config& config, bench_impl::BenchmarkCaseRegistry& registry)
50 : Runner(registry) {
51 measurers_.push_back(std::make_shared<measurer::ProcessingTimeMeasurer>(
54 measurers_.push_back(std::make_shared<measurer::MeanProcessingTimeMeasurer>(
57
58 reporters_.push_back(std::make_shared<reporter::ConsoleReporter>());
59
60 if (!config.plot_prefix.empty()) {
61 reporters_.push_back(
62 std::make_shared<reporter::PlotReporter>(config.plot_prefix));
63 }
64
65 if (!config.json_file_path.empty()) {
66 reporters_.push_back(
67 std::make_shared<reporter::JsonReporter>(config.json_file_path));
68 }
69
70 if (!config.msgpack_file_path.empty()) {
71 reporters_.push_back(std::make_shared<reporter::MsgPackReporter>(
72 config.msgpack_file_path));
73 }
74
75 if (!config.compressed_msgpack_file_path.empty()) {
76 reporters_.push_back(
77 std::make_shared<reporter::CompressedMsgPackReporter>(
78 config.compressed_msgpack_file_path));
79 }
80
82 for (const auto& pattern : config.include_glob) {
83 filter.include_with_glob(pattern);
84 }
85 for (const auto& pattern : config.exclude_glob) {
86 filter.exclude_with_glob(pattern);
87 }
88 for (const auto& regex : config.include_regex) {
89 filter.include_with_regex(regex);
90 }
91 for (const auto& regex : config.exclude_regex) {
92 filter.exclude_with_regex(regex);
93 }
94 registry.filter_by(filter);
95}
96
98 : registry_(registry) {}
99
100Runner::~Runner() = default;
101
102void Runner::run() const {
103 auto time_stamp = clock::SystemClock::now();
104 for (const auto& reporter : reporters_) {
105 reporter->experiment_starts(time_stamp);
106 }
107
108 for (const auto& measurer : measurers_) {
109 for (const auto& reporter : reporters_) {
110 reporter->measurer_starts(measurer->name());
111 }
112
113 for (const auto& group_pair : registry_.benchmarks()) {
114 const auto& group = group_pair.second;
115 for (const auto& reporter : reporters_) {
116 reporter->group_starts(group.name(), group.config());
117 }
118
119 for (const auto& bench_case_pair : group.cases()) {
120 const auto& bench_case = bench_case_pair.second;
121 run_case(measurer, bench_case);
122 }
123
124 for (const auto& reporter : reporters_) {
125 reporter->group_finished(group.name());
126 }
127 }
128
129 for (const auto& reporter : reporters_) {
130 reporter->measurer_finished(measurer->name());
131 }
132 }
133
134 time_stamp = clock::SystemClock::now();
135 for (const auto& reporter : reporters_) {
136 reporter->experiment_finished(time_stamp);
137 }
138}
139
140void Runner::run_case(const std::shared_ptr<measurer::IMeasurer>& measurer,
141 const std::shared_ptr<bench_impl::IBenchmarkCase>& bench_case) const {
142 auto params = bench_case->params();
143 auto generator = params.create_generator();
144
145 while (true) {
146 const auto cond = BenchmarkCondition(generator.generate());
147
148 run_case_with_condition(measurer, bench_case, cond);
149
150 if (!generator.iterate()) {
151 break;
152 }
153 }
154}
155
156void Runner::run_case_with_condition(
157 const std::shared_ptr<measurer::IMeasurer>& measurer,
158 const std::shared_ptr<bench_impl::IBenchmarkCase>& bench_case,
159 const BenchmarkCondition& cond) const {
160 for (const auto& reporter : reporters_) {
161 reporter->case_starts(bench_case->info());
162 }
163
164 std::exception_ptr error_in_reporter;
165 try {
166 const auto measurement = measurer->measure(bench_case.get(), cond);
167
168 try {
169 for (const auto& reporter : reporters_) {
170 reporter->measurement_succeeded(measurement);
171 }
172 } catch (...) {
173 error_in_reporter = std::current_exception();
174 }
175 } catch (...) {
176 const auto error = std::current_exception();
177 for (const auto& reporter : reporters_) {
178 reporter->measurement_failed(bench_case->info(), cond, error);
179 }
180 }
181 if (error_in_reporter) {
182 std::rethrow_exception(error_in_reporter);
183 }
184
185 for (const auto& reporter : reporters_) {
186 reporter->case_finished(bench_case->info());
187 }
188}
189
190} // namespace runner
191} // namespace stat_bench
Definition of BenchmarkCaseName class.
Definition of BenchmarkCaseRegistry class.
Definition of BenchmarkCondition class.
Definition of BenchmarkGroup class.
Definition of BenchmarkGroupName class.
Class of conditions of benchmarks.
Class of registry of cases of benchmarks.
void filter_by(const filters::ComposedFilter &filter)
Filter.
static auto now() -> SystemTimePoint
Get the current time.
Class of composed filters of benchmarks.
void run() const
Run benchmarks.
Definition runner.cpp:102
Runner(const Config &config, bench_impl::BenchmarkCaseRegistry &registry=bench_impl::BenchmarkCaseRegistry::instance())
Constructor.
Definition runner.cpp:48
Definition of ComposedFilter class.
Definition of CompressedMsgPackReporter class.
Definition of ConsoleReporter class.
Definition of JsonReporter class.
Definition of MeanProcessingTimeMeasurer class.
Definition of MsgPackReporter class.
Namespace of classes to measure time in benchmarks.
Namespace of reporters of results of benchmarks.
Namespace of classes to run benchmarks.
Namespace of stat_bench source codes.
Definition of OrderedMap class.
Definition of ParameterConfig class.
Definition of ParameterGenerator class.
Definition of PlotReporter class.
Definition of ProcessingTimeMeasurer class.
Definition of Runner class.
Class of configurations.
Definition config.h:54
std::vector< std::string > include_regex
Regular expressions of benchmark names to include.
Definition config.h:114
std::size_t min_warming_up_iterations
Minimum number of iterations for warming up.
Definition config.h:72
std::string json_file_path
File path of JSON data file.
Definition config.h:91
std::string msgpack_file_path
File path of MsgPack data file.
Definition config.h:98
std::size_t processing_time_samples
Number of samples for measurements of processing time.
Definition config.h:59
double min_warming_up_duration_sec
Minimum duration for warming up. [sec].
Definition config.h:76
double min_sample_duration_sec
Minimum duration of a sample for measurement of mean processing time. [sec].
Definition config.h:69
std::string plot_prefix
Prefix of filepaths to generate plots.
Definition config.h:84
std::vector< std::string > include_glob
Glob patterns of benchmark names to include.
Definition config.h:108
std::size_t mean_processing_time_samples
Number of samples for measurements of mean processing time.
Definition config.h:62
std::vector< std::string > exclude_regex
Regular expressions of benchmark names to exclude.
Definition config.h:117
std::vector< std::string > exclude_glob
Glob patterns of benchmark names to exclude.
Definition config.h:111
std::string compressed_msgpack_file_path
File path of compressed MsgPack data file.
Definition config.h:105
Definition of SystemClock class.
Definition of SystemTimePoint class.