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