catch_benchmark.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Created by Joachim on 16/04/2019.
  3. * Adapted from donated nonius code.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. // Benchmark
  9. #ifndef TWOBLUECUBES_CATCH_BENCHMARK_HPP_INCLUDED
  10. #define TWOBLUECUBES_CATCH_BENCHMARK_HPP_INCLUDED
  11. #include "../catch_config.hpp"
  12. #include "../catch_context.h"
  13. #include "../catch_interfaces_reporter.h"
  14. #include "../catch_test_registry.h"
  15. #include "catch_chronometer.hpp"
  16. #include "catch_clock.hpp"
  17. #include "catch_environment.hpp"
  18. #include "catch_execution_plan.hpp"
  19. #include "detail/catch_estimate_clock.hpp"
  20. #include "detail/catch_complete_invoke.hpp"
  21. #include "detail/catch_analyse.hpp"
  22. #include "detail/catch_benchmark_function.hpp"
  23. #include "detail/catch_run_for_at_least.hpp"
  24. #include <algorithm>
  25. #include <functional>
  26. #include <string>
  27. #include <vector>
  28. #include <cmath>
  29. namespace Catch {
  30. namespace Benchmark {
  31. struct Benchmark {
  32. Benchmark(std::string &&name)
  33. : name(std::move(name)) {}
  34. template <class FUN>
  35. Benchmark(std::string &&name, FUN &&func)
  36. : fun(std::move(func)), name(std::move(name)) {}
  37. template <typename Clock>
  38. ExecutionPlan<FloatDuration<Clock>> prepare(const IConfig &cfg, Environment<FloatDuration<Clock>> env) const {
  39. auto min_time = env.clock_resolution.mean * Detail::minimum_ticks;
  40. auto run_time = std::max(min_time, std::chrono::duration_cast<decltype(min_time)>(cfg.benchmarkWarmupTime()));
  41. auto&& test = Detail::run_for_at_least<Clock>(std::chrono::duration_cast<ClockDuration<Clock>>(run_time), 1, fun);
  42. int new_iters = static_cast<int>(std::ceil(min_time * test.iterations / test.elapsed));
  43. return { new_iters, test.elapsed / test.iterations * new_iters * cfg.benchmarkSamples(), fun, std::chrono::duration_cast<FloatDuration<Clock>>(cfg.benchmarkWarmupTime()), Detail::warmup_iterations };
  44. }
  45. template <typename Clock = default_clock>
  46. void run() {
  47. IConfigPtr cfg = getCurrentContext().getConfig();
  48. auto env = Detail::measure_environment<Clock>();
  49. getResultCapture().benchmarkPreparing(name);
  50. CATCH_TRY{
  51. auto plan = user_code([&] {
  52. return prepare<Clock>(*cfg, env);
  53. });
  54. BenchmarkInfo info {
  55. name,
  56. plan.estimated_duration.count(),
  57. plan.iterations_per_sample,
  58. cfg->benchmarkSamples(),
  59. cfg->benchmarkResamples(),
  60. env.clock_resolution.mean.count(),
  61. env.clock_cost.mean.count()
  62. };
  63. getResultCapture().benchmarkStarting(info);
  64. auto samples = user_code([&] {
  65. return plan.template run<Clock>(*cfg, env);
  66. });
  67. auto analysis = Detail::analyse(*cfg, env, samples.begin(), samples.end());
  68. BenchmarkStats<FloatDuration<Clock>> stats{ info, analysis.samples, analysis.mean, analysis.standard_deviation, analysis.outliers, analysis.outlier_variance };
  69. getResultCapture().benchmarkEnded(stats);
  70. } CATCH_CATCH_ALL{
  71. if (translateActiveException() != Detail::benchmarkErrorMsg) // benchmark errors have been reported, otherwise rethrow.
  72. std::rethrow_exception(std::current_exception());
  73. }
  74. }
  75. // sets lambda to be used in fun *and* executes benchmark!
  76. template <typename Fun,
  77. typename std::enable_if<!Detail::is_related<Fun, Benchmark>::value, int>::type = 0>
  78. Benchmark & operator=(Fun func) {
  79. fun = Detail::BenchmarkFunction(func);
  80. run();
  81. return *this;
  82. }
  83. explicit operator bool() {
  84. return true;
  85. }
  86. private:
  87. Detail::BenchmarkFunction fun;
  88. std::string name;
  89. };
  90. }
  91. } // namespace Catch
  92. #define INTERNAL_CATCH_GET_1_ARG(arg1, arg2, ...) arg1
  93. #define INTERNAL_CATCH_GET_2_ARG(arg1, arg2, ...) arg2
  94. #define INTERNAL_CATCH_BENCHMARK(BenchmarkName, name, benchmarkIndex)\
  95. if( Catch::Benchmark::Benchmark BenchmarkName{name} ) \
  96. BenchmarkName = [&](int benchmarkIndex)
  97. #define INTERNAL_CATCH_BENCHMARK_ADVANCED(BenchmarkName, name)\
  98. if( Catch::Benchmark::Benchmark BenchmarkName{name} ) \
  99. BenchmarkName = [&]
  100. #endif // TWOBLUECUBES_CATCH_BENCHMARK_HPP_INCLUDED