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/reporter.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 36 bool ConsoleReporter::ReportContext(const Context& context) { 37 name_field_width_ = context.name_field_width; 38 printed_header_ = false; 39 40 PrintBasicContext(&GetErrorStream(), context); 41 42 #ifdef BENCHMARK_OS_WINDOWS 43 if (color_output_ && &std::cout != &GetOutputStream()) { 44 GetErrorStream() 45 << "Color printing is only supported for stdout on windows." 46 " Disabling color printing\n"; 47 color_output_ = false; 48 } 49 #endif 50 51 return true; 52 } 53 54 void ConsoleReporter::PrintHeader(const Run& run) { 55 std::string str = 56 FormatString("%-*s %13s %13s %10s\n", static_cast<int>(name_field_width_), 57 "Benchmark", "Time", "CPU", "Iterations"); 58 if(!run.counters.empty()) { 59 str += " UserCounters..."; 60 } 61 std::string line = std::string(str.length(), '-'); 62 GetOutputStream() << line << "\n" << str << line << "\n"; 63 } 64 65 void ConsoleReporter::ReportRuns(const std::vector<Run>& reports) { 66 for (const auto& run : reports) { 67 // print the header if none was printed yet 68 if (!printed_header_) { 69 printed_header_ = true; 70 PrintHeader(run); 71 } 72 // As an alternative to printing the headers like this, we could sort 73 // the benchmarks by header and then print like that. 74 PrintRunData(run); 75 } 76 } 77 78 static void IgnoreColorPrint(std::ostream& out, LogColor, const char* fmt, 79 ...) { 80 va_list args; 81 va_start(args, fmt); 82 out << FormatString(fmt, args); 83 va_end(args); 84 } 85 86 void ConsoleReporter::PrintRunData(const Run& result) { 87 typedef void(PrinterFn)(std::ostream&, LogColor, const char*, ...); 88 auto& Out = GetOutputStream(); 89 PrinterFn* printer = 90 color_output_ ? (PrinterFn*)ColorPrintf : IgnoreColorPrint; 91 auto name_color = 92 (result.report_big_o || result.report_rms) ? COLOR_BLUE : COLOR_GREEN; 93 printer(Out, name_color, "%-*s ", name_field_width_, 94 result.benchmark_name.c_str()); 95 96 if (result.error_occurred) { 97 printer(Out, COLOR_RED, "ERROR OCCURRED: \'%s\'", 98 result.error_message.c_str()); 99 printer(Out, COLOR_DEFAULT, "\n"); 100 return; 101 } 102 // Format bytes per second 103 std::string rate; 104 if (result.bytes_per_second > 0) { 105 rate = StrCat(" ", HumanReadableNumber(result.bytes_per_second), "B/s"); 106 } 107 108 // Format items per second 109 std::string items; 110 if (result.items_per_second > 0) { 111 items = 112 StrCat(" ", HumanReadableNumber(result.items_per_second), " items/s"); 113 } 114 115 const double real_time = result.GetAdjustedRealTime(); 116 const double cpu_time = result.GetAdjustedCPUTime(); 117 118 if (result.report_big_o) { 119 std::string big_o = GetBigOString(result.complexity); 120 printer(Out, COLOR_YELLOW, "%10.2f %s %10.2f %s ", real_time, big_o.c_str(), 121 cpu_time, big_o.c_str()); 122 } else if (result.report_rms) { 123 printer(Out, COLOR_YELLOW, "%10.0f %% %10.0f %% ", real_time * 100, 124 cpu_time * 100); 125 } else { 126 const char* timeLabel = GetTimeUnitString(result.time_unit); 127 printer(Out, COLOR_YELLOW, "%10.0f %s %10.0f %s ", real_time, timeLabel, 128 cpu_time, timeLabel); 129 } 130 131 if (!result.report_big_o && !result.report_rms) { 132 printer(Out, COLOR_CYAN, "%10lld", result.iterations); 133 } 134 135 for (auto& c : result.counters) { 136 auto const& s = HumanReadableNumber(c.second.value); 137 printer(Out, COLOR_DEFAULT, " %s=%s", c.first.c_str(), s.c_str()); 138 } 139 140 if (!rate.empty()) { 141 printer(Out, COLOR_DEFAULT, " %*s", 13, rate.c_str()); 142 } 143 144 if (!items.empty()) { 145 printer(Out, COLOR_DEFAULT, " %*s", 18, items.c_str()); 146 } 147 148 if (!result.report_label.empty()) { 149 printer(Out, COLOR_DEFAULT, " %s", result.report_label.c_str()); 150 } 151 152 printer(Out, COLOR_DEFAULT, "\n"); 153 } 154 155 } // end namespace benchmark 156