Debian Codegraph @

3rdparty/catch2/tests/SelfTest/IntrospectiveTests/InternalBenchmark.tests.cpp

raw symbols SCIP
1
2 // Copyright Catch2 Authors
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE.txt or copy at
5 // https://www.boost.org/LICENSE_1_0.txt)
6
7 // SPDX-License-Identifier: BSL-1.0
8 // Adapted from donated nonius code.
9
10
11 #if defined( __GNUC__ ) || defined( __clang__ )
12 # pragma GCC diagnostic ignored "-Wfloat-equal"
13 #endif
14
15
16 #include <catch2/catch_test_macros.hpp>
17 #include <catch2/catch_approx.hpp>
18 #include <catch2/catch_config.hpp>
19 #include <catch2/benchmark/catch_benchmark.hpp>
20 #include <catch2/benchmark/catch_chronometer.hpp>
21 #include <catch2/benchmark/detail/catch_analyse.hpp>
22 #include <catch2/benchmark/detail/catch_benchmark_function.hpp>
23 #include <catch2/benchmark/detail/catch_estimate_clock.hpp>
24
25 #include <numeric>
26
27 namespace {
28 struct manual_clock {
29 public:
30 using duration = std::chrono::nanoseconds;
31 using time_point = std::chrono::time_point<manual_clock, duration>;
32 using rep = duration::rep;
33 using period = duration::period;
34 enum { is_steady = true };
35
36 static time_point now() {
37 return time_point(duration(tick()));
38 }
39
40 static void advance(int ticks = 1) {
41 tick() += ticks;
42 }
43
44 private:
45 static rep& tick() {
46 static rep the_tick = 0;
47 return the_tick;
48 }
49 };
50
51 struct counting_clock {
52 public:
53 using duration = std::chrono::nanoseconds;
54 using time_point = std::chrono::time_point<counting_clock, duration>;
55 using rep = duration::rep;
56 using period = duration::period;
57 enum { is_steady = true };
58
59 static time_point now() {
60 static rep ticks = 0;
61 return time_point(duration(ticks += rate()));
62 }
63
64 static void set_rate(rep new_rate) { rate() = new_rate; }
65
66 private:
67 static rep& rate() {
68 static rep the_rate = 1;
69 return the_rate;
70 }
71 };
72
73 struct TestChronometerModel : Catch::Benchmark::Detail::ChronometerConcept {
74 int started = 0;
75 int finished = 0;
76
77 void start() override { ++started; }
78 void finish() override { ++finished; }
79 };
80 } // namespace
81
82 TEST_CASE("warmup", "[benchmark]") {
83 auto rate = 1000;
84 counting_clock::set_rate(rate);
85
86 auto start = counting_clock::now();
87 auto iterations = Catch::Benchmark::Detail::warmup<counting_clock>();
88 auto end = counting_clock::now();
89
90 REQUIRE((iterations * rate) > Catch::Benchmark::Detail::warmup_time.count());
91 REQUIRE((end - start) > Catch::Benchmark::Detail::warmup_time);
92 }
93
94 TEST_CASE("resolution", "[benchmark]") {
95 auto rate = 1000;
96 counting_clock::set_rate(rate);
97
98 size_t count = 10;
99 auto res = Catch::Benchmark::Detail::resolution<counting_clock>(static_cast<int>(count));
100
101 REQUIRE(res.size() == count);
102
103 for (size_t i = 1; i < count; ++i) {
104 REQUIRE(res[i] == rate);
105 }
106 }
107
108 TEST_CASE("estimate_clock_resolution", "[benchmark]") {
109 auto rate = 2'000;
110 counting_clock::set_rate(rate);
111
112 int iters = 160'000;
113 auto res = Catch::Benchmark::Detail::estimate_clock_resolution<counting_clock>(iters);
114
115 REQUIRE(res.mean.count() == rate);
116 REQUIRE(res.outliers.total() == 0);
117 }
118
119 TEST_CASE("benchmark function call", "[benchmark]") {
120 SECTION("without chronometer") {
121 auto called = 0;
122 auto model = TestChronometerModel{};
123 auto meter = Catch::Benchmark::Chronometer{ model, 1 };
124 auto fn = Catch::Benchmark::Detail::BenchmarkFunction{ [&] {
125 CHECK(model.started == 1);
126 CHECK(model.finished == 0);
127 ++called;
128 } };
129
130 fn(meter);
131
132 CHECK(model.started == 1);
133 CHECK(model.finished == 1);
134 CHECK(called == 1);
135 }
136
137 SECTION("with chronometer") {
138 auto called = 0;
139 auto model = TestChronometerModel{};
140 auto meter = Catch::Benchmark::Chronometer{ model, 1 };
141 auto fn = Catch::Benchmark::Detail::BenchmarkFunction{ [&](Catch::Benchmark::Chronometer) {
142 CHECK(model.started == 0);
143 CHECK(model.finished == 0);
144 ++called;
145 } };
146
147 fn(meter);
148
149 CHECK(model.started == 0);
150 CHECK(model.finished == 0);
151 CHECK(called == 1);
152 }
153 }
154
155 TEST_CASE("uniform samples", "[benchmark]") {
156 std::vector<double> samples(100);
157 std::fill(samples.begin(), samples.end(), 23);
158
159 using it = std::vector<double>::iterator;
160 auto e = Catch::Benchmark::Detail::bootstrap(0.95, samples.begin(), samples.end(), samples, [](it a, it b) {
161 auto sum = std::accumulate(a, b, 0.);
162 return sum / (b - a);
163 });
164 CHECK(e.point == 23);
165 CHECK(e.upper_bound == 23);
166 CHECK(e.lower_bound == 23);
167 CHECK(e.confidence_interval == 0.95);
168 }
169
170
171 TEST_CASE("normal_cdf", "[benchmark]") {
172 using Catch::Benchmark::Detail::normal_cdf;
173 using Catch::Approx;
174 CHECK(normal_cdf(0.000000) == Approx(0.50000000000000000));
175 CHECK(normal_cdf(1.000000) == Approx(0.84134474606854293));
176 CHECK(normal_cdf(-1.000000) == Approx(0.15865525393145705));
177 CHECK(normal_cdf(2.809729) == Approx(0.99752083845315409));
178 CHECK(normal_cdf(-1.352570) == Approx(0.08809652095066035));
179 }
180
181 TEST_CASE("erfc_inv", "[benchmark]") {
182 using Catch::Benchmark::Detail::erfc_inv;
183 using Catch::Approx;
184 CHECK(erfc_inv(1.103560) == Approx(-0.09203687623843015));
185 CHECK(erfc_inv(1.067400) == Approx(-0.05980291115763361));
186 CHECK(erfc_inv(0.050000) == Approx(1.38590382434967796));
187 }
188
189 TEST_CASE("normal_quantile", "[benchmark]") {
190 using Catch::Benchmark::Detail::normal_quantile;
191 using Catch::Approx;
192 CHECK(normal_quantile(0.551780) == Approx(0.13015979861484198));
193 CHECK(normal_quantile(0.533700) == Approx(0.08457408802851875));
194 CHECK(normal_quantile(0.025000) == Approx(-1.95996398454005449));
195 }
196
197
198 TEST_CASE("mean", "[benchmark]") {
199 std::vector<double> x{ 10., 20., 14., 16., 30., 24. };
200
201 auto m = Catch::Benchmark::Detail::mean(x.begin(), x.end());
202
203 REQUIRE(m == 19.);
204 }
205
206 TEST_CASE("weighted_average_quantile", "[benchmark]") {
207 std::vector<double> x{ 10., 20., 14., 16., 30., 24. };
208
209 auto q1 = Catch::Benchmark::Detail::weighted_average_quantile(1, 4, x.begin(), x.end());
210 auto med = Catch::Benchmark::Detail::weighted_average_quantile(1, 2, x.begin(), x.end());
211 auto q3 = Catch::Benchmark::Detail::weighted_average_quantile(3, 4, x.begin(), x.end());
212
213 REQUIRE(q1 == 14.5);
214 REQUIRE(med == 18.);
215 REQUIRE(q3 == 23.);
216 }
217
218 TEST_CASE("classify_outliers", "[benchmark]") {
219 auto require_outliers = [](Catch::Benchmark::OutlierClassification o, int los, int lom, int him, int his) {
220 REQUIRE(o.low_severe == los);
221 REQUIRE(o.low_mild == lom);
222 REQUIRE(o.high_mild == him);
223 REQUIRE(o.high_severe == his);
224 REQUIRE(o.total() == los + lom + him + his);
225 };
226
227 SECTION("none") {
228 std::vector<double> x{ 10., 20., 14., 16., 30., 24. };
229
230 auto o = Catch::Benchmark::Detail::classify_outliers(x.begin(), x.end());
231
232 REQUIRE(o.samples_seen == static_cast<int>(x.size()));
233 require_outliers(o, 0, 0, 0, 0);
234 }
235 SECTION("low severe") {
236 std::vector<double> x{ -12., 20., 14., 16., 30., 24. };
237
238 auto o = Catch::Benchmark::Detail::classify_outliers(x.begin(), x.end());
239
240 REQUIRE(o.samples_seen == static_cast<int>(x.size()));
241 require_outliers(o, 1, 0, 0, 0);
242 }
243 SECTION("low mild") {
244 std::vector<double> x{ 1., 20., 14., 16., 30., 24. };
245
246 auto o = Catch::Benchmark::Detail::classify_outliers(x.begin(), x.end());
247
248 REQUIRE(o.samples_seen == static_cast<int>(x.size()));
249 require_outliers(o, 0, 1, 0, 0);
250 }
251 SECTION("high mild") {
252 std::vector<double> x{ 10., 20., 14., 16., 36., 24. };
253
254 auto o = Catch::Benchmark::Detail::classify_outliers(x.begin(), x.end());
255
256 REQUIRE(o.samples_seen == static_cast<int>(x.size()));
257 require_outliers(o, 0, 0, 1, 0);
258 }
259 SECTION("high severe") {
260 std::vector<double> x{ 10., 20., 14., 16., 49., 24. };
261
262 auto o = Catch::Benchmark::Detail::classify_outliers(x.begin(), x.end());
263
264 REQUIRE(o.samples_seen == static_cast<int>(x.size()));
265 require_outliers(o, 0, 0, 0, 1);
266 }
267 SECTION("mixed") {
268 std::vector<double> x{ -20., 20., 14., 16., 39., 24. };
269
270 auto o = Catch::Benchmark::Detail::classify_outliers(x.begin(), x.end());
271
272 REQUIRE(o.samples_seen == static_cast<int>(x.size()));
273 require_outliers(o, 1, 0, 1, 0);
274 }
275 }
276
277 TEST_CASE("analyse", "[approvals][benchmark]") {
278 Catch::ConfigData data{};
279 data.benchmarkConfidenceInterval = 0.95;
280 data.benchmarkNoAnalysis = false;
281 data.benchmarkResamples = 1000;
282 data.benchmarkSamples = 99;
283 Catch::Config config{data};
284
285 using Duration = Catch::Benchmark::FloatDuration<Catch::Benchmark::default_clock>;
286
287 Catch::Benchmark::Environment<Duration> env;
288 std::vector<Duration> samples(99);
289 for (size_t i = 0; i < samples.size(); ++i) {
290 samples[i] = Duration(23 + (i % 3 - 1));
291 }
292
293 auto analysis = Catch::Benchmark::Detail::analyse(config, env, samples.begin(), samples.end());
294 CHECK( analysis.mean.point.count() == 23 );
295 CHECK( analysis.mean.lower_bound.count() < 23 );
296 CHECK(analysis.mean.lower_bound.count() > 22);
297 CHECK(analysis.mean.upper_bound.count() > 23);
298 CHECK(analysis.mean.upper_bound.count() < 24);
299
300 CHECK(analysis.standard_deviation.point.count() > 0.5);
301 CHECK(analysis.standard_deviation.point.count() < 1);
302 CHECK(analysis.standard_deviation.lower_bound.count() > 0.5);
303 CHECK(analysis.standard_deviation.lower_bound.count() < 1);
304 CHECK(analysis.standard_deviation.upper_bound.count() > 0.5);
305 CHECK(analysis.standard_deviation.upper_bound.count() < 1);
306
307 CHECK(analysis.outliers.total() == 0);
308 CHECK(analysis.outliers.low_mild == 0);
309 CHECK(analysis.outliers.low_severe == 0);
310 CHECK(analysis.outliers.high_mild == 0);
311 CHECK(analysis.outliers.high_severe == 0);
312 CHECK(analysis.outliers.samples_seen == static_cast<int>(samples.size()));
313
314 CHECK(analysis.outlier_variance < 0.5);
315 CHECK(analysis.outlier_variance > 0);
316 }
317
318 TEST_CASE("analyse no analysis", "[benchmark]") {
319 Catch::ConfigData data{};
320 data.benchmarkConfidenceInterval = 0.95;
321 data.benchmarkNoAnalysis = true;
322 data.benchmarkResamples = 1000;
323 data.benchmarkSamples = 99;
324 Catch::Config config{ data };
325
326 using Duration = Catch::Benchmark::FloatDuration<Catch::Benchmark::default_clock>;
327
328 Catch::Benchmark::Environment<Duration> env;
329 std::vector<Duration> samples(99);
330 for (size_t i = 0; i < samples.size(); ++i) {
331 samples[i] = Duration(23 + (i % 3 - 1));
332 }
333
334 auto analysis = Catch::Benchmark::Detail::analyse(config, env, samples.begin(), samples.end());
335 CHECK(analysis.mean.point.count() == 23);
336 CHECK(analysis.mean.lower_bound.count() == 23);
337 CHECK(analysis.mean.upper_bound.count() == 23);
338
339 CHECK(analysis.standard_deviation.point.count() == 0);
340 CHECK(analysis.standard_deviation.lower_bound.count() == 0);
341 CHECK(analysis.standard_deviation.upper_bound.count() == 0);
342
343 CHECK(analysis.outliers.total() == 0);
344 CHECK(analysis.outliers.low_mild == 0);
345 CHECK(analysis.outliers.low_severe == 0);
346 CHECK(analysis.outliers.high_mild == 0);
347 CHECK(analysis.outliers.high_severe == 0);
348 CHECK(analysis.outliers.samples_seen == 0);
349
350 CHECK(analysis.outlier_variance == 0);
351 }
352
353 TEST_CASE("run_for_at_least, int", "[benchmark]") {
354 manual_clock::duration time(100);
355
356 int old_x = 1;
357 auto Timing = Catch::Benchmark::Detail::run_for_at_least<manual_clock>(time, 1, [&old_x](int x) -> int {
358 CHECK(x >= old_x);
359 manual_clock::advance(x);
360 old_x = x;
361 return x + 17;
362 });
363
364 REQUIRE(Timing.elapsed >= time);
365 REQUIRE(Timing.result == Timing.iterations + 17);
366 REQUIRE(Timing.iterations >= time.count());
367 }
368
369 TEST_CASE("run_for_at_least, chronometer", "[benchmark]") {
370 manual_clock::duration time(100);
371
372 int old_runs = 1;
373 auto Timing = Catch::Benchmark::Detail::run_for_at_least<manual_clock>(time, 1, [&old_runs](Catch::Benchmark::Chronometer meter) -> int {
374 CHECK(meter.runs() >= old_runs);
375 manual_clock::advance(100);
376 meter.measure([] {
377 manual_clock::advance(1);
378 });
379 old_runs = meter.runs();
380 return meter.runs() + 17;
381 });
382
383 REQUIRE(Timing.elapsed >= time);
384 REQUIRE(Timing.result == Timing.iterations + 17);
385 REQUIRE(Timing.iterations >= time.count());
386 }
387
388
389 TEST_CASE("measure", "[benchmark]") {
390 auto r = Catch::Benchmark::Detail::measure<manual_clock>([](int x) -> int {
391 CHECK(x == 17);
392 manual_clock::advance(42);
393 return 23;
394 }, 17);
395 auto s = Catch::Benchmark::Detail::measure<manual_clock>([](int x) -> int {
396 CHECK(x == 23);
397 manual_clock::advance(69);
398 return 17;
399 }, 23);
400
401 CHECK(r.elapsed.count() == 42);
402 CHECK(r.result == 23);
403 CHECK(r.iterations == 1);
404
405 CHECK(s.elapsed.count() == 69);
406 CHECK(s.result == 17);
407 CHECK(s.iterations == 1);
408 }
409
410 TEST_CASE("run benchmark", "[benchmark][approvals]") {
411 counting_clock::set_rate(1000);
412 auto start = counting_clock::now();
413
414 Catch::Benchmark::Benchmark bench{ "Test Benchmark", [](Catch::Benchmark::Chronometer meter) {
415 counting_clock::set_rate(100000);
416 meter.measure([] { return counting_clock::now(); });
417 } };
418
419 bench.run<counting_clock>();
420 auto end = counting_clock::now();
421
422 CHECK((end - start).count() == 2867251000);
423 }
424
425 TEST_CASE("Failing benchmarks", "[!benchmark][.approvals]") {
426 SECTION("empty", "Benchmark that has been optimized away (because it is empty)") {
427 BENCHMARK("Empty benchmark") {};
428 }
429 SECTION("throw", "Benchmark that throws an exception") {
430 BENCHMARK("Throwing benchmark") {
431 throw "just a plain literal, bleh";
432 };
433 }
434 SECTION("assert", "Benchmark that asserts inside") {
435 BENCHMARK("Asserting benchmark") {
436 REQUIRE(1 == 2);
437 };
438 }
439 SECTION("fail", "Benchmark that fails inside") {
440 BENCHMARK("FAIL'd benchmark") {
441 FAIL("This benchmark only fails, nothing else");
442 };
443 }
444 }
445
446 TEST_CASE( "Failing benchmark respects should-fail",
447 "[!shouldfail][!benchmark][approvals]" ) {
448 BENCHMARK( "Asserting benchmark" ) { REQUIRE( 1 == 2 ); };
449 }