| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "statistics.h" |
|
|
| #include <algorithm> |
| #include <cmath> |
| #include <numeric> |
| #include <string> |
| #include <vector> |
|
|
| #include "benchmark/benchmark.h" |
| #include "check.h" |
|
|
| namespace benchmark { |
|
|
| auto StatisticsSum = [](const std::vector<double>& v) { |
| return std::accumulate(v.begin(), v.end(), 0.0); |
| }; |
|
|
| double StatisticsMean(const std::vector<double>& v) { |
| if (v.empty()) return 0.0; |
| return StatisticsSum(v) * (1.0 / v.size()); |
| } |
|
|
| double StatisticsMedian(const std::vector<double>& v) { |
| if (v.size() < 3) return StatisticsMean(v); |
| std::vector<double> copy(v); |
|
|
| auto center = copy.begin() + v.size() / 2; |
| std::nth_element(copy.begin(), center, copy.end()); |
|
|
| |
| |
| |
| |
| if (v.size() % 2 == 1) return *center; |
| auto center2 = copy.begin() + v.size() / 2 - 1; |
| std::nth_element(copy.begin(), center2, copy.end()); |
| return (*center + *center2) / 2.0; |
| } |
|
|
| |
| auto SumSquares = [](const std::vector<double>& v) { |
| return std::inner_product(v.begin(), v.end(), v.begin(), 0.0); |
| }; |
|
|
| auto Sqr = [](const double dat) { return dat * dat; }; |
| auto Sqrt = [](const double dat) { |
| |
| if (dat < 0.0) return 0.0; |
| return std::sqrt(dat); |
| }; |
|
|
| double StatisticsStdDev(const std::vector<double>& v) { |
| const auto mean = StatisticsMean(v); |
| if (v.empty()) return mean; |
|
|
| |
| if (v.size() == 1) return 0.0; |
|
|
| const double avg_squares = SumSquares(v) * (1.0 / v.size()); |
| return Sqrt(v.size() / (v.size() - 1.0) * (avg_squares - Sqr(mean))); |
| } |
|
|
| double StatisticsCV(const std::vector<double>& v) { |
| if (v.size() < 2) return 0.0; |
|
|
| const auto stddev = StatisticsStdDev(v); |
| const auto mean = StatisticsMean(v); |
|
|
| return stddev / mean; |
| } |
|
|
| std::vector<BenchmarkReporter::Run> ComputeStats( |
| const std::vector<BenchmarkReporter::Run>& reports) { |
| typedef BenchmarkReporter::Run Run; |
| std::vector<Run> results; |
|
|
| auto error_count = std::count_if(reports.begin(), reports.end(), |
| [](Run const& run) { return run.skipped; }); |
|
|
| if (reports.size() - error_count < 2) { |
| |
| return results; |
| } |
|
|
| |
| std::vector<double> real_accumulated_time_stat; |
| std::vector<double> cpu_accumulated_time_stat; |
|
|
| real_accumulated_time_stat.reserve(reports.size()); |
| cpu_accumulated_time_stat.reserve(reports.size()); |
|
|
| |
| |
| const IterationCount run_iterations = reports.front().iterations; |
| |
| struct CounterStat { |
| Counter c; |
| std::vector<double> s; |
| }; |
| std::map<std::string, CounterStat> counter_stats; |
| for (Run const& r : reports) { |
| for (auto const& cnt : r.counters) { |
| auto it = counter_stats.find(cnt.first); |
| if (it == counter_stats.end()) { |
| it = counter_stats |
| .emplace(cnt.first, |
| CounterStat{cnt.second, std::vector<double>{}}) |
| .first; |
| it->second.s.reserve(reports.size()); |
| } else { |
| BM_CHECK_EQ(it->second.c.flags, cnt.second.flags); |
| } |
| } |
| } |
|
|
| |
| for (Run const& run : reports) { |
| BM_CHECK_EQ(reports[0].benchmark_name(), run.benchmark_name()); |
| BM_CHECK_EQ(run_iterations, run.iterations); |
| if (run.skipped) continue; |
| real_accumulated_time_stat.emplace_back(run.real_accumulated_time); |
| cpu_accumulated_time_stat.emplace_back(run.cpu_accumulated_time); |
| |
| for (auto const& cnt : run.counters) { |
| auto it = counter_stats.find(cnt.first); |
| BM_CHECK_NE(it, counter_stats.end()); |
| it->second.s.emplace_back(cnt.second); |
| } |
| } |
|
|
| |
| std::string report_label = reports[0].report_label; |
| for (std::size_t i = 1; i < reports.size(); i++) { |
| if (reports[i].report_label != report_label) { |
| report_label = ""; |
| break; |
| } |
| } |
|
|
| const double iteration_rescale_factor = |
| double(reports.size()) / double(run_iterations); |
|
|
| for (const auto& Stat : *reports[0].statistics) { |
| |
| Run data; |
| data.run_name = reports[0].run_name; |
| data.family_index = reports[0].family_index; |
| data.per_family_instance_index = reports[0].per_family_instance_index; |
| data.run_type = BenchmarkReporter::Run::RT_Aggregate; |
| data.threads = reports[0].threads; |
| data.repetitions = reports[0].repetitions; |
| data.repetition_index = Run::no_repetition_index; |
| data.aggregate_name = Stat.name_; |
| data.aggregate_unit = Stat.unit_; |
| data.report_label = report_label; |
|
|
| |
| |
| |
| |
| |
| data.iterations = reports.size(); |
|
|
| data.real_accumulated_time = Stat.compute_(real_accumulated_time_stat); |
| data.cpu_accumulated_time = Stat.compute_(cpu_accumulated_time_stat); |
|
|
| if (data.aggregate_unit == StatisticUnit::kTime) { |
| |
| |
| |
| |
| |
| data.real_accumulated_time *= iteration_rescale_factor; |
| data.cpu_accumulated_time *= iteration_rescale_factor; |
| } |
|
|
| data.time_unit = reports[0].time_unit; |
|
|
| |
| for (auto const& kv : counter_stats) { |
| |
| const auto uc_stat = Stat.compute_(kv.second.s); |
| auto c = Counter(uc_stat, counter_stats[kv.first].c.flags, |
| counter_stats[kv.first].c.oneK); |
| data.counters[kv.first] = c; |
| } |
|
|
| results.push_back(data); |
| } |
|
|
| return results; |
| } |
|
|
| } |
|
|