1 // Copyright 2015 Google Inc. All rights reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "benchmark/benchmark.h"
26 #include "colorprint.h"
27 #include "commandlineflags.h"
28 #include "complexity.h"
30 #include "internal_macros.h"
31 #include "string_util.h"
36 bool ConsoleReporter::ReportContext(const Context
& context
) {
37 name_field_width_
= context
.name_field_width
;
38 printed_header_
= false;
39 prev_counters_
.clear();
41 PrintBasicContext(&GetErrorStream(), context
);
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";
48 output_options_
= static_cast<OutputOptions
>(output_options_
& ~OO_Color
);
55 void ConsoleReporter::PrintHeader(const Run
& run
) {
57 FormatString("%-*s %13s %15s %12s", static_cast<int>(name_field_width_
),
58 "Benchmark", "Time", "CPU", "Iterations");
59 if (!run
.counters
.empty()) {
60 if (output_options_
& OO_Tabular
) {
61 for (auto const& c
: run
.counters
) {
62 str
+= FormatString(" %10s", c
.first
.c_str());
65 str
+= " UserCounters...";
68 std::string line
= std::string(str
.length(), '-');
69 GetOutputStream() << line
<< "\n" << str
<< "\n" << line
<< "\n";
72 void ConsoleReporter::ReportRuns(const std::vector
<Run
>& reports
) {
73 for (const auto& run
: reports
) {
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_
));
82 printed_header_
= true;
83 prev_counters_
= run
.counters
;
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.
93 static void IgnoreColorPrint(std::ostream
& out
, LogColor
, const char* fmt
,
97 out
<< FormatString(fmt
, args
);
101 static std::string
FormatTime(double time
) {
102 // Align decimal places...
104 return FormatString("%10.3f", time
);
107 return FormatString("%10.2f", time
);
110 return FormatString("%10.1f", time
);
112 return FormatString("%10.0f", time
);
115 void ConsoleReporter::PrintRunData(const Run
& result
) {
116 typedef void(PrinterFn
)(std::ostream
&, LogColor
, const char*, ...);
117 auto& Out
= GetOutputStream();
118 PrinterFn
* printer
= (output_options_
& OO_Color
)
119 ? static_cast<PrinterFn
*>(ColorPrintf
)
122 (result
.report_big_o
|| result
.report_rms
) ? COLOR_BLUE
: COLOR_GREEN
;
123 printer(Out
, name_color
, "%-*s ", name_field_width_
,
124 result
.benchmark_name().c_str());
126 if (result
.error_occurred
) {
127 printer(Out
, COLOR_RED
, "ERROR OCCURRED: \'%s\'",
128 result
.error_message
.c_str());
129 printer(Out
, COLOR_DEFAULT
, "\n");
133 const double real_time
= result
.GetAdjustedRealTime();
134 const double cpu_time
= result
.GetAdjustedCPUTime();
135 const std::string real_time_str
= FormatTime(real_time
);
136 const std::string cpu_time_str
= FormatTime(cpu_time
);
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
,
141 big_o
.c_str(), 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(),
149 timeLabel
, cpu_time_str
.c_str(), timeLabel
);
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
), "%");
157 if (!result
.report_big_o
&& !result
.report_rms
) {
158 printer(Out
, COLOR_CYAN
, "%10lld", result
.iterations
);
161 for (auto& c
: result
.counters
) {
162 const std::size_t cNameLen
=
163 std::max(std::string::size_type(10), c
.first
.length());
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
);
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";
175 if (output_options_
& OO_Tabular
) {
176 printer(Out
, COLOR_DEFAULT
, " %*s%s", cNameLen
- strlen(unit
), s
.c_str(),
179 printer(Out
, COLOR_DEFAULT
, " %s=%s%s", c
.first
.c_str(), s
.c_str(), unit
);
183 if (!result
.report_label
.empty()) {
184 printer(Out
, COLOR_DEFAULT
, " %s", result
.report_label
.c_str());
187 printer(Out
, COLOR_DEFAULT
, "\n");
190 } // end namespace benchmark