catch_analyse.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // Run and analyse one benchmark
  9. #ifndef TWOBLUECUBES_CATCH_DETAIL_ANALYSE_HPP_INCLUDED
  10. #define TWOBLUECUBES_CATCH_DETAIL_ANALYSE_HPP_INCLUDED
  11. #include "../catch_clock.hpp"
  12. #include "../catch_sample_analysis.hpp"
  13. #include "catch_stats.hpp"
  14. #include <algorithm>
  15. #include <iterator>
  16. #include <vector>
  17. namespace Catch {
  18. namespace Benchmark {
  19. namespace Detail {
  20. template <typename Duration, typename Iterator>
  21. SampleAnalysis<Duration> analyse(const IConfig &cfg, Environment<Duration>, Iterator first, Iterator last) {
  22. if (!cfg.benchmarkNoAnalysis()) {
  23. std::vector<double> samples;
  24. samples.reserve(last - first);
  25. std::transform(first, last, std::back_inserter(samples), [](Duration d) { return d.count(); });
  26. auto analysis = Catch::Benchmark::Detail::analyse_samples(cfg.benchmarkConfidenceInterval(), cfg.benchmarkResamples(), samples.begin(), samples.end());
  27. auto outliers = Catch::Benchmark::Detail::classify_outliers(samples.begin(), samples.end());
  28. auto wrap_estimate = [](Estimate<double> e) {
  29. return Estimate<Duration> {
  30. Duration(e.point),
  31. Duration(e.lower_bound),
  32. Duration(e.upper_bound),
  33. e.confidence_interval,
  34. };
  35. };
  36. std::vector<Duration> samples2;
  37. samples2.reserve(samples.size());
  38. std::transform(samples.begin(), samples.end(), std::back_inserter(samples2), [](double d) { return Duration(d); });
  39. return {
  40. std::move(samples2),
  41. wrap_estimate(analysis.mean),
  42. wrap_estimate(analysis.standard_deviation),
  43. outliers,
  44. analysis.outlier_variance,
  45. };
  46. } else {
  47. std::vector<Duration> samples;
  48. samples.reserve(last - first);
  49. Duration mean = Duration(0);
  50. int i = 0;
  51. for (auto it = first; it < last; ++it, ++i) {
  52. samples.push_back(Duration(*it));
  53. mean += Duration(*it);
  54. }
  55. mean /= i;
  56. return {
  57. std::move(samples),
  58. Estimate<Duration>{mean, mean, mean, 0.0},
  59. Estimate<Duration>{Duration(0), Duration(0), Duration(0), 0.0},
  60. OutlierClassification{},
  61. 0.0
  62. };
  63. }
  64. }
  65. } // namespace Detail
  66. } // namespace Benchmark
  67. } // namespace Catch
  68. #endif // TWOBLUECUBES_CATCH_DETAIL_ANALYSE_HPP_INCLUDED