1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
// Adapted from donated nonius code.
#pragma once
#include <Progmasoft/Catch3/UserConfig.hpp>
#include <Progmasoft/Catch3/Internal/CompilerCapabilities.hpp>
#include <Progmasoft/Catch3/Internal/Context.hpp>
#include <Progmasoft/Catch3/Internal/MoveAndForward.hpp>
#include <Progmasoft/Catch3/Internal/TestFailureException.hpp>
#include <Progmasoft/Catch3/Internal/UniqueName.hpp>
#include <Progmasoft/Catch3/Interfaces/IResultCapture.hpp>
#include <Progmasoft/Catch3/Interfaces/IConfig.hpp>
#include <Progmasoft/Catch3/Interfaces/IRegistryHub.hpp>
#include <Progmasoft/Catch3/Benchmark/Detail/BenchmarkStats.hpp>
#include <Progmasoft/Catch3/Benchmark/Clock.hpp>
#include <Progmasoft/Catch3/Benchmark/Environment.hpp>
#include <Progmasoft/Catch3/Benchmark/ExecutionPlan.hpp>
#include <Progmasoft/Catch3/Benchmark/Detail/EstimateClock.hpp>
#include <Progmasoft/Catch3/Benchmark/Detail/Analyse.hpp>
#include <Progmasoft/Catch3/Benchmark/Detail/BenchmarkFunction.hpp>
#include <Progmasoft/Catch3/Benchmark/Detail/RunForAtLeast.hpp>
#include <algorithm>
#include <chrono>
#include <exception>
#include <string>
#include <cmath>
#include <type_traits>
namespace Catch {
namespace Benchmark {
namespace Detail {
template <typename Clock>
ExecutionPlan prepare( const IConfig& cfg,
Environment env,
BenchmarkFunction&& fun ) {
auto min_time =
env.clock_resolution.mean * Detail::minimum_ticks;
auto run_time =
std::max( min_time,
std::chrono::duration_cast<decltype( min_time )>(
cfg.benchmarkWarmupTime() ) );
auto&& test = Detail::run_for_at_least<Clock>(
std::chrono::duration_cast<IDuration>( run_time ), 1, fun );
int new_iters = static_cast<int>(
std::ceil( min_time * test.iterations / test.elapsed ) );
return { new_iters,
test.elapsed / test.iterations * new_iters *
cfg.benchmarkSamples(),
CATCH_MOVE( fun ),
std::chrono::duration_cast<FDuration>(
cfg.benchmarkWarmupTime() ),
Detail::warmup_iterations };
}
// These are wrappers for their respective function templated
// over `default_clock`. This allows outlining the usual use
// of the template into single TU and save on compilation costs.
Environment measure_environment_default();
ExecutionPlan prepare_default( const IConfig& cfg,
Environment env,
BenchmarkFunction&& fun );
std::vector<FDuration> run_plan_default( ExecutionPlan const& plan,
const IConfig& cfg,
Environment env );
template <typename Clock>
std::enable_if_t<std::is_same<Clock, default_clock>::value,
Environment>
measureEnvironmentDispatch() {
return measure_environment_default();
}
template <typename Clock>
std::enable_if_t<!std::is_same<Clock, default_clock>::value,
Environment>
measureEnvironmentDispatch() {
return measure_environment<Clock>();
}
template <typename Clock>
std::enable_if_t<std::is_same<Clock, default_clock>::value,
std::vector<FDuration>>
runPlanDispatch( ExecutionPlan const& plan,
const IConfig& cfg,
Environment env ) {
return run_plan_default( plan, cfg, env );
}
template <typename Clock>
std::enable_if_t<!std::is_same<Clock, default_clock>::value,
std::vector<FDuration>>
runPlanDispatch( ExecutionPlan const& plan,
const IConfig& cfg,
Environment env ) {
return plan.template run<Clock>( cfg, env );
}
template <typename Clock>
std::enable_if_t<std::is_same<Clock, default_clock>::value,
ExecutionPlan>
prepareDispatch( const IConfig& cfg,
Environment env,
BenchmarkFunction&& fun ) {
return prepare_default( cfg, env, CATCH_MOVE( fun ) );
}
template <typename Clock>
std::enable_if_t<!std::is_same<Clock, default_clock>::value,
ExecutionPlan>
prepareDispatch( const IConfig& cfg,
Environment env,
BenchmarkFunction&& fun ) {
return prepare<Clock>( cfg, env, CATCH_MOVE( fun ) );
}
} // namespace Detail
struct Benchmark {
Benchmark(std::string&& benchmarkName)
: name(CATCH_MOVE(benchmarkName)) {}
template <class FUN>
Benchmark(std::string&& benchmarkName , FUN &&func)
: fun(CATCH_MOVE(func)), name(CATCH_MOVE(benchmarkName)) {}
template <typename Clock = default_clock>
void run() {
static_assert( Clock::is_steady,
"Benchmarking clock should be steady" );
auto const* cfg = getCurrentContext().getConfig();
auto env = Detail::measureEnvironmentDispatch<Clock>();
getResultCapture().benchmarkPreparing(name);
CATCH_TRY{
auto plan = user_code([&] {
return Detail::prepareDispatch<Clock>( *cfg, env, CATCH_MOVE(fun) );
});
BenchmarkInfo info {
CATCH_MOVE(name),
plan.estimated_duration.count(),
plan.iterations_per_sample,
cfg->benchmarkSamples(),
cfg->benchmarkResamples(),
env.clock_resolution.mean.count(),
env.clock_cost.mean.count()
};
getResultCapture().benchmarkStarting(info);
auto samples = user_code([&] {
return Detail::runPlanDispatch<Clock>( plan, *cfg, env );
});
auto analysis = Detail::analyse(*cfg, samples.data(), samples.data() + samples.size());
BenchmarkStats<> stats{ CATCH_MOVE(info), CATCH_MOVE(analysis.samples), analysis.mean, analysis.standard_deviation, analysis.outliers, analysis.outlier_variance };
getResultCapture().benchmarkEnded(stats);
} CATCH_CATCH_ALL {
getResultCapture().benchmarkFailed(translateActiveException());
// We let the exception go further up so that the
// test case is marked as failed.
std::rethrow_exception(std::current_exception());
}
}
// sets lambda to be used in fun *and* executes benchmark!
template <typename Fun, std::enable_if_t<!Detail::is_related_v<Fun, Benchmark>, int> = 0>
Benchmark & operator=(Fun func) {
auto const* cfg = getCurrentContext().getConfig();
if (!cfg->skipBenchmarks()) {
fun = Detail::BenchmarkFunction(func);
run();
}
return *this;
}
explicit operator bool() {
return true;
}
private:
Detail::BenchmarkFunction fun;
std::string name;
};
}
} // namespace Catch
#define INTERNAL_CATCH_GET_1_ARG(arg1, arg2, ...) arg1
#define INTERNAL_CATCH_GET_2_ARG(arg1, arg2, ...) arg2
#define INTERNAL_CATCH_BENCHMARK(BenchmarkName, name, benchmarkIndex)\
if( Catch::Benchmark::Benchmark BenchmarkName{name} ) \
BenchmarkName = [&](int benchmarkIndex)
#define INTERNAL_CATCH_BENCHMARK_ADVANCED(BenchmarkName, name)\
if( Catch::Benchmark::Benchmark BenchmarkName{name} ) \
BenchmarkName = [&]
#if defined(CATCH_CONFIG_PREFIX_ALL)
#define CATCH_BENCHMARK(...) \
INTERNAL_CATCH_BENCHMARK(INTERNAL_CATCH_UNIQUE_NAME(CATCH2_INTERNAL_BENCHMARK_), INTERNAL_CATCH_GET_1_ARG(__VA_ARGS__,,), INTERNAL_CATCH_GET_2_ARG(__VA_ARGS__,,))
#define CATCH_BENCHMARK_ADVANCED(name) \
INTERNAL_CATCH_BENCHMARK_ADVANCED(INTERNAL_CATCH_UNIQUE_NAME(CATCH2_INTERNAL_BENCHMARK_), name)
#else
#define BENCHMARK(...) \
INTERNAL_CATCH_BENCHMARK(INTERNAL_CATCH_UNIQUE_NAME(CATCH2_INTERNAL_BENCHMARK_), INTERNAL_CATCH_GET_1_ARG(__VA_ARGS__,,), INTERNAL_CATCH_GET_2_ARG(__VA_ARGS__,,))
#define BENCHMARK_ADVANCED(name) \
INTERNAL_CATCH_BENCHMARK_ADVANCED(INTERNAL_CATCH_UNIQUE_NAME(CATCH2_INTERNAL_BENCHMARK_), name)
#endif
|