1
0
mirror of https://github.com/google/benchmark.git synced 2025-04-29 14:30:37 +08:00

Make iterations field width dynamic

This commit is contained in:
Andreas Abel 2023-09-18 17:15:50 +02:00
parent 8403489a39
commit 5b5f19d427
3 changed files with 27 additions and 4 deletions

View File

@ -1895,7 +1895,10 @@ class BENCHMARK_EXPORT ConsoleReporter : public BenchmarkReporter {
OO_Defaults = OO_ColorTabular
};
explicit ConsoleReporter(OutputOptions opts_ = OO_Defaults)
: output_options_(opts_), name_field_width_(0), printed_header_(false) {}
: output_options_(opts_),
name_field_width_(0),
iterations_field_width_(0),
printed_header_(false) {}
bool ReportContext(const Context& context) BENCHMARK_OVERRIDE;
void ReportRuns(const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
@ -1907,6 +1910,7 @@ class BENCHMARK_EXPORT ConsoleReporter : public BenchmarkReporter {
OutputOptions output_options_;
size_t name_field_width_;
UserCounters prev_counters_;
size_t iterations_field_width_;
bool printed_header_;
};

View File

@ -38,6 +38,7 @@ bool ConsoleReporter::ReportContext(const Context& context) {
name_field_width_ = context.name_field_width;
printed_header_ = false;
prev_counters_.clear();
iterations_field_width_ = std::string("Iterations").length();
PrintBasicContext(&GetErrorStream(), context);
@ -56,8 +57,10 @@ bool ConsoleReporter::ReportContext(const Context& context) {
BENCHMARK_EXPORT
void ConsoleReporter::PrintHeader(const Run& run) {
std::string str =
FormatString("%-*s %13s %15s %15s", static_cast<int>(name_field_width_),
"Benchmark", "Time", "CPU", "Iterations");
FormatString("%-*s %13s %15s %*s",
static_cast<int>(name_field_width_), "Benchmark",
"Time", "CPU",
static_cast<int>(iterations_field_width_) + 2, "Iterations");
if (!run.counters.empty()) {
if (output_options_ & OO_Tabular) {
for (auto const& c : run.counters) {
@ -73,6 +76,15 @@ void ConsoleReporter::PrintHeader(const Run& run) {
BENCHMARK_EXPORT
void ConsoleReporter::ReportRuns(const std::vector<Run>& reports) {
auto max_iterations = std::max_element(reports.begin(), reports.end(),
[](const Run& a, const Run& b) {
return a.iterations > b.iterations;
})->iterations;
auto cur_iterations_field_width = std::max(
std::to_string(max_iterations).length(),
iterations_field_width_
);
for (const auto& run : reports) {
// print the header:
// --- if none was printed yet
@ -81,9 +93,12 @@ void ConsoleReporter::ReportRuns(const std::vector<Run>& reports) {
// has different fields from the prev header
print_header |= (output_options_ & OO_Tabular) &&
(!internal::SameNames(run.counters, prev_counters_));
// --- or if the width of the iterations field has increased
print_header |= (cur_iterations_field_width > iterations_field_width_);
if (print_header) {
printed_header_ = true;
prev_counters_ = run.counters;
iterations_field_width_ = cur_iterations_field_width;
PrintHeader(run);
}
// As an alternative to printing the headers like this, we could sort
@ -171,7 +186,8 @@ void ConsoleReporter::PrintRunData(const Run& result) {
}
if (!result.report_big_o && !result.report_rms) {
printer(Out, COLOR_CYAN, "%13lld", result.iterations);
printer(Out, COLOR_CYAN, "%*lld", static_cast<int>(iterations_field_width_),
result.iterations);
}
for (auto& c : result.counters) {

View File

@ -38,6 +38,9 @@ ADD_CASES(TC_ConsoleOut,
{"^BM_CounterSet0_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
{"^BM_CounterSet0_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
{"^BM_CounterSet0_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
{"^[-]+$", MR_Next},
{"^Benchmark %s Time %s CPU %s Iterations %s Bar %s Baz %s Foo$", MR_Next},
{"^[-]+$", MR_Next},
{"^BM_CounterSet0_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
{"^BM_CounterSet1_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},
{"^BM_CounterSet1_Tabular/threads:%int %console_report [ ]*%hrfloat [ ]*%hrfloat [ ]*%hrfloat$", MR_Next},