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