json_reporter.cc
Go to the documentation of this file.
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 <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 
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 
36 std::string FormatKV(std::string const& key, const char* value) {
37  return StringPrintF("\"%s\": \"%s\"", key.c_str(), value);
38 }
39 
40 std::string FormatKV(std::string const& key, bool value) {
41  return StringPrintF("\"%s\": %s", key.c_str(), value ? "true" : "false");
42 }
43 
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 
50 std::string FormatKV(std::string const& key, double value) {
51  return StringPrintF("\"%s\": %.2f", key.c_str(), value);
52 }
53 
54 int64_t RoundDouble(double v) { return static_cast<int64_t>(v + 0.5); }
55 
56 } // end namespace
57 
58 bool 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 
90 void 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 
113  // Close the list of benchmarks and the top level object.
114  GetOutputStream() << "\n ]\n}\n";
115 }
116 
117 void 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  for(auto &c : run.counters) {
158  out << ",\n"
159  << indent
160  << FormatKV(c.first, RoundDouble(c.second));
161  }
162  if (!run.report_label.empty()) {
163  out << ",\n" << indent << FormatKV("label", run.report_label);
164  }
165  out << '\n';
166 }
167 
168 } // end namespace benchmark
benchmark::JSONReporter::ReportRuns
virtual void ReportRuns(const std::vector< Run > &reports)
Definition: json_reporter.cc:90
benchmark::JSONReporter::PrintRunData
void PrintRunData(const Run &report)
Definition: json_reporter.cc:117
benchmark
Definition: benchmark.h:241
indent
static int indent(upb_textprinter *p)
Definition: php/ext/google/protobuf/upb.c:8400
benchmark::JSONReporter::first_report_
bool first_report_
Definition: benchmark.h:1168
benchmark::BenchmarkReporter::Run::report_rms
bool report_rms
Definition: benchmark.h:1070
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
benchmark::JSONReporter::Finalize
virtual void Finalize()
Definition: json_reporter.cc:112
benchmark::JSONReporter::ReportContext
virtual bool ReportContext(const Context &context)
Definition: json_reporter.cc:58
benchmark::BenchmarkReporter::Run::GetAdjustedCPUTime
double GetAdjustedCPUTime() const
Definition: reporter.cc:62
benchmark::BenchmarkReporter::Run::error_message
std::string error_message
Definition: benchmark.h:1037
benchmark::BenchmarkReporter::Run::report_label
std::string report_label
Definition: benchmark.h:1035
complexity.h
benchmark::BenchmarkReporter::Run::benchmark_name
std::string benchmark_name
Definition: benchmark.h:1034
benchmark::GetTimeUnitString
const char * GetTimeUnitString(TimeUnit unit)
Definition: benchmark.h:1184
benchmark::BenchmarkReporter::Run
Definition: benchmark.h:1017
benchmark::BenchmarkReporter::Run::items_per_second
double items_per_second
Definition: benchmark.h:1058
key
const SETUP_TEARDOWN_TESTCONTEXT char * key
Definition: test_wss_transport.cpp:10
benchmark::GetBigOString
std::string GetBigOString(BigO complexity)
Definition: complexity.cc:48
benchmark::BenchmarkReporter::Run::bytes_per_second
double bytes_per_second
Definition: benchmark.h:1057
benchmark::BenchmarkReporter::Context::mhz_per_cpu
double mhz_per_cpu
Definition: benchmark.h:1010
benchmark::BenchmarkReporter::Run::complexity
BigO complexity
Definition: benchmark.h:1064
v
const GLdouble * v
Definition: glcorearb.h:3106
timers.h
benchmark::BenchmarkReporter::Run::time_unit
TimeUnit time_unit
Definition: benchmark.h:1040
string_util.h
benchmark::BenchmarkReporter::Run::GetAdjustedRealTime
double GetAdjustedRealTime() const
Definition: reporter.cc:56
benchmark::BenchmarkReporter::Run::error_occurred
bool error_occurred
Definition: benchmark.h:1036
benchmark::StringPrintF
std::string StringPrintF(const char *format,...)
Definition: string_util.cc:155
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
benchmark::BenchmarkReporter::Context::num_cpus
int num_cpus
Definition: benchmark.h:1009
benchmark::BenchmarkReporter::Context
Definition: benchmark.h:1008
benchmark::BenchmarkReporter::GetOutputStream
std::ostream & GetOutputStream() const
Definition: benchmark.h:1114
benchmark::BenchmarkReporter::Run::report_big_o
bool report_big_o
Definition: benchmark.h:1069
benchmark::BenchmarkReporter::Run::counters
UserCounters counters
Definition: benchmark.h:1072
benchmark.h
benchmark::LocalDateTimeString
std::string LocalDateTimeString()
Definition: timers.cc:210
it
MapIter it
Definition: php/ext/google/protobuf/map.c:205
benchmark::BenchmarkReporter::Context::cpu_scaling_enabled
bool cpu_scaling_enabled
Definition: benchmark.h:1011
benchmark::BenchmarkReporter::Run::iterations
int64_t iterations
Definition: benchmark.h:1039


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:55