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