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 "benchmark/benchmark.h"
16 #include "complexity.h"
17 #include "counter.h"
18 
19 #include <algorithm>
20 #include <cstdint>
21 #include <cstdio>
22 #include <iostream>
23 #include <string>
24 #include <tuple>
25 #include <vector>
26 
27 #include "check.h"
28 #include "colorprint.h"
29 #include "commandlineflags.h"
30 #include "internal_macros.h"
31 #include "string_util.h"
32 #include "timers.h"
33 
34 namespace benchmark {
35 
38  printed_header_ = false;
39  prev_counters_.clear();
40 
41  PrintBasicContext(&GetErrorStream(), context);
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 %13s %10s", 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  str += "\n";
68  std::string line = std::string(str.length(), '-');
69  GetOutputStream() << line << "\n" << str << line << "\n";
70 }
71 
72 void ConsoleReporter::ReportRuns(const std::vector<Run>& reports) {
73  for (const auto& run : reports) {
74  // print the header:
75  // --- if none was printed yet
76  bool print_header = !printed_header_;
77  // --- or if the format is tabular and this run
78  // has different fields from the prev header
79  print_header |= (output_options_ & OO_Tabular) &&
80  (!internal::SameNames(run.counters, prev_counters_));
81  if (print_header) {
82  printed_header_ = true;
83  prev_counters_ = run.counters;
84  PrintHeader(run);
85  }
86  // As an alternative to printing the headers like this, we could sort
87  // the benchmarks by header and then print. But this would require
88  // waiting for the full results before printing, or printing twice.
89  PrintRunData(run);
90  }
91 }
92 
93 static void IgnoreColorPrint(std::ostream& out, LogColor, const char* fmt,
94  ...) {
95  va_list args;
96  va_start(args, fmt);
97  out << FormatString(fmt, args);
98  va_end(args);
99 }
100 
101 void ConsoleReporter::PrintRunData(const Run& result) {
102  typedef void(PrinterFn)(std::ostream&, LogColor, const char*, ...);
103  auto& Out = GetOutputStream();
104  PrinterFn* printer = (output_options_ & OO_Color) ?
105  (PrinterFn*)ColorPrintf : IgnoreColorPrint;
106  auto name_color =
107  (result.report_big_o || result.report_rms) ? COLOR_BLUE : COLOR_GREEN;
108  printer(Out, name_color, "%-*s ", name_field_width_,
109  result.benchmark_name.c_str());
110 
111  if (result.error_occurred) {
112  printer(Out, COLOR_RED, "ERROR OCCURRED: \'%s\'",
113  result.error_message.c_str());
114  printer(Out, COLOR_DEFAULT, "\n");
115  return;
116  }
117  // Format bytes per second
118  std::string rate;
119  if (result.bytes_per_second > 0) {
120  rate = StrCat(" ", HumanReadableNumber(result.bytes_per_second), "B/s");
121  }
122 
123  // Format items per second
124  std::string items;
125  if (result.items_per_second > 0) {
126  items =
127  StrCat(" ", HumanReadableNumber(result.items_per_second), " items/s");
128  }
129 
130  const double real_time = result.GetAdjustedRealTime();
131  const double cpu_time = result.GetAdjustedCPUTime();
132 
133  if (result.report_big_o) {
134  std::string big_o = GetBigOString(result.complexity);
135  printer(Out, COLOR_YELLOW, "%10.2f %s %10.2f %s ", real_time, big_o.c_str(),
136  cpu_time, big_o.c_str());
137  } else if (result.report_rms) {
138  printer(Out, COLOR_YELLOW, "%10.0f %% %10.0f %% ", real_time * 100,
139  cpu_time * 100);
140  } else {
141  const char* timeLabel = GetTimeUnitString(result.time_unit);
142  printer(Out, COLOR_YELLOW, "%10.0f %s %10.0f %s ", real_time, timeLabel,
143  cpu_time, timeLabel);
144  }
145 
146  if (!result.report_big_o && !result.report_rms) {
147  printer(Out, COLOR_CYAN, "%10lld", result.iterations);
148  }
149 
150  for (auto& c : result.counters) {
151  auto const& s = HumanReadableNumber(c.second.value);
152  if (output_options_ & OO_Tabular) {
153  if (c.second.flags & Counter::kIsRate) {
154  printer(Out, COLOR_DEFAULT, " %8s/s", s.c_str());
155  } else {
156  printer(Out, COLOR_DEFAULT, " %10s", s.c_str());
157  }
158  } else {
159  const char* unit = (c.second.flags & Counter::kIsRate) ? "/s" : "";
160  printer(Out, COLOR_DEFAULT, " %s=%s%s", c.first.c_str(), s.c_str(),
161  unit);
162  }
163  }
164 
165  if (!rate.empty()) {
166  printer(Out, COLOR_DEFAULT, " %*s", 13, rate.c_str());
167  }
168 
169  if (!items.empty()) {
170  printer(Out, COLOR_DEFAULT, " %*s", 18, items.c_str());
171  }
172 
173  if (!result.report_label.empty()) {
174  printer(Out, COLOR_DEFAULT, " %s", result.report_label.c_str());
175  }
176 
177  printer(Out, COLOR_DEFAULT, "\n");
178 }
179 
180 } // end namespace benchmark
benchmark::COLOR_CYAN
@ COLOR_CYAN
Definition: colorprint.h:16
benchmark::BenchmarkReporter::GetErrorStream
std::ostream & GetErrorStream() const
Definition: benchmark.h:1116
benchmark::COLOR_DEFAULT
@ COLOR_DEFAULT
Definition: colorprint.h:10
colorprint.h
internal_macros.h
check.h
benchmark
Definition: benchmark.h:241
benchmark::LogColor
LogColor
Definition: colorprint.h:9
s
XmlRpcServer s
benchmark::ConsoleReporter::output_options_
OutputOptions output_options_
Definition: benchmark.h:1152
benchmark::ConsoleReporter::ReportContext
virtual bool ReportContext(const Context &context)
Definition: console_reporter.cc:36
benchmark::BenchmarkReporter::Run::report_rms
bool report_rms
Definition: benchmark.h:1070
benchmark::ConsoleReporter::name_field_width_
size_t name_field_width_
Definition: benchmark.h:1153
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
benchmark::BenchmarkReporter::Context::name_field_width
size_t name_field_width
Definition: benchmark.h:1014
benchmark::ConsoleReporter::PrintHeader
virtual void PrintHeader(const Run &report)
Definition: console_reporter.cc:55
benchmark::ConsoleReporter::prev_counters_
UserCounters prev_counters_
Definition: benchmark.h:1154
benchmark::BenchmarkReporter::Run::GetAdjustedCPUTime
double GetAdjustedCPUTime() const
Definition: reporter.cc:62
benchmark::BenchmarkReporter::PrintBasicContext
static void PrintBasicContext(std::ostream *out, Context const &context)
Definition: reporter.cc:34
benchmark::FormatString
std::string FormatString(const char *msg, va_list args)
Definition: colorprint.cc:85
benchmark::ConsoleReporter::printed_header_
bool printed_header_
Definition: benchmark.h:1155
benchmark::ConsoleReporter::PrintRunData
virtual void PrintRunData(const Run &report)
Definition: console_reporter.cc:101
benchmark::ConsoleReporter::OutputOptions
OutputOptions
Definition: benchmark.h:1134
benchmark::BenchmarkReporter::Run::error_message
std::string error_message
Definition: benchmark.h:1037
benchmark::ColorPrintf
void ColorPrintf(std::ostream &out, LogColor color, const char *fmt,...)
Definition: colorprint.cc:121
benchmark::BenchmarkReporter::Run::report_label
std::string report_label
Definition: benchmark.h:1035
update_failure_list.str
str
Definition: update_failure_list.py:41
complexity.h
benchmark::ConsoleReporter::OO_Color
@ OO_Color
Definition: benchmark.h:1136
benchmark::ConsoleReporter::ReportRuns
virtual void ReportRuns(const std::vector< Run > &reports)
Definition: console_reporter.cc:72
testing::internal::fmt
GTEST_API_ const char * fmt
Definition: gtest.h:1835
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::Counter::kIsRate
@ kIsRate
Definition: benchmark.h:344
benchmark::BenchmarkReporter::Run
Definition: benchmark.h:1017
benchmark::BenchmarkReporter::Run::items_per_second
double items_per_second
Definition: benchmark.h:1058
benchmark::COLOR_GREEN
@ COLOR_GREEN
Definition: colorprint.h:12
benchmark::GetBigOString
std::string GetBigOString(BigO complexity)
Definition: complexity.cc:48
commandlineflags.h
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
benchmark::COLOR_RED
@ COLOR_RED
Definition: colorprint.h:11
benchmark::BenchmarkReporter::Run::bytes_per_second
double bytes_per_second
Definition: benchmark.h:1057
benchmark::BenchmarkReporter::Run::complexity
BigO complexity
Definition: benchmark.h:1064
timers.h
benchmark::BenchmarkReporter::Run::time_unit
TimeUnit time_unit
Definition: benchmark.h:1040
benchmark::HumanReadableNumber
std::string HumanReadableNumber(double n)
Definition: string_util.cc:118
benchmark::COLOR_YELLOW
@ COLOR_YELLOW
Definition: colorprint.h:13
string_util.h
benchmark::ConsoleReporter::OO_Tabular
@ OO_Tabular
Definition: benchmark.h:1137
benchmark::BenchmarkReporter::Run::GetAdjustedRealTime
double GetAdjustedRealTime() const
Definition: reporter.cc:56
benchmark::COLOR_BLUE
@ COLOR_BLUE
Definition: colorprint.h:14
benchmark::BenchmarkReporter::Run::error_occurred
bool error_occurred
Definition: benchmark.h:1036
benchmark::IgnoreColorPrint
static void IgnoreColorPrint(std::ostream &out, LogColor, const char *fmt,...)
Definition: console_reporter.cc:93
counter.h
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::StrCat
std::string StrCat(Args &&... args)
Definition: string_util.h:29
benchmark::internal::SameNames
bool SameNames(UserCounters const &l, UserCounters const &r)
Definition: counter.cc:54
benchmark::BenchmarkReporter::Run::iterations
int64_t iterations
Definition: benchmark.h:1039
benchmarks.python.py_benchmark.args
args
Definition: py_benchmark.py:24


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