xref: /netbsd-src/external/apache2/llvm/dist/llvm/utils/benchmark/src/statistics.cc (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1*7330f729Sjoerg // Copyright 2016 Ismael Jimenez Martinez. All rights reserved.
2*7330f729Sjoerg // Copyright 2017 Roman Lebedev. All rights reserved.
3*7330f729Sjoerg //
4*7330f729Sjoerg // Licensed under the Apache License, Version 2.0 (the "License");
5*7330f729Sjoerg // you may not use this file except in compliance with the License.
6*7330f729Sjoerg // You may obtain a copy of the License at
7*7330f729Sjoerg //
8*7330f729Sjoerg //     http://www.apache.org/licenses/LICENSE-2.0
9*7330f729Sjoerg //
10*7330f729Sjoerg // Unless required by applicable law or agreed to in writing, software
11*7330f729Sjoerg // distributed under the License is distributed on an "AS IS" BASIS,
12*7330f729Sjoerg // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*7330f729Sjoerg // See the License for the specific language governing permissions and
14*7330f729Sjoerg // limitations under the License.
15*7330f729Sjoerg 
16*7330f729Sjoerg #include "benchmark/benchmark.h"
17*7330f729Sjoerg 
18*7330f729Sjoerg #include <algorithm>
19*7330f729Sjoerg #include <cmath>
20*7330f729Sjoerg #include <string>
21*7330f729Sjoerg #include <vector>
22*7330f729Sjoerg #include <numeric>
23*7330f729Sjoerg #include "check.h"
24*7330f729Sjoerg #include "statistics.h"
25*7330f729Sjoerg 
26*7330f729Sjoerg namespace benchmark {
27*7330f729Sjoerg 
__anon6a9b19540102(const std::vector<double>& v) 28*7330f729Sjoerg auto StatisticsSum = [](const std::vector<double>& v) {
29*7330f729Sjoerg   return std::accumulate(v.begin(), v.end(), 0.0);
30*7330f729Sjoerg };
31*7330f729Sjoerg 
StatisticsMean(const std::vector<double> & v)32*7330f729Sjoerg double StatisticsMean(const std::vector<double>& v) {
33*7330f729Sjoerg   if (v.empty()) return 0.0;
34*7330f729Sjoerg   return StatisticsSum(v) * (1.0 / v.size());
35*7330f729Sjoerg }
36*7330f729Sjoerg 
StatisticsMedian(const std::vector<double> & v)37*7330f729Sjoerg double StatisticsMedian(const std::vector<double>& v) {
38*7330f729Sjoerg   if (v.size() < 3) return StatisticsMean(v);
39*7330f729Sjoerg   std::vector<double> copy(v);
40*7330f729Sjoerg 
41*7330f729Sjoerg   auto center = copy.begin() + v.size() / 2;
42*7330f729Sjoerg   std::nth_element(copy.begin(), center, copy.end());
43*7330f729Sjoerg 
44*7330f729Sjoerg   // did we have an odd number of samples?
45*7330f729Sjoerg   // if yes, then center is the median
46*7330f729Sjoerg   // it no, then we are looking for the average between center and the value before
47*7330f729Sjoerg   if(v.size() % 2 == 1)
48*7330f729Sjoerg     return *center;
49*7330f729Sjoerg   auto center2 = copy.begin() + v.size() / 2 - 1;
50*7330f729Sjoerg   std::nth_element(copy.begin(), center2, copy.end());
51*7330f729Sjoerg   return (*center + *center2) / 2.0;
52*7330f729Sjoerg }
53*7330f729Sjoerg 
54*7330f729Sjoerg // Return the sum of the squares of this sample set
__anon6a9b19540202(const std::vector<double>& v) 55*7330f729Sjoerg auto SumSquares = [](const std::vector<double>& v) {
56*7330f729Sjoerg   return std::inner_product(v.begin(), v.end(), v.begin(), 0.0);
57*7330f729Sjoerg };
58*7330f729Sjoerg 
__anon6a9b19540302(const double dat) 59*7330f729Sjoerg auto Sqr = [](const double dat) { return dat * dat; };
__anon6a9b19540402(const double dat) 60*7330f729Sjoerg auto Sqrt = [](const double dat) {
61*7330f729Sjoerg   // Avoid NaN due to imprecision in the calculations
62*7330f729Sjoerg   if (dat < 0.0) return 0.0;
63*7330f729Sjoerg   return std::sqrt(dat);
64*7330f729Sjoerg };
65*7330f729Sjoerg 
StatisticsStdDev(const std::vector<double> & v)66*7330f729Sjoerg double StatisticsStdDev(const std::vector<double>& v) {
67*7330f729Sjoerg   const auto mean = StatisticsMean(v);
68*7330f729Sjoerg   if (v.empty()) return mean;
69*7330f729Sjoerg 
70*7330f729Sjoerg   // Sample standard deviation is undefined for n = 1
71*7330f729Sjoerg   if (v.size() == 1)
72*7330f729Sjoerg     return 0.0;
73*7330f729Sjoerg 
74*7330f729Sjoerg   const double avg_squares = SumSquares(v) * (1.0 / v.size());
75*7330f729Sjoerg   return Sqrt(v.size() / (v.size() - 1.0) * (avg_squares - Sqr(mean)));
76*7330f729Sjoerg }
77*7330f729Sjoerg 
ComputeStats(const std::vector<BenchmarkReporter::Run> & reports)78*7330f729Sjoerg std::vector<BenchmarkReporter::Run> ComputeStats(
79*7330f729Sjoerg     const std::vector<BenchmarkReporter::Run>& reports) {
80*7330f729Sjoerg   typedef BenchmarkReporter::Run Run;
81*7330f729Sjoerg   std::vector<Run> results;
82*7330f729Sjoerg 
83*7330f729Sjoerg   auto error_count =
84*7330f729Sjoerg       std::count_if(reports.begin(), reports.end(),
85*7330f729Sjoerg                     [](Run const& run) { return run.error_occurred; });
86*7330f729Sjoerg 
87*7330f729Sjoerg   if (reports.size() - error_count < 2) {
88*7330f729Sjoerg     // We don't report aggregated data if there was a single run.
89*7330f729Sjoerg     return results;
90*7330f729Sjoerg   }
91*7330f729Sjoerg 
92*7330f729Sjoerg   // Accumulators.
93*7330f729Sjoerg   std::vector<double> real_accumulated_time_stat;
94*7330f729Sjoerg   std::vector<double> cpu_accumulated_time_stat;
95*7330f729Sjoerg   std::vector<double> bytes_per_second_stat;
96*7330f729Sjoerg   std::vector<double> items_per_second_stat;
97*7330f729Sjoerg 
98*7330f729Sjoerg   real_accumulated_time_stat.reserve(reports.size());
99*7330f729Sjoerg   cpu_accumulated_time_stat.reserve(reports.size());
100*7330f729Sjoerg   bytes_per_second_stat.reserve(reports.size());
101*7330f729Sjoerg   items_per_second_stat.reserve(reports.size());
102*7330f729Sjoerg 
103*7330f729Sjoerg   // All repetitions should be run with the same number of iterations so we
104*7330f729Sjoerg   // can take this information from the first benchmark.
105*7330f729Sjoerg   int64_t const run_iterations = reports.front().iterations;
106*7330f729Sjoerg   // create stats for user counters
107*7330f729Sjoerg   struct CounterStat {
108*7330f729Sjoerg     Counter c;
109*7330f729Sjoerg     std::vector<double> s;
110*7330f729Sjoerg   };
111*7330f729Sjoerg   std::map< std::string, CounterStat > counter_stats;
112*7330f729Sjoerg   for(Run const& r : reports) {
113*7330f729Sjoerg     for(auto const& cnt : r.counters) {
114*7330f729Sjoerg       auto it = counter_stats.find(cnt.first);
115*7330f729Sjoerg       if(it == counter_stats.end()) {
116*7330f729Sjoerg         counter_stats.insert({cnt.first, {cnt.second, std::vector<double>{}}});
117*7330f729Sjoerg         it = counter_stats.find(cnt.first);
118*7330f729Sjoerg         it->second.s.reserve(reports.size());
119*7330f729Sjoerg       } else {
120*7330f729Sjoerg         CHECK_EQ(counter_stats[cnt.first].c.flags, cnt.second.flags);
121*7330f729Sjoerg       }
122*7330f729Sjoerg     }
123*7330f729Sjoerg   }
124*7330f729Sjoerg 
125*7330f729Sjoerg   // Populate the accumulators.
126*7330f729Sjoerg   for (Run const& run : reports) {
127*7330f729Sjoerg     CHECK_EQ(reports[0].benchmark_name, run.benchmark_name);
128*7330f729Sjoerg     CHECK_EQ(run_iterations, run.iterations);
129*7330f729Sjoerg     if (run.error_occurred) continue;
130*7330f729Sjoerg     real_accumulated_time_stat.emplace_back(run.real_accumulated_time);
131*7330f729Sjoerg     cpu_accumulated_time_stat.emplace_back(run.cpu_accumulated_time);
132*7330f729Sjoerg     items_per_second_stat.emplace_back(run.items_per_second);
133*7330f729Sjoerg     bytes_per_second_stat.emplace_back(run.bytes_per_second);
134*7330f729Sjoerg     // user counters
135*7330f729Sjoerg     for(auto const& cnt : run.counters) {
136*7330f729Sjoerg       auto it = counter_stats.find(cnt.first);
137*7330f729Sjoerg       CHECK_NE(it, counter_stats.end());
138*7330f729Sjoerg       it->second.s.emplace_back(cnt.second);
139*7330f729Sjoerg     }
140*7330f729Sjoerg   }
141*7330f729Sjoerg 
142*7330f729Sjoerg   // Only add label if it is same for all runs
143*7330f729Sjoerg   std::string report_label = reports[0].report_label;
144*7330f729Sjoerg   for (std::size_t i = 1; i < reports.size(); i++) {
145*7330f729Sjoerg     if (reports[i].report_label != report_label) {
146*7330f729Sjoerg       report_label = "";
147*7330f729Sjoerg       break;
148*7330f729Sjoerg     }
149*7330f729Sjoerg   }
150*7330f729Sjoerg 
151*7330f729Sjoerg   for(const auto& Stat : *reports[0].statistics) {
152*7330f729Sjoerg     // Get the data from the accumulator to BenchmarkReporter::Run's.
153*7330f729Sjoerg     Run data;
154*7330f729Sjoerg     data.benchmark_name = reports[0].benchmark_name + "_" + Stat.name_;
155*7330f729Sjoerg     data.report_label = report_label;
156*7330f729Sjoerg     data.iterations = run_iterations;
157*7330f729Sjoerg 
158*7330f729Sjoerg     data.real_accumulated_time = Stat.compute_(real_accumulated_time_stat);
159*7330f729Sjoerg     data.cpu_accumulated_time = Stat.compute_(cpu_accumulated_time_stat);
160*7330f729Sjoerg     data.bytes_per_second = Stat.compute_(bytes_per_second_stat);
161*7330f729Sjoerg     data.items_per_second = Stat.compute_(items_per_second_stat);
162*7330f729Sjoerg 
163*7330f729Sjoerg     data.time_unit = reports[0].time_unit;
164*7330f729Sjoerg 
165*7330f729Sjoerg     // user counters
166*7330f729Sjoerg     for(auto const& kv : counter_stats) {
167*7330f729Sjoerg       const auto uc_stat = Stat.compute_(kv.second.s);
168*7330f729Sjoerg       auto c = Counter(uc_stat, counter_stats[kv.first].c.flags);
169*7330f729Sjoerg       data.counters[kv.first] = c;
170*7330f729Sjoerg     }
171*7330f729Sjoerg 
172*7330f729Sjoerg     results.push_back(data);
173*7330f729Sjoerg   }
174*7330f729Sjoerg 
175*7330f729Sjoerg   return results;
176*7330f729Sjoerg }
177*7330f729Sjoerg 
178*7330f729Sjoerg }  // end namespace benchmark
179