protobuf/third_party/benchmark/src/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 
112 void JSONReporter::Finalize() {
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
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
regen-readme.it
it
Definition: regen-readme.py:15
benchmark::JSONReporter::PrintRunData
void PrintRunData(const Run &report)
Definition: benchmark/src/json_reporter.cc:228
benchmark
Definition: bm_alarm.cc:55
fix_build_deps.c
list c
Definition: fix_build_deps.py:490
benchmark::JSONReporter::first_report_
bool first_report_
Definition: benchmark/include/benchmark/benchmark.h:1609
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
benchmark::JSONReporter::Finalize
virtual void Finalize() BENCHMARK_OVERRIDE
Definition: benchmark/src/json_reporter.cc:223
python_utils.upload_rbe_results.indent
indent
Definition: upload_rbe_results.py:183
benchmark::GetTimeUnitString
const char * GetTimeUnitString(TimeUnit unit)
Definition: benchmark/include/benchmark/benchmark.h:1650
timers.h
complexity.h
benchmark::JSONReporter::ReportContext
virtual bool ReportContext(const Context &context) BENCHMARK_OVERRIDE
Definition: benchmark/src/json_reporter.cc:120
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
benchmark::JSONReporter::ReportRuns
virtual void ReportRuns(const std::vector< Run > &reports) BENCHMARK_OVERRIDE
Definition: benchmark/src/json_reporter.cc:201
benchmark::GetBigOString
std::string GetBigOString(BigO complexity)
Definition: benchmark/src/complexity.cc:53
value
const char * value
Definition: hpack_parser_table.cc:165
string_util.h
key
const char * key
Definition: hpack_parser_table.cc:164
client.run
def run()
Definition: examples/python/async_streaming/client.py:109
benchmark::StringPrintF
std::string StringPrintF(const char *format,...)
Definition: bloaty/third_party/protobuf/third_party/benchmark/src/string_util.cc:155
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
googletest-break-on-failure-unittest.Run
def Run(command)
Definition: bloaty/third_party/googletest/googletest/test/googletest-break-on-failure-unittest.py:76
benchmark::BenchmarkReporter::GetOutputStream
std::ostream & GetOutputStream() const
Definition: benchmark/include/benchmark/benchmark.h:1553
benchmark::LocalDateTimeString
std::string LocalDateTimeString()
Definition: benchmark/src/timers.cc:182


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:12