benchmark/src/console_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 <algorithm>
16 #include <cstdint>
17 #include <cstdio>
18 #include <cstring>
19 #include <iostream>
20 #include <string>
21 #include <tuple>
22 #include <vector>
23 
24 #include "benchmark/benchmark.h"
25 #include "check.h"
26 #include "colorprint.h"
27 #include "commandlineflags.h"
28 #include "complexity.h"
29 #include "counter.h"
30 #include "internal_macros.h"
31 #include "string_util.h"
32 #include "timers.h"
33 
34 namespace benchmark {
35 
37  name_field_width_ = context.name_field_width;
38  printed_header_ = false;
39  prev_counters_.clear();
40 
42 
43 #ifdef BENCHMARK_OS_WINDOWS
44  if ((output_options_ & OO_Color) && &std::cout != &GetOutputStream()) {
46  << "Color printing is only supported for stdout on windows."
47  " Disabling color printing\n";
49  }
50 #endif
51 
52  return true;
53 }
54 
56  std::string str = FormatString("%-*s %13s %15s %12s", static_cast<int>(name_field_width_),
57  "Benchmark", "Time", "CPU", "Iterations");
58  if(!run.counters.empty()) {
60  for(auto const& c : run.counters) {
61  str += FormatString(" %10s", c.first.c_str());
62  }
63  } else {
64  str += " UserCounters...";
65  }
66  }
67  std::string line = std::string(str.length(), '-');
68  GetOutputStream() << line << "\n" << str << "\n" << line << "\n";
69 }
70 
71 void ConsoleReporter::ReportRuns(const std::vector<Run>& reports) {
72  for (const auto& run : reports) {
73  // print the header:
74  // --- if none was printed yet
75  bool print_header = !printed_header_;
76  // --- or if the format is tabular and this run
77  // has different fields from the prev header
78  print_header |= (output_options_ & OO_Tabular) &&
80  if (print_header) {
81  printed_header_ = true;
82  prev_counters_ = run.counters;
84  }
85  // As an alternative to printing the headers like this, we could sort
86  // the benchmarks by header and then print. But this would require
87  // waiting for the full results before printing, or printing twice.
89  }
90 }
91 
92 static void IgnoreColorPrint(std::ostream& out, LogColor, const char* fmt,
93  ...) {
94  va_list args;
95  va_start(args, fmt);
96  out << FormatString(fmt, args);
97  va_end(args);
98 }
99 
100 
101 static std::string FormatTime(double time) {
102  // Align decimal places...
103  if (time < 1.0) {
104  return FormatString("%10.3f", time);
105  }
106  if (time < 10.0) {
107  return FormatString("%10.2f", time);
108  }
109  if (time < 100.0) {
110  return FormatString("%10.1f", time);
111  }
112  return FormatString("%10.0f", time);
113 }
114 
116  typedef void(PrinterFn)(std::ostream&, LogColor, const char*, ...);
117  auto& Out = GetOutputStream();
118  PrinterFn* printer = (output_options_ & OO_Color) ?
119  (PrinterFn*)ColorPrintf : IgnoreColorPrint;
120  auto name_color =
121  (result.report_big_o || result.report_rms) ? COLOR_BLUE : COLOR_GREEN;
122  printer(Out, name_color, "%-*s ", name_field_width_,
123  result.benchmark_name().c_str());
124 
125  if (result.error_occurred) {
126  printer(Out, COLOR_RED, "ERROR OCCURRED: \'%s\'",
127  result.error_message.c_str());
128  printer(Out, COLOR_DEFAULT, "\n");
129  return;
130  }
131 
132  const double real_time = result.GetAdjustedRealTime();
133  const double cpu_time = result.GetAdjustedCPUTime();
134  const std::string real_time_str = FormatTime(real_time);
135  const std::string cpu_time_str = FormatTime(cpu_time);
136 
137 
138  if (result.report_big_o) {
139  std::string big_o = GetBigOString(result.complexity);
140  printer(Out, COLOR_YELLOW, "%10.2f %-4s %10.2f %-4s ", real_time, big_o.c_str(),
141  cpu_time, big_o.c_str());
142  } else if (result.report_rms) {
143  printer(Out, COLOR_YELLOW, "%10.0f %-4s %10.0f %-4s ", real_time * 100, "%",
144  cpu_time * 100, "%");
145  } else if (result.run_type != Run::RT_Aggregate ||
146  result.aggregate_unit == StatisticUnit::kTime) {
147  const char* timeLabel = GetTimeUnitString(result.time_unit);
148  printer(Out, COLOR_YELLOW, "%s %-4s %s %-4s ", real_time_str.c_str(), timeLabel,
149  cpu_time_str.c_str(), timeLabel);
150  } else {
151  assert(result.aggregate_unit == StatisticUnit::kPercentage);
152  printer(Out, COLOR_YELLOW, "%10.2f %-4s %10.2f %-4s ",
153  (100. * result.real_accumulated_time), "%",
154  (100. * result.cpu_accumulated_time), "%");
155  }
156 
157  if (!result.report_big_o && !result.report_rms) {
158  printer(Out, COLOR_CYAN, "%10lld", result.iterations);
159  }
160 
161  for (auto& c : result.counters) {
162  const std::size_t cNameLen = std::max(std::string::size_type(10),
163  c.first.length());
164  std::string s;
165  const char* unit = "";
166  if (result.run_type == Run::RT_Aggregate &&
167  result.aggregate_unit == StatisticUnit::kPercentage) {
168  s = StrFormat("%.2f", 100. * c.second.value);
169  unit = "%";
170  } else {
171  s = HumanReadableNumber(c.second.value, c.second.oneK);
172  if (c.second.flags & Counter::kIsRate)
173  unit = (c.second.flags & Counter::kInvert) ? "s" : "/s";
174  }
175  if (output_options_ & OO_Tabular) {
176  printer(Out, COLOR_DEFAULT, " %*s%s", cNameLen - strlen(unit), s.c_str(),
177  unit);
178  } else {
179  printer(Out, COLOR_DEFAULT, " %s=%s%s", c.first.c_str(), s.c_str(), unit);
180  }
181  }
182 
183  if (!result.report_label.empty()) {
184  printer(Out, COLOR_DEFAULT, " %s", result.report_label.c_str());
185  }
186 
187  printer(Out, COLOR_DEFAULT, "\n");
188 }
189 
190 } // end namespace benchmark
benchmark::Counter::kInvert
@ kInvert
Definition: benchmark/include/benchmark/benchmark.h:408
xds_interop_client.str
str
Definition: xds_interop_client.py:487
benchmark::kTime
@ kTime
Definition: benchmark/include/benchmark/benchmark.h:453
benchmark::BenchmarkReporter::GetErrorStream
std::ostream & GetErrorStream() const
Definition: benchmark/include/benchmark/benchmark.h:1555
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
benchmark::BenchmarkReporter::Run::RT_Aggregate
@ RT_Aggregate
Definition: benchmark/include/benchmark/benchmark.h:1425
benchmark::Counter::kIsRate
@ kIsRate
Definition: benchmark/include/benchmark/benchmark.h:388
check.h
benchmark::kPercentage
@ kPercentage
Definition: benchmark/include/benchmark/benchmark.h:453
benchmark
Definition: bm_alarm.cc:55
timers.h
benchmark::ConsoleReporter::OO_Tabular
@ OO_Tabular
Definition: benchmark/include/benchmark/benchmark.h:1576
benchmark::LogColor
LogColor
Definition: benchmark/src/colorprint.h:9
benchmark::ConsoleReporter::output_options_
OutputOptions output_options_
Definition: benchmark/include/benchmark/benchmark.h:1593
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
benchmark::COLOR_DEFAULT
@ COLOR_DEFAULT
Definition: benchmark/src/colorprint.h:10
benchmark::ConsoleReporter::name_field_width_
size_t name_field_width_
Definition: benchmark/include/benchmark/benchmark.h:1594
colorprint.h
benchmark::ConsoleReporter::ReportRuns
virtual void ReportRuns(const std::vector< Run > &reports) BENCHMARK_OVERRIDE
Definition: benchmark/src/console_reporter.cc:71
benchmark::ConsoleReporter::PrintHeader
virtual void PrintHeader(const Run &report)
Definition: benchmark/src/console_reporter.cc:55
benchmark::GetTimeUnitString
const char * GetTimeUnitString(TimeUnit unit)
Definition: benchmark/include/benchmark/benchmark.h:1650
benchmark::ConsoleReporter::prev_counters_
UserCounters prev_counters_
Definition: benchmark/include/benchmark/benchmark.h:1595
benchmark::BenchmarkReporter::PrintBasicContext
static void PrintBasicContext(std::ostream *out, Context const &context)
Definition: benchmark/src/reporter.cc:39
benchmark::FormatString
std::string FormatString(const char *msg, va_list args)
Definition: benchmark/src/colorprint.cc:85
benchmark::ConsoleReporter::printed_header_
bool printed_header_
Definition: benchmark/include/benchmark/benchmark.h:1596
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
benchmark::ConsoleReporter::PrintRunData
virtual void PrintRunData(const Run &report)
Definition: benchmark/src/console_reporter.cc:115
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
benchmark::ConsoleReporter::OutputOptions
OutputOptions
Definition: benchmark/include/benchmark/benchmark.h:1573
benchmark::ColorPrintf
void ColorPrintf(std::ostream &out, LogColor color, const char *fmt,...)
Definition: benchmark/src/colorprint.cc:121
benchmark::HumanReadableNumber
std::string HumanReadableNumber(double n, double one_k)
Definition: benchmark/src/string_util.cc:121
benchmark::COLOR_YELLOW
@ COLOR_YELLOW
Definition: benchmark/src/colorprint.h:13
benchmark::COLOR_RED
@ COLOR_RED
Definition: benchmark/src/colorprint.h:11
counter.h
benchmark::COLOR_CYAN
@ COLOR_CYAN
Definition: benchmark/src/colorprint.h:16
benchmark::BenchmarkReporter::Run
Definition: benchmark/include/benchmark/benchmark.h:1423
benchmark::COLOR_GREEN
@ COLOR_GREEN
Definition: benchmark/src/colorprint.h:12
benchmark::GetBigOString
std::string GetBigOString(BigO complexity)
Definition: benchmark/src/complexity.cc:53
internal_macros.h
testing::internal::fmt
GTEST_API_ const char * fmt
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1808
string_util.h
benchmark::StrFormat
std::string StrFormat(const char *format,...)
Definition: benchmark/src/string_util.cc:158
benchmark::ConsoleReporter::ReportContext
virtual bool ReportContext(const Context &context) BENCHMARK_OVERRIDE
Definition: benchmark/src/console_reporter.cc:36
client.run
def run()
Definition: examples/python/async_streaming/client.py:109
benchmark::COLOR_BLUE
@ COLOR_BLUE
Definition: benchmark/src/colorprint.h:14
commandlineflags.h
regen-readme.line
line
Definition: regen-readme.py:30
benchmark::ConsoleReporter::OO_Color
@ OO_Color
Definition: benchmark/include/benchmark/benchmark.h:1575
benchmark::IgnoreColorPrint
static void IgnoreColorPrint(std::ostream &out, LogColor, const char *fmt,...)
Definition: benchmark/src/console_reporter.cc:92
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
complexity.h
benchmark::BenchmarkReporter::Context
Definition: benchmark/include/benchmark/benchmark.h:1414
benchmark::BenchmarkReporter::GetOutputStream
std::ostream & GetOutputStream() const
Definition: benchmark/include/benchmark/benchmark.h:1553
benchmark::internal::SameNames
bool SameNames(UserCounters const &l, UserCounters const &r)
Definition: benchmark/src/counter.cc:66
benchmark::FormatTime
static std::string FormatTime(double time)
Definition: benchmark/src/console_reporter.cc:101


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:01