| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include "benchmark/benchmark.h" |
| |
|
| | #include <algorithm> |
| | #include <cmath> |
| | #include <numeric> |
| | #include <string> |
| | #include <vector> |
| | #include "check.h" |
| | #include "statistics.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))); |
| | } |
| |
|
| | 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.error_occurred; }); |
| |
|
| | 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()); |
| |
|
| | |
| | |
| | int64_t const 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()) { |
| | counter_stats.insert({cnt.first, {cnt.second, std::vector<double>{}}}); |
| | it = counter_stats.find(cnt.first); |
| | it->second.s.reserve(reports.size()); |
| | } else { |
| | CHECK_EQ(counter_stats[cnt.first].c.flags, cnt.second.flags); |
| | } |
| | } |
| | } |
| |
|
| | |
| | for (Run const& run : reports) { |
| | CHECK_EQ(reports[0].benchmark_name(), run.benchmark_name()); |
| | CHECK_EQ(run_iterations, run.iterations); |
| | if (run.error_occurred) 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); |
| | 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].benchmark_name(); |
| | data.run_type = BenchmarkReporter::Run::RT_Aggregate; |
| | data.aggregate_name = Stat.name_; |
| | 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); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | 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; |
| | } |
| |
|
| | } |
| |
|