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