File indexing completed on 2025-01-30 10:02:48
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef CATCH_EXECUTION_PLAN_HPP_INCLUDED
0011 #define CATCH_EXECUTION_PLAN_HPP_INCLUDED
0012
0013 #include <catch2/interfaces/catch_interfaces_config.hpp>
0014 #include <catch2/benchmark/catch_clock.hpp>
0015 #include <catch2/benchmark/catch_environment.hpp>
0016 #include <catch2/benchmark/detail/catch_benchmark_function.hpp>
0017 #include <catch2/benchmark/detail/catch_repeat.hpp>
0018 #include <catch2/benchmark/detail/catch_run_for_at_least.hpp>
0019
0020 #include <vector>
0021
0022 namespace Catch {
0023 namespace Benchmark {
0024 template <typename Duration>
0025 struct ExecutionPlan {
0026 int iterations_per_sample;
0027 Duration estimated_duration;
0028 Detail::BenchmarkFunction benchmark;
0029 Duration warmup_time;
0030 int warmup_iterations;
0031
0032 template <typename Duration2>
0033 operator ExecutionPlan<Duration2>() const {
0034 return { iterations_per_sample, estimated_duration, benchmark, warmup_time, warmup_iterations };
0035 }
0036
0037 template <typename Clock>
0038 std::vector<FloatDuration<Clock>> run(const IConfig &cfg, Environment<FloatDuration<Clock>> env) const {
0039
0040 Detail::run_for_at_least<Clock>(std::chrono::duration_cast<ClockDuration<Clock>>(warmup_time), warmup_iterations, Detail::repeat(now<Clock>{}));
0041
0042 std::vector<FloatDuration<Clock>> times;
0043 const auto num_samples = cfg.benchmarkSamples();
0044 times.reserve( num_samples );
0045 for ( size_t i = 0; i < num_samples; ++i ) {
0046 Detail::ChronometerModel<Clock> model;
0047 this->benchmark( Chronometer( model, iterations_per_sample ) );
0048 auto sample_time = model.elapsed() - env.clock_cost.mean;
0049 if ( sample_time < FloatDuration<Clock>::zero() ) {
0050 sample_time = FloatDuration<Clock>::zero();
0051 }
0052 times.push_back(sample_time / iterations_per_sample);
0053 }
0054 return times;
0055 }
0056 };
0057 }
0058 }
0059
0060 #endif