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/benchmark.h"
16 #include "complexity.h"
17 
18 #include <algorithm>
19 #include <cmath>
20 #include <cstdint>
21 #include <iomanip>  // for setprecision
22 #include <iostream>
23 #include <limits>
24 #include <string>
25 #include <tuple>
26 #include <vector>
27 
28 #include "string_util.h"
29 #include "timers.h"
30 
31 namespace benchmark {
32 
33 namespace {
34 
StrEscape(const std::string & s)35 std::string StrEscape(const std::string & s) {
36   std::string tmp;
37   tmp.reserve(s.size());
38   for (char c : s) {
39     switch (c) {
40     case '\b': tmp += "\\b"; break;
41     case '\f': tmp += "\\f"; break;
42     case '\n': tmp += "\\n"; break;
43     case '\r': tmp += "\\r"; break;
44     case '\t': tmp += "\\t"; break;
45     case '\\': tmp += "\\\\"; break;
46     case '"' : tmp += "\\\""; break;
47     default  : tmp += c; break;
48     }
49   }
50   return tmp;
51 }
52 
FormatKV(std::string const & key,std::string const & value)53 std::string FormatKV(std::string const& key, std::string const& value) {
54   return StrFormat("\"%s\": \"%s\"", StrEscape(key).c_str(), StrEscape(value).c_str());
55 }
56 
FormatKV(std::string const & key,const char * value)57 std::string FormatKV(std::string const& key, const char* value) {
58   return StrFormat("\"%s\": \"%s\"", StrEscape(key).c_str(), StrEscape(value).c_str());
59 }
60 
FormatKV(std::string const & key,bool value)61 std::string FormatKV(std::string const& key, bool value) {
62   return StrFormat("\"%s\": %s", StrEscape(key).c_str(), value ? "true" : "false");
63 }
64 
FormatKV(std::string const & key,int64_t value)65 std::string FormatKV(std::string const& key, int64_t value) {
66   std::stringstream ss;
67   ss << '"' << StrEscape(key) << "\": " << value;
68   return ss.str();
69 }
70 
FormatKV(std::string const & key,IterationCount value)71 std::string FormatKV(std::string const& key, IterationCount value) {
72   std::stringstream ss;
73   ss << '"' << StrEscape(key) << "\": " << value;
74   return ss.str();
75 }
76 
FormatKV(std::string const & key,double value)77 std::string FormatKV(std::string const& key, double value) {
78   std::stringstream ss;
79   ss << '"' << StrEscape(key) << "\": ";
80 
81   if (std::isnan(value))
82     ss << (value < 0 ? "-" : "") << "NaN";
83   else if (std::isinf(value))
84     ss << (value < 0 ? "-" : "") << "Infinity";
85   else {
86     const auto max_digits10 =
87         std::numeric_limits<decltype(value)>::max_digits10;
88     const auto max_fractional_digits10 = max_digits10 - 1;
89     ss << std::scientific << std::setprecision(max_fractional_digits10)
90        << value;
91   }
92   return ss.str();
93 }
94 
RoundDouble(double v)95 int64_t RoundDouble(double v) { return std::lround(v); }
96 
97 }  // end namespace
98 
ReportContext(const Context & context)99 bool JSONReporter::ReportContext(const Context& context) {
100   std::ostream& out = GetOutputStream();
101 
102   out << "{\n";
103   std::string inner_indent(2, ' ');
104 
105   // Open context block and print context information.
106   out << inner_indent << "\"context\": {\n";
107   std::string indent(4, ' ');
108 
109   std::string walltime_value = LocalDateTimeString();
110   out << indent << FormatKV("date", walltime_value) << ",\n";
111 
112   out << indent << FormatKV("host_name", context.sys_info.name) << ",\n";
113 
114   if (Context::executable_name) {
115     out << indent << FormatKV("executable", Context::executable_name) << ",\n";
116   }
117 
118   CPUInfo const& info = context.cpu_info;
119   out << indent << FormatKV("num_cpus", static_cast<int64_t>(info.num_cpus))
120       << ",\n";
121   out << indent
122       << FormatKV("mhz_per_cpu",
123                   RoundDouble(info.cycles_per_second / 1000000.0))
124       << ",\n";
125   if (CPUInfo::Scaling::UNKNOWN != info.scaling) {
126     out << indent << FormatKV("cpu_scaling_enabled", info.scaling == CPUInfo::Scaling::ENABLED ? true : false)
127         << ",\n";
128   }
129 
130   out << indent << "\"caches\": [\n";
131   indent = std::string(6, ' ');
132   std::string cache_indent(8, ' ');
133   for (size_t i = 0; i < info.caches.size(); ++i) {
134     auto& CI = info.caches[i];
135     out << indent << "{\n";
136     out << cache_indent << FormatKV("type", CI.type) << ",\n";
137     out << cache_indent << FormatKV("level", static_cast<int64_t>(CI.level))
138         << ",\n";
139     out << cache_indent
140         << FormatKV("size", static_cast<int64_t>(CI.size)) << ",\n";
141     out << cache_indent
142         << FormatKV("num_sharing", static_cast<int64_t>(CI.num_sharing))
143         << "\n";
144     out << indent << "}";
145     if (i != info.caches.size() - 1) out << ",";
146     out << "\n";
147   }
148   indent = std::string(4, ' ');
149   out << indent << "],\n";
150   out << indent << "\"load_avg\": [";
151   for (auto it = info.load_avg.begin(); it != info.load_avg.end();) {
152     out << *it++;
153     if (it != info.load_avg.end()) out << ",";
154   }
155   out << "],\n";
156 
157 #if defined(NDEBUG)
158   const char build_type[] = "release";
159 #else
160   const char build_type[] = "debug";
161 #endif
162   out << indent << FormatKV("library_build_type", build_type) << "\n";
163   // Close context block and open the list of benchmarks.
164   out << inner_indent << "},\n";
165   out << inner_indent << "\"benchmarks\": [\n";
166   return true;
167 }
168 
ReportRuns(std::vector<Run> const & reports)169 void JSONReporter::ReportRuns(std::vector<Run> const& reports) {
170   if (reports.empty()) {
171     return;
172   }
173   std::string indent(4, ' ');
174   std::ostream& out = GetOutputStream();
175   if (!first_report_) {
176     out << ",\n";
177   }
178   first_report_ = false;
179 
180   for (auto it = reports.begin(); it != reports.end(); ++it) {
181     out << indent << "{\n";
182     PrintRunData(*it);
183     out << indent << '}';
184     auto it_cp = it;
185     if (++it_cp != reports.end()) {
186       out << ",\n";
187     }
188   }
189 }
190 
Finalize()191 void JSONReporter::Finalize() {
192   // Close the list of benchmarks and the top level object.
193   GetOutputStream() << "\n  ]\n}\n";
194 }
195 
PrintRunData(Run const & run)196 void JSONReporter::PrintRunData(Run const& run) {
197   std::string indent(6, ' ');
198   std::ostream& out = GetOutputStream();
199   out << indent << FormatKV("name", run.benchmark_name()) << ",\n";
200   out << indent << FormatKV("run_name", run.run_name.str()) << ",\n";
201   out << indent << FormatKV("run_type", [&run]() -> const char* {
202     switch (run.run_type) {
203       case BenchmarkReporter::Run::RT_Iteration:
204         return "iteration";
205       case BenchmarkReporter::Run::RT_Aggregate:
206         return "aggregate";
207     }
208     BENCHMARK_UNREACHABLE();
209   }()) << ",\n";
210   out << indent << FormatKV("repetitions", run.repetitions) << ",\n";
211   if (run.run_type != BenchmarkReporter::Run::RT_Aggregate) {
212     out << indent << FormatKV("repetition_index", run.repetition_index)
213         << ",\n";
214   }
215   out << indent << FormatKV("threads", run.threads) << ",\n";
216   if (run.run_type == BenchmarkReporter::Run::RT_Aggregate) {
217     out << indent << FormatKV("aggregate_name", run.aggregate_name) << ",\n";
218   }
219   if (run.error_occurred) {
220     out << indent << FormatKV("error_occurred", run.error_occurred) << ",\n";
221     out << indent << FormatKV("error_message", run.error_message) << ",\n";
222   }
223   if (!run.report_big_o && !run.report_rms) {
224     out << indent << FormatKV("iterations", run.iterations) << ",\n";
225     out << indent << FormatKV("real_time", run.GetAdjustedRealTime()) << ",\n";
226     out << indent << FormatKV("cpu_time", run.GetAdjustedCPUTime());
227     out << ",\n"
228         << indent << FormatKV("time_unit", GetTimeUnitString(run.time_unit));
229   } else if (run.report_big_o) {
230     out << indent << FormatKV("cpu_coefficient", run.GetAdjustedCPUTime())
231         << ",\n";
232     out << indent << FormatKV("real_coefficient", run.GetAdjustedRealTime())
233         << ",\n";
234     out << indent << FormatKV("big_o", GetBigOString(run.complexity)) << ",\n";
235     out << indent << FormatKV("time_unit", GetTimeUnitString(run.time_unit));
236   } else if (run.report_rms) {
237     out << indent << FormatKV("rms", run.GetAdjustedCPUTime());
238   }
239 
240   for (auto& c : run.counters) {
241     out << ",\n" << indent << FormatKV(c.first, c.second);
242   }
243 
244   if (run.has_memory_result) {
245     out << ",\n" << indent << FormatKV("allocs_per_iter", run.allocs_per_iter);
246     out << ",\n" << indent << FormatKV("max_bytes_used", run.max_bytes_used);
247   }
248 
249   if (!run.report_label.empty()) {
250     out << ",\n" << indent << FormatKV("label", run.report_label);
251   }
252   out << '\n';
253 }
254 
255 }  // end namespace benchmark
256