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