Skip to content

Commit 10bc615

Browse files
authored
Merge d84d911 into 9e34655
2 parents 9e34655 + d84d911 commit 10bc615

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

include/benchmark/reporter.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,17 @@ class BenchmarkReporter {
157157
// Simple reporter that outputs benchmark data to the console. This is the
158158
// default reporter used by RunSpecifiedBenchmarks().
159159
class ConsoleReporter : public BenchmarkReporter {
160-
public:
161-
enum OutputOptions { OO_None, OO_Color };
162-
explicit ConsoleReporter(OutputOptions color_output = OO_Color)
163-
: name_field_width_(0), color_output_(color_output == OO_Color) {}
160+
public:
161+
enum OutputOptions {
162+
OO_None = 0,
163+
OO_Color = 1,
164+
OO_Tabular = 2,
165+
OO_ColorTabular = OO_Color|OO_Tabular,
166+
OO_Defaults = OO_ColorTabular
167+
};
168+
explicit ConsoleReporter(OutputOptions opts_ = OO_Defaults)
169+
: output_options_(opts_), name_field_width_(0),
170+
prev_counters_(), printed_header_(false) {}
164171

165172
virtual bool ReportContext(const Context& context);
166173
virtual void ReportRuns(const std::vector<Run>& reports);
@@ -169,11 +176,10 @@ class ConsoleReporter : public BenchmarkReporter {
169176
virtual void PrintRunData(const Run& report);
170177
virtual void PrintHeader(const Run& report);
171178

179+
OutputOptions output_options_;
172180
size_t name_field_width_;
181+
UserCounters prev_counters_;
173182
bool printed_header_;
174-
175-
private:
176-
bool color_output_;
177183
};
178184

179185
class JSONReporter : public BenchmarkReporter {

src/benchmark.cc

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ DEFINE_string(benchmark_color, "auto",
9191
"environment variable is set to a terminal type that supports "
9292
"colors.");
9393

94+
DEFINE_bool(benchmark_counters_tabular, false,
95+
"Whether to use tabular format when printing user counters to "
96+
"the console. Valid values: 'true'/'yes'/1, 'false'/'no'/0."
97+
"Defaults to false.");
98+
9499
DEFINE_int32(v, 0, "The level of verbose logging to output");
95100

96101
namespace benchmark {
@@ -510,10 +515,10 @@ void RunBenchmarks(const std::vector<Benchmark::Instance>& benchmarks,
510515
}
511516

512517
std::unique_ptr<BenchmarkReporter> CreateReporter(
513-
std::string const& name, ConsoleReporter::OutputOptions allow_color) {
518+
std::string const& name, ConsoleReporter::OutputOptions output_opts) {
514519
typedef std::unique_ptr<BenchmarkReporter> PtrType;
515520
if (name == "console") {
516-
return PtrType(new ConsoleReporter(allow_color));
521+
return PtrType(new ConsoleReporter(output_opts));
517522
} else if (name == "json") {
518523
return PtrType(new JSONReporter);
519524
} else if (name == "csv") {
@@ -546,16 +551,21 @@ size_t RunSpecifiedBenchmarks(BenchmarkReporter* console_reporter,
546551
std::unique_ptr<BenchmarkReporter> default_console_reporter;
547552
std::unique_ptr<BenchmarkReporter> default_file_reporter;
548553
if (!console_reporter) {
549-
auto output_opts = ConsoleReporter::OO_None;
550-
if (FLAGS_benchmark_color == "auto")
551-
output_opts = IsColorTerminal() ? ConsoleReporter::OO_Color
552-
: ConsoleReporter::OO_None;
553-
else
554-
output_opts = IsTruthyFlagValue(FLAGS_benchmark_color)
555-
? ConsoleReporter::OO_Color
556-
: ConsoleReporter::OO_None;
557-
default_console_reporter =
558-
internal::CreateReporter(FLAGS_benchmark_format, output_opts);
554+
int output_opts = ConsoleReporter::OO_Defaults;
555+
if ((FLAGS_benchmark_color == "auto" && IsColorTerminal()) ||
556+
IsTruthyFlagValue(FLAGS_benchmark_color)) {
557+
output_opts |= ConsoleReporter::OO_Color;
558+
} else {
559+
output_opts &= ~ConsoleReporter::OO_Color;
560+
}
561+
if(FLAGS_benchmark_counters_tabular) {
562+
output_opts |= ConsoleReporter::OO_Tabular;
563+
} else {
564+
output_opts &= ~ConsoleReporter::OO_Tabular;
565+
}
566+
default_console_reporter = internal::CreateReporter(
567+
FLAGS_benchmark_format,
568+
static_cast< ConsoleReporter::OutputOptions >(output_opts));
559569
console_reporter = default_console_reporter.get();
560570
}
561571
auto& Out = console_reporter->GetOutputStream();
@@ -614,6 +624,7 @@ void PrintUsageAndExit() {
614624
" [--benchmark_out=<filename>]\n"
615625
" [--benchmark_out_format=<json|console|csv>]\n"
616626
" [--benchmark_color={auto|true|false}]\n"
627+
" [--benchmark_counters_tabular={true|false}]\n"
617628
" [--v=<verbosity>]\n");
618629
exit(0);
619630
}
@@ -638,6 +649,8 @@ void ParseCommandLineFlags(int* argc, char** argv) {
638649
// "color_print" is the deprecated name for "benchmark_color".
639650
// TODO: Remove this.
640651
ParseStringFlag(argv[i], "color_print", &FLAGS_benchmark_color) ||
652+
ParseBoolFlag(argv[i], "benchmark_counters_tabular",
653+
&FLAGS_benchmark_counters_tabular) ||
641654
ParseInt32Flag(argv[i], "v", &FLAGS_v)) {
642655
for (int j = i; j != *argc - 1; ++j) argv[j] = argv[j + 1];
643656

src/console_reporter.cc

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,16 @@ namespace benchmark {
3636
bool ConsoleReporter::ReportContext(const Context& context) {
3737
name_field_width_ = context.name_field_width;
3838
printed_header_ = false;
39+
prev_counters_.clear();
3940

4041
PrintBasicContext(&GetErrorStream(), context);
4142

4243
#ifdef BENCHMARK_OS_WINDOWS
43-
if (color_output_ && &std::cout != &GetOutputStream()) {
44+
if ((output_options_ & OO_Color) && &std::cout != &GetOutputStream()) {
4445
GetErrorStream()
4546
<< "Color printing is only supported for stdout on windows."
4647
" Disabling color printing\n";
47-
color_output_ = false;
48+
output_options_ = static_cast< OutputOptions >(output_options_ & ~OO_Color);
4849
}
4950
#endif
5051

@@ -64,13 +65,21 @@ void ConsoleReporter::PrintHeader(const Run& run) {
6465

6566
void ConsoleReporter::ReportRuns(const std::vector<Run>& reports) {
6667
for (const auto& run : reports) {
67-
// print the header if none was printed yet
68-
if (!printed_header_) {
68+
// print the header:
69+
// --- if none was printed yet
70+
bool print_header = !printed_header_;
71+
// --- or if the format is tabular and this run
72+
// has different fields from the prev header
73+
print_header |= (output_options_ & OO_Tabular) &&
74+
(!internal::SameNames(run.counters, prev_counters_));
75+
if (print_header) {
6976
printed_header_ = true;
77+
prev_counters_ = run.counters;
7078
PrintHeader(run);
7179
}
7280
// As an alternative to printing the headers like this, we could sort
73-
// the benchmarks by header and then print like that.
81+
// the benchmarks by header and then print. But this would require
82+
// waiting for the full results before printing, or printing twice.
7483
PrintRunData(run);
7584
}
7685
}
@@ -86,8 +95,8 @@ static void IgnoreColorPrint(std::ostream& out, LogColor, const char* fmt,
8695
void ConsoleReporter::PrintRunData(const Run& result) {
8796
typedef void(PrinterFn)(std::ostream&, LogColor, const char*, ...);
8897
auto& Out = GetOutputStream();
89-
PrinterFn* printer =
90-
color_output_ ? (PrinterFn*)ColorPrintf : IgnoreColorPrint;
98+
PrinterFn* printer = (output_options_ & OO_Color) ?
99+
(PrinterFn*)ColorPrintf : IgnoreColorPrint;
91100
auto name_color =
92101
(result.report_big_o || result.report_rms) ? COLOR_BLUE : COLOR_GREEN;
93102
printer(Out, name_color, "%-*s ", name_field_width_,
@@ -134,7 +143,11 @@ void ConsoleReporter::PrintRunData(const Run& result) {
134143

135144
for (auto& c : result.counters) {
136145
auto const& s = HumanReadableNumber(c.second.value);
137-
printer(Out, COLOR_DEFAULT, " %s=%s", c.first.c_str(), s.c_str());
146+
if(output_options_ & OO_Tabular) {
147+
printer(Out, COLOR_DEFAULT, " %10s", s.c_str());
148+
} else {
149+
printer(Out, COLOR_DEFAULT, " %s=%s", c.first.c_str(), s.c_str());
150+
}
138151
}
139152

140153
if (!rate.empty()) {

src/counter.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ bool SameNames(UserCounters const& l, UserCounters const& r) {
5757
return false;
5858
}
5959
for (auto const& c : l) {
60-
if ( r.find(c.first) == r.end()) {
60+
if (r.find(c.first) == r.end()) {
6161
return false;
6262
}
6363
}

0 commit comments

Comments
 (0)