Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:52:11

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
0008 // Adapted from donated nonius code.
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         struct ExecutionPlan {
0025             int iterations_per_sample;
0026             FDuration estimated_duration;
0027             Detail::BenchmarkFunction benchmark;
0028             FDuration warmup_time;
0029             int warmup_iterations;
0030 
0031             template <typename Clock>
0032             std::vector<FDuration> run(const IConfig &cfg, Environment env) const {
0033                 // warmup a bit
0034                 Detail::run_for_at_least<Clock>(
0035                     std::chrono::duration_cast<IDuration>( warmup_time ),
0036                     warmup_iterations,
0037                     Detail::repeat( []() { return Clock::now(); } )
0038                 );
0039 
0040                 std::vector<FDuration> times;
0041                 const auto num_samples = cfg.benchmarkSamples();
0042                 times.reserve( num_samples );
0043                 for ( size_t i = 0; i < num_samples; ++i ) {
0044                     Detail::ChronometerModel<Clock> model;
0045                     this->benchmark( Chronometer( model, iterations_per_sample ) );
0046                     auto sample_time = model.elapsed() - env.clock_cost.mean;
0047                     if ( sample_time < FDuration::zero() ) {
0048                         sample_time = FDuration::zero();
0049                     }
0050                     times.push_back(sample_time / iterations_per_sample);
0051                 }
0052                 return times;
0053             }
0054         };
0055     } // namespace Benchmark
0056 } // namespace Catch
0057 
0058 #endif // CATCH_EXECUTION_PLAN_HPP_INCLUDED