catch_execution_plan.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // Execution plan
  9. #ifndef TWOBLUECUBES_CATCH_EXECUTION_PLAN_HPP_INCLUDED
  10. #define TWOBLUECUBES_CATCH_EXECUTION_PLAN_HPP_INCLUDED
  11. #include "../catch_config.hpp"
  12. #include "catch_clock.hpp"
  13. #include "catch_environment.hpp"
  14. #include "detail/catch_benchmark_function.hpp"
  15. #include "detail/catch_repeat.hpp"
  16. #include "detail/catch_run_for_at_least.hpp"
  17. #include <algorithm>
  18. namespace Catch {
  19. namespace Benchmark {
  20. template <typename Duration>
  21. struct ExecutionPlan {
  22. int iterations_per_sample;
  23. Duration estimated_duration;
  24. Detail::BenchmarkFunction benchmark;
  25. Duration warmup_time;
  26. int warmup_iterations;
  27. template <typename Duration2>
  28. operator ExecutionPlan<Duration2>() const {
  29. return { iterations_per_sample, estimated_duration, benchmark, warmup_time, warmup_iterations };
  30. }
  31. template <typename Clock>
  32. std::vector<FloatDuration<Clock>> run(const IConfig &cfg, Environment<FloatDuration<Clock>> env) const {
  33. // warmup a bit
  34. Detail::run_for_at_least<Clock>(std::chrono::duration_cast<ClockDuration<Clock>>(warmup_time), warmup_iterations, Detail::repeat(now<Clock>{}));
  35. std::vector<FloatDuration<Clock>> times;
  36. times.reserve(cfg.benchmarkSamples());
  37. std::generate_n(std::back_inserter(times), cfg.benchmarkSamples(), [this, env] {
  38. Detail::ChronometerModel<Clock> model;
  39. this->benchmark(Chronometer(model, iterations_per_sample));
  40. auto sample_time = model.elapsed() - env.clock_cost.mean;
  41. if (sample_time < FloatDuration<Clock>::zero()) sample_time = FloatDuration<Clock>::zero();
  42. return sample_time / iterations_per_sample;
  43. });
  44. return times;
  45. }
  46. };
  47. } // namespace Benchmark
  48. } // namespace Catch
  49. #endif // TWOBLUECUBES_CATCH_EXECUTION_PLAN_HPP_INCLUDED