[libc] Switch to using the generic `<gpuintrin.h>` implementations (#121810)
[llvm-project.git] / third-party / benchmark / src / console_reporter.cc
blob35c3de2a4dbaeab4368a100aacfb01d2b958b458
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.
15 #include <algorithm>
16 #include <cstdint>
17 #include <cstdio>
18 #include <cstring>
19 #include <iostream>
20 #include <string>
21 #include <tuple>
22 #include <vector>
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"
34 namespace benchmark {
36 BENCHMARK_EXPORT
37 bool ConsoleReporter::ReportContext(const Context& context) {
38 name_field_width_ = context.name_field_width;
39 printed_header_ = false;
40 prev_counters_.clear();
42 PrintBasicContext(&GetErrorStream(), context);
44 #ifdef BENCHMARK_OS_WINDOWS
45 if ((output_options_ & OO_Color)) {
46 auto stdOutBuf = std::cout.rdbuf();
47 auto outStreamBuf = GetOutputStream().rdbuf();
48 if (stdOutBuf != outStreamBuf) {
49 GetErrorStream()
50 << "Color printing is only supported for stdout on windows."
51 " Disabling color printing\n";
52 output_options_ = static_cast<OutputOptions>(output_options_ & ~OO_Color);
55 #endif
57 return true;
60 BENCHMARK_EXPORT
61 void ConsoleReporter::PrintHeader(const Run& run) {
62 std::string str =
63 FormatString("%-*s %13s %15s %12s", static_cast<int>(name_field_width_),
64 "Benchmark", "Time", "CPU", "Iterations");
65 if (!run.counters.empty()) {
66 if (output_options_ & OO_Tabular) {
67 for (auto const& c : run.counters) {
68 str += FormatString(" %10s", c.first.c_str());
70 } else {
71 str += " UserCounters...";
74 std::string line = std::string(str.length(), '-');
75 GetOutputStream() << line << "\n" << str << "\n" << line << "\n";
78 BENCHMARK_EXPORT
79 void ConsoleReporter::ReportRuns(const std::vector<Run>& reports) {
80 for (const auto& run : reports) {
81 // print the header:
82 // --- if none was printed yet
83 bool print_header = !printed_header_;
84 // --- or if the format is tabular and this run
85 // has different fields from the prev header
86 print_header |= (output_options_ & OO_Tabular) &&
87 (!internal::SameNames(run.counters, prev_counters_));
88 if (print_header) {
89 printed_header_ = true;
90 prev_counters_ = run.counters;
91 PrintHeader(run);
93 // As an alternative to printing the headers like this, we could sort
94 // the benchmarks by header and then print. But this would require
95 // waiting for the full results before printing, or printing twice.
96 PrintRunData(run);
100 static void IgnoreColorPrint(std::ostream& out, LogColor, const char* fmt,
101 ...) {
102 va_list args;
103 va_start(args, fmt);
104 out << FormatString(fmt, args);
105 va_end(args);
108 static std::string FormatTime(double time) {
109 // For the time columns of the console printer 13 digits are reserved. One of
110 // them is a space and max two of them are the time unit (e.g ns). That puts
111 // us at 10 digits usable for the number.
112 // Align decimal places...
113 if (time < 1.0) {
114 return FormatString("%10.3f", time);
116 if (time < 10.0) {
117 return FormatString("%10.2f", time);
119 if (time < 100.0) {
120 return FormatString("%10.1f", time);
122 // Assuming the time is at max 9.9999e+99 and we have 10 digits for the
123 // number, we get 10-1(.)-1(e)-1(sign)-2(exponent) = 5 digits to print.
124 if (time > 9999999999 /*max 10 digit number*/) {
125 return FormatString("%1.4e", time);
127 return FormatString("%10.0f", time);
130 BENCHMARK_EXPORT
131 void ConsoleReporter::PrintRunData(const Run& result) {
132 typedef void(PrinterFn)(std::ostream&, LogColor, const char*, ...);
133 auto& Out = GetOutputStream();
134 PrinterFn* printer = (output_options_ & OO_Color)
135 ? static_cast<PrinterFn*>(ColorPrintf)
136 : IgnoreColorPrint;
137 auto name_color =
138 (result.report_big_o || result.report_rms) ? COLOR_BLUE : COLOR_GREEN;
139 printer(Out, name_color, "%-*s ", name_field_width_,
140 result.benchmark_name().c_str());
142 if (internal::SkippedWithError == result.skipped) {
143 printer(Out, COLOR_RED, "ERROR OCCURRED: \'%s\'",
144 result.skip_message.c_str());
145 printer(Out, COLOR_DEFAULT, "\n");
146 return;
147 } else if (internal::SkippedWithMessage == result.skipped) {
148 printer(Out, COLOR_WHITE, "SKIPPED: \'%s\'", result.skip_message.c_str());
149 printer(Out, COLOR_DEFAULT, "\n");
150 return;
153 const double real_time = result.GetAdjustedRealTime();
154 const double cpu_time = result.GetAdjustedCPUTime();
155 const std::string real_time_str = FormatTime(real_time);
156 const std::string cpu_time_str = FormatTime(cpu_time);
158 if (result.report_big_o) {
159 std::string big_o = GetBigOString(result.complexity);
160 printer(Out, COLOR_YELLOW, "%10.2f %-4s %10.2f %-4s ", real_time,
161 big_o.c_str(), cpu_time, big_o.c_str());
162 } else if (result.report_rms) {
163 printer(Out, COLOR_YELLOW, "%10.0f %-4s %10.0f %-4s ", real_time * 100, "%",
164 cpu_time * 100, "%");
165 } else if (result.run_type != Run::RT_Aggregate ||
166 result.aggregate_unit == StatisticUnit::kTime) {
167 const char* timeLabel = GetTimeUnitString(result.time_unit);
168 printer(Out, COLOR_YELLOW, "%s %-4s %s %-4s ", real_time_str.c_str(),
169 timeLabel, cpu_time_str.c_str(), timeLabel);
170 } else {
171 assert(result.aggregate_unit == StatisticUnit::kPercentage);
172 printer(Out, COLOR_YELLOW, "%10.2f %-4s %10.2f %-4s ",
173 (100. * result.real_accumulated_time), "%",
174 (100. * result.cpu_accumulated_time), "%");
177 if (!result.report_big_o && !result.report_rms) {
178 printer(Out, COLOR_CYAN, "%10lld", result.iterations);
181 for (auto& c : result.counters) {
182 const std::size_t cNameLen =
183 std::max(std::string::size_type(10), c.first.length());
184 std::string s;
185 const char* unit = "";
186 if (result.run_type == Run::RT_Aggregate &&
187 result.aggregate_unit == StatisticUnit::kPercentage) {
188 s = StrFormat("%.2f", 100. * c.second.value);
189 unit = "%";
190 } else {
191 s = HumanReadableNumber(c.second.value, c.second.oneK);
192 if (c.second.flags & Counter::kIsRate)
193 unit = (c.second.flags & Counter::kInvert) ? "s" : "/s";
195 if (output_options_ & OO_Tabular) {
196 printer(Out, COLOR_DEFAULT, " %*s%s", cNameLen - strlen(unit), s.c_str(),
197 unit);
198 } else {
199 printer(Out, COLOR_DEFAULT, " %s=%s%s", c.first.c_str(), s.c_str(), unit);
203 if (!result.report_label.empty()) {
204 printer(Out, COLOR_DEFAULT, " %s", result.report_label.c_str());
207 printer(Out, COLOR_DEFAULT, "\n");
210 } // end namespace benchmark