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 <cstdint>
18 #include <iostream>
19 #include <string>
20 #include <vector>
21 
22 #include "string_util.h"
23 #include "walltime.h"
24 
25 namespace benchmark {
26 
27 namespace {
28 
FormatKV(std::string const & key,std::string const & value)29 std::string FormatKV(std::string const& key, std::string const& value) {
30   return StringPrintF("\"%s\": \"%s\"", key.c_str(), value.c_str());
31 }
32 
FormatKV(std::string const & key,const char * value)33 std::string FormatKV(std::string const& key, const char* value) {
34   return StringPrintF("\"%s\": \"%s\"", key.c_str(), value);
35 }
36 
FormatKV(std::string const & key,bool value)37 std::string FormatKV(std::string const& key, bool value) {
38   return StringPrintF("\"%s\": %s", key.c_str(), value ? "true" : "false");
39 }
40 
FormatKV(std::string const & key,int64_t value)41 std::string FormatKV(std::string const& key, int64_t value) {
42   std::stringstream ss;
43   ss << '"' << key << "\": " << value;
44   return ss.str();
45 }
46 
RoundDouble(double v)47 int64_t RoundDouble(double v) {
48     return static_cast<int64_t>(v + 0.5);
49 }
50 
51 } // end namespace
52 
ReportContext(const Context & context)53 bool JSONReporter::ReportContext(const Context& context) {
54   std::ostream& out = std::cout;
55 
56   out << "{\n";
57   std::string inner_indent(2, ' ');
58 
59   // Open context block and print context information.
60   out << inner_indent << "\"context\": {\n";
61   std::string indent(4, ' ');
62 
63   std::string walltime_value = LocalDateTimeString();
64   out << indent << FormatKV("date", walltime_value) << ",\n";
65 
66   out << indent
67       << FormatKV("num_cpus", static_cast<int64_t>(context.num_cpus))
68       << ",\n";
69   out << indent
70       << FormatKV("mhz_per_cpu", RoundDouble(context.mhz_per_cpu))
71       << ",\n";
72   out << indent
73       << FormatKV("cpu_scaling_enabled", context.cpu_scaling_enabled)
74       << ",\n";
75 
76 #if defined(NDEBUG)
77   const char build_type[] = "release";
78 #else
79   const char build_type[] = "debug";
80 #endif
81   out << indent << FormatKV("library_build_type", build_type) << "\n";
82   // Close context block and open the list of benchmarks.
83   out << inner_indent << "},\n";
84   out << inner_indent << "\"benchmarks\": [\n";
85   return true;
86 }
87 
ReportRuns(std::vector<Run> const & reports)88 void JSONReporter::ReportRuns(std::vector<Run> const& reports) {
89   if (reports.empty()) {
90     return;
91   }
92   std::string indent(4, ' ');
93   std::ostream& out = std::cout;
94   if (!first_report_) {
95     out << ",\n";
96   }
97   first_report_ = false;
98   std::vector<Run> reports_cp = reports;
99   if (reports.size() >= 2) {
100     Run mean_data;
101     Run stddev_data;
102     BenchmarkReporter::ComputeStats(reports, &mean_data, &stddev_data);
103     reports_cp.push_back(mean_data);
104     reports_cp.push_back(stddev_data);
105   }
106   for (auto it = reports_cp.begin(); it != reports_cp.end(); ++it) {
107      out << indent << "{\n";
108      PrintRunData(*it);
109      out << indent << '}';
110      auto it_cp = it;
111      if (++it_cp != reports_cp.end()) {
112          out << ",\n";
113      }
114   }
115 }
116 
Finalize()117 void JSONReporter::Finalize() {
118     // Close the list of benchmarks and the top level object.
119     std::cout << "\n  ]\n}\n";
120 }
121 
PrintRunData(Run const & run)122 void JSONReporter::PrintRunData(Run const& run) {
123     double const multiplier = 1e9; // nano second multiplier
124     double cpu_time = run.cpu_accumulated_time * multiplier;
125     double real_time = run.real_accumulated_time * multiplier;
126     if (run.iterations != 0) {
127         real_time = real_time / static_cast<double>(run.iterations);
128         cpu_time = cpu_time / static_cast<double>(run.iterations);
129     }
130 
131     std::string indent(6, ' ');
132     std::ostream& out = std::cout;
133     out << indent
134         << FormatKV("name", run.benchmark_name)
135         << ",\n";
136     out << indent
137         << FormatKV("iterations", run.iterations)
138         << ",\n";
139     out << indent
140         << FormatKV("real_time", RoundDouble(real_time))
141         << ",\n";
142     out << indent
143         << FormatKV("cpu_time", RoundDouble(cpu_time));
144     if (run.bytes_per_second > 0.0) {
145         out << ",\n" << indent
146             << FormatKV("bytes_per_second", RoundDouble(run.bytes_per_second));
147     }
148     if (run.items_per_second > 0.0) {
149         out << ",\n" << indent
150             << FormatKV("items_per_second", RoundDouble(run.items_per_second));
151     }
152     if (!run.report_label.empty()) {
153         out << ",\n" << indent
154             << FormatKV("label", run.report_label);
155     }
156     out << '\n';
157 }
158 
159 } // end namespace benchmark
160