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