1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "benchmark/reporter.h"
16 
17 #include <cstdlib>
18 #include <vector>
19 
20 #include "check.h"
21 #include "stat.h"
22 
23 namespace benchmark {
24 
ComputeStats(const std::vector<Run> & reports,Run * mean_data,Run * stddev_data)25 void BenchmarkReporter::ComputeStats(
26     const std::vector<Run>& reports,
27     Run* mean_data, Run* stddev_data) {
28   CHECK(reports.size() >= 2) << "Cannot compute stats for less than 2 reports";
29   // Accumulators.
30   Stat1_d real_accumulated_time_stat;
31   Stat1_d cpu_accumulated_time_stat;
32   Stat1_d bytes_per_second_stat;
33   Stat1_d items_per_second_stat;
34   // All repetitions should be run with the same number of iterations so we
35   // can take this information from the first benchmark.
36   int64_t const run_iterations = reports.front().iterations;
37 
38   // Populate the accumulators.
39   for (Run const& run : reports) {
40     CHECK_EQ(reports[0].benchmark_name, run.benchmark_name);
41     CHECK_EQ(run_iterations, run.iterations);
42     real_accumulated_time_stat +=
43         Stat1_d(run.real_accumulated_time/run.iterations, run.iterations);
44     cpu_accumulated_time_stat +=
45         Stat1_d(run.cpu_accumulated_time/run.iterations, run.iterations);
46     items_per_second_stat += Stat1_d(run.items_per_second, run.iterations);
47     bytes_per_second_stat += Stat1_d(run.bytes_per_second, run.iterations);
48   }
49 
50   // Get the data from the accumulator to BenchmarkReporter::Run's.
51   mean_data->benchmark_name = reports[0].benchmark_name + "_mean";
52   mean_data->iterations = run_iterations;
53   mean_data->real_accumulated_time = real_accumulated_time_stat.Mean() *
54                                      run_iterations;
55   mean_data->cpu_accumulated_time = cpu_accumulated_time_stat.Mean() *
56                                     run_iterations;
57   mean_data->bytes_per_second = bytes_per_second_stat.Mean();
58   mean_data->items_per_second = items_per_second_stat.Mean();
59 
60   // Only add label to mean/stddev if it is same for all runs
61   mean_data->report_label = reports[0].report_label;
62   for (std::size_t i = 1; i < reports.size(); i++) {
63     if (reports[i].report_label != reports[0].report_label) {
64       mean_data->report_label = "";
65       break;
66     }
67   }
68 
69   stddev_data->benchmark_name = reports[0].benchmark_name + "_stddev";
70   stddev_data->report_label = mean_data->report_label;
71   stddev_data->iterations = 0;
72   stddev_data->real_accumulated_time =
73       real_accumulated_time_stat.StdDev();
74   stddev_data->cpu_accumulated_time =
75       cpu_accumulated_time_stat.StdDev();
76   stddev_data->bytes_per_second = bytes_per_second_stat.StdDev();
77   stddev_data->items_per_second = items_per_second_stat.StdDev();
78 }
79 
Finalize()80 void BenchmarkReporter::Finalize() {
81 }
82 
~BenchmarkReporter()83 BenchmarkReporter::~BenchmarkReporter() {
84 }
85 
86 } // end namespace benchmark
87