| 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 |
auto e = Catch::Benchmark::Detail::bootstrap( |
| 160 |
0.95, |
| 161 |
samples.data(), |
| 162 |
samples.data() + samples.size(), |
| 163 |
samples, |
| 164 |
[]( double const* a, double const* b ) { |
| 165 |
auto sum = std::accumulate(a, b, 0.); |
| 166 |
return sum / (b - a); |
| 167 |
}); |
| 168 |
CHECK(e.point == 23); |
| 169 |
CHECK(e.upper_bound == 23); |
| 170 |
CHECK(e.lower_bound == 23); |
| 171 |
CHECK(e.confidence_interval == 0.95); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
TEST_CASE("normal_cdf", "[benchmark][approvals]") { |
| 176 |
using Catch::Benchmark::Detail::normal_cdf; |
| 177 |
using Catch::Approx; |
| 178 |
CHECK(normal_cdf(0.000000) == Approx(0.50000000000000000)); |
| 179 |
CHECK(normal_cdf(1.000000) == Approx(0.84134474606854293)); |
| 180 |
CHECK(normal_cdf(-1.000000) == Approx(0.15865525393145705)); |
| 181 |
CHECK(normal_cdf(2.809729) == Approx(0.99752083845315409)); |
| 182 |
CHECK(normal_cdf(-1.352570) == Approx(0.08809652095066035)); |
| 183 |
} |
| 184 |
|
| 185 |
TEST_CASE("erfc_inv", "[benchmark]") { |
| 186 |
using Catch::Benchmark::Detail::erfc_inv; |
| 187 |
using Catch::Approx; |
| 188 |
CHECK(erfc_inv(1.103560) == Approx(-0.09203687623843015)); |
| 189 |
CHECK(erfc_inv(1.067400) == Approx(-0.05980291115763361)); |
| 190 |
CHECK(erfc_inv(0.050000) == Approx(1.38590382434967796)); |
| 191 |
} |
| 192 |
|
| 193 |
TEST_CASE("normal_quantile", "[benchmark]") { |
| 194 |
using Catch::Benchmark::Detail::normal_quantile; |
| 195 |
using Catch::Approx; |
| 196 |
CHECK(normal_quantile(0.551780) == Approx(0.13015979861484198)); |
| 197 |
CHECK(normal_quantile(0.533700) == Approx(0.08457408802851875)); |
| 198 |
CHECK(normal_quantile(0.025000) == Approx(-1.95996398454005449)); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
TEST_CASE("mean", "[benchmark]") { |
| 203 |
std::vector<double> x{ 10., 20., 14., 16., 30., 24. }; |
| 204 |
|
| 205 |
auto m = Catch::Benchmark::Detail::mean(x.data(), x.data() + x.size()); |
| 206 |
|
| 207 |
REQUIRE(m == 19.); |
| 208 |
} |
| 209 |
|
| 210 |
TEST_CASE("weighted_average_quantile", "[benchmark]") { |
| 211 |
std::vector<double> x{ 10., 20., 14., 16., 30., 24. }; |
| 212 |
|
| 213 |
auto q1 = Catch::Benchmark::Detail::weighted_average_quantile(1, 4, x.data(), x.data() + x.size()); |
| 214 |
auto med = Catch::Benchmark::Detail::weighted_average_quantile(1, 2, x.data(), x.data() + x.size()); |
| 215 |
auto q3 = Catch::Benchmark::Detail::weighted_average_quantile(3, 4, x.data(), x.data() + x.size()); |
| 216 |
|
| 217 |
REQUIRE(q1 == 14.5); |
| 218 |
REQUIRE(med == 18.); |
| 219 |
REQUIRE(q3 == 23.); |
| 220 |
} |
| 221 |
|
| 222 |
TEST_CASE("classify_outliers", "[benchmark]") { |
| 223 |
auto require_outliers = [](Catch::Benchmark::OutlierClassification o, int los, int lom, int him, int his) { |
| 224 |
REQUIRE(o.low_severe == los); |
| 225 |
REQUIRE(o.low_mild == lom); |
| 226 |
REQUIRE(o.high_mild == him); |
| 227 |
REQUIRE(o.high_severe == his); |
| 228 |
REQUIRE(o.total() == los + lom + him + his); |
| 229 |
}; |
| 230 |
|
| 231 |
SECTION("none") { |
| 232 |
std::vector<double> x{ 10., 20., 14., 16., 30., 24. }; |
| 233 |
|
| 234 |
auto o = Catch::Benchmark::Detail::classify_outliers( |
| 235 |
x.data(), x.data() + x.size() ); |
| 236 |
|
| 237 |
REQUIRE(o.samples_seen == static_cast<int>(x.size())); |
| 238 |
require_outliers(o, 0, 0, 0, 0); |
| 239 |
} |
| 240 |
SECTION("low severe") { |
| 241 |
std::vector<double> x{ -12., 20., 14., 16., 30., 24. }; |
| 242 |
|
| 243 |
auto o = Catch::Benchmark::Detail::classify_outliers( |
| 244 |
x.data(), x.data() + x.size() ); |
| 245 |
|
| 246 |
REQUIRE(o.samples_seen == static_cast<int>(x.size())); |
| 247 |
require_outliers(o, 1, 0, 0, 0); |
| 248 |
} |
| 249 |
SECTION("low mild") { |
| 250 |
std::vector<double> x{ 1., 20., 14., 16., 30., 24. }; |
| 251 |
|
| 252 |
auto o = Catch::Benchmark::Detail::classify_outliers( |
| 253 |
x.data(), x.data() + x.size() ); |
| 254 |
|
| 255 |
REQUIRE(o.samples_seen == static_cast<int>(x.size())); |
| 256 |
require_outliers(o, 0, 1, 0, 0); |
| 257 |
} |
| 258 |
SECTION("high mild") { |
| 259 |
std::vector<double> x{ 10., 20., 14., 16., 36., 24. }; |
| 260 |
|
| 261 |
auto o = Catch::Benchmark::Detail::classify_outliers( |
| 262 |
x.data(), x.data() + x.size() ); |
| 263 |
|
| 264 |
REQUIRE(o.samples_seen == static_cast<int>(x.size())); |
| 265 |
require_outliers(o, 0, 0, 1, 0); |
| 266 |
} |
| 267 |
SECTION("high severe") { |
| 268 |
std::vector<double> x{ 10., 20., 14., 16., 49., 24. }; |
| 269 |
|
| 270 |
auto o = Catch::Benchmark::Detail::classify_outliers( |
| 271 |
x.data(), x.data() + x.size() ); |
| 272 |
|
| 273 |
REQUIRE(o.samples_seen == static_cast<int>(x.size())); |
| 274 |
require_outliers(o, 0, 0, 0, 1); |
| 275 |
} |
| 276 |
SECTION("mixed") { |
| 277 |
std::vector<double> x{ -20., 20., 14., 16., 39., 24. }; |
| 278 |
|
| 279 |
auto o = Catch::Benchmark::Detail::classify_outliers( |
| 280 |
x.data(), x.data() + x.size() ); |
| 281 |
|
| 282 |
REQUIRE(o.samples_seen == static_cast<int>(x.size())); |
| 283 |
require_outliers(o, 1, 0, 1, 0); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
TEST_CASE("analyse", "[approvals][benchmark]") { |
| 288 |
Catch::ConfigData data{}; |
| 289 |
data.benchmarkConfidenceInterval = 0.95; |
| 290 |
data.benchmarkNoAnalysis = false; |
| 291 |
data.benchmarkResamples = 1000; |
| 292 |
data.benchmarkSamples = 99; |
| 293 |
Catch::Config config{data}; |
| 294 |
|
| 295 |
using FDuration = Catch::Benchmark::FDuration; |
| 296 |
std::vector<FDuration> samples(99); |
| 297 |
for (size_t i = 0; i < samples.size(); ++i) { |
| 298 |
samples[i] = FDuration(23 + (i % 3 - 1)); |
| 299 |
} |
| 300 |
|
| 301 |
auto analysis = Catch::Benchmark::Detail::analyse(config, samples.data(), samples.data() + samples.size()); |
| 302 |
CHECK( analysis.mean.point.count() == 23 ); |
| 303 |
CHECK( analysis.mean.lower_bound.count() < 23 ); |
| 304 |
CHECK(analysis.mean.lower_bound.count() > 22); |
| 305 |
CHECK(analysis.mean.upper_bound.count() > 23); |
| 306 |
CHECK(analysis.mean.upper_bound.count() < 24); |
| 307 |
|
| 308 |
CHECK(analysis.standard_deviation.point.count() > 0.5); |
| 309 |
CHECK(analysis.standard_deviation.point.count() < 1); |
| 310 |
CHECK(analysis.standard_deviation.lower_bound.count() > 0.5); |
| 311 |
CHECK(analysis.standard_deviation.lower_bound.count() < 1); |
| 312 |
CHECK(analysis.standard_deviation.upper_bound.count() > 0.5); |
| 313 |
CHECK(analysis.standard_deviation.upper_bound.count() < 1); |
| 314 |
|
| 315 |
CHECK(analysis.outliers.total() == 0); |
| 316 |
CHECK(analysis.outliers.low_mild == 0); |
| 317 |
CHECK(analysis.outliers.low_severe == 0); |
| 318 |
CHECK(analysis.outliers.high_mild == 0); |
| 319 |
CHECK(analysis.outliers.high_severe == 0); |
| 320 |
CHECK(analysis.outliers.samples_seen == static_cast<int>(samples.size())); |
| 321 |
|
| 322 |
CHECK(analysis.outlier_variance < 0.5); |
| 323 |
CHECK(analysis.outlier_variance > 0); |
| 324 |
} |
| 325 |
|
| 326 |
TEST_CASE("analyse no analysis", "[benchmark]") { |
| 327 |
Catch::ConfigData data{}; |
| 328 |
data.benchmarkConfidenceInterval = 0.95; |
| 329 |
data.benchmarkNoAnalysis = true; |
| 330 |
data.benchmarkResamples = 1000; |
| 331 |
data.benchmarkSamples = 99; |
| 332 |
Catch::Config config{ data }; |
| 333 |
|
| 334 |
using FDuration = Catch::Benchmark::FDuration; |
| 335 |
std::vector<FDuration> samples(99); |
| 336 |
for (size_t i = 0; i < samples.size(); ++i) { |
| 337 |
samples[i] = FDuration(23 + (i % 3 - 1)); |
| 338 |
} |
| 339 |
|
| 340 |
auto analysis = Catch::Benchmark::Detail::analyse(config, samples.data(), samples.data() + samples.size()); |
| 341 |
CHECK(analysis.mean.point.count() == 23); |
| 342 |
CHECK(analysis.mean.lower_bound.count() == 23); |
| 343 |
CHECK(analysis.mean.upper_bound.count() == 23); |
| 344 |
|
| 345 |
CHECK(analysis.standard_deviation.point.count() == 0); |
| 346 |
CHECK(analysis.standard_deviation.lower_bound.count() == 0); |
| 347 |
CHECK(analysis.standard_deviation.upper_bound.count() == 0); |
| 348 |
|
| 349 |
CHECK(analysis.outliers.total() == 0); |
| 350 |
CHECK(analysis.outliers.low_mild == 0); |
| 351 |
CHECK(analysis.outliers.low_severe == 0); |
| 352 |
CHECK(analysis.outliers.high_mild == 0); |
| 353 |
CHECK(analysis.outliers.high_severe == 0); |
| 354 |
CHECK(analysis.outliers.samples_seen == 0); |
| 355 |
|
| 356 |
CHECK(analysis.outlier_variance == 0); |
| 357 |
} |
| 358 |
|
| 359 |
TEST_CASE("run_for_at_least, int", "[benchmark]") { |
| 360 |
manual_clock::duration time(100); |
| 361 |
|
| 362 |
int old_x = 1; |
| 363 |
auto Timing = Catch::Benchmark::Detail::run_for_at_least<manual_clock>(time, 1, [&old_x](int x) -> int { |
| 364 |
CHECK(x >= old_x); |
| 365 |
manual_clock::advance(x); |
| 366 |
old_x = x; |
| 367 |
return x + 17; |
| 368 |
}); |
| 369 |
|
| 370 |
REQUIRE(Timing.elapsed >= time); |
| 371 |
REQUIRE(Timing.result == Timing.iterations + 17); |
| 372 |
REQUIRE(Timing.iterations >= time.count()); |
| 373 |
} |
| 374 |
|
| 375 |
TEST_CASE("run_for_at_least, chronometer", "[benchmark]") { |
| 376 |
manual_clock::duration time(100); |
| 377 |
|
| 378 |
int old_runs = 1; |
| 379 |
auto Timing = Catch::Benchmark::Detail::run_for_at_least<manual_clock>(time, 1, [&old_runs](Catch::Benchmark::Chronometer meter) -> int { |
| 380 |
CHECK(meter.runs() >= old_runs); |
| 381 |
manual_clock::advance(100); |
| 382 |
meter.measure([] { |
| 383 |
manual_clock::advance(1); |
| 384 |
}); |
| 385 |
old_runs = meter.runs(); |
| 386 |
return meter.runs() + 17; |
| 387 |
}); |
| 388 |
|
| 389 |
REQUIRE(Timing.elapsed >= time); |
| 390 |
REQUIRE(Timing.result == Timing.iterations + 17); |
| 391 |
REQUIRE(Timing.iterations >= time.count()); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
TEST_CASE("measure", "[benchmark]") { |
| 396 |
auto r = Catch::Benchmark::Detail::measure<manual_clock>([](int x) -> int { |
| 397 |
CHECK(x == 17); |
| 398 |
manual_clock::advance(42); |
| 399 |
return 23; |
| 400 |
}, 17); |
| 401 |
auto s = Catch::Benchmark::Detail::measure<manual_clock>([](int x) -> int { |
| 402 |
CHECK(x == 23); |
| 403 |
manual_clock::advance(69); |
| 404 |
return 17; |
| 405 |
}, 23); |
| 406 |
|
| 407 |
CHECK(r.elapsed.count() == 42); |
| 408 |
CHECK(r.result == 23); |
| 409 |
CHECK(r.iterations == 1); |
| 410 |
|
| 411 |
CHECK(s.elapsed.count() == 69); |
| 412 |
CHECK(s.result == 17); |
| 413 |
CHECK(s.iterations == 1); |
| 414 |
} |
| 415 |
|
| 416 |
TEST_CASE("run benchmark", "[benchmark][approvals]") { |
| 417 |
counting_clock::set_rate(1000); |
| 418 |
auto start = counting_clock::now(); |
| 419 |
|
| 420 |
Catch::Benchmark::Benchmark bench{ "Test Benchmark", [](Catch::Benchmark::Chronometer meter) { |
| 421 |
counting_clock::set_rate(100000); |
| 422 |
meter.measure([] { return counting_clock::now(); }); |
| 423 |
} }; |
| 424 |
|
| 425 |
bench.run<counting_clock>(); |
| 426 |
auto end = counting_clock::now(); |
| 427 |
|
| 428 |
CHECK((end - start).count() == 2867251000); |
| 429 |
} |
| 430 |
|
| 431 |
TEST_CASE("Failing benchmarks", "[!benchmark][.approvals]") { |
| 432 |
SECTION("empty", "Benchmark that has been optimized away (because it is empty)") { |
| 433 |
BENCHMARK("Empty benchmark") {}; |
| 434 |
} |
| 435 |
SECTION("throw", "Benchmark that throws an exception") { |
| 436 |
BENCHMARK("Throwing benchmark") { |
| 437 |
throw "just a plain literal, bleh"; |
| 438 |
}; |
| 439 |
} |
| 440 |
SECTION("assert", "Benchmark that asserts inside") { |
| 441 |
BENCHMARK("Asserting benchmark") { |
| 442 |
REQUIRE(1 == 2); |
| 443 |
}; |
| 444 |
} |
| 445 |
SECTION("fail", "Benchmark that fails inside") { |
| 446 |
BENCHMARK("FAIL'd benchmark") { |
| 447 |
FAIL("This benchmark only fails, nothing else"); |
| 448 |
}; |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
TEST_CASE( "Failing benchmark respects should-fail", |
| 453 |
"[!shouldfail][!benchmark][approvals]" ) { |
| 454 |
BENCHMARK( "Asserting benchmark" ) { REQUIRE( 1 == 2 ); }; |
| 455 |
} |