Add optional ms time unit for console reporter

Some benchmarks may run a few milliseconds which makes it kind of hard to visually compare, since the currently only available nanoseconds numbers can get very large in this case. Therefore this commit adds an optional command line flag --benchmark_time_unit which lets the user choose between ns and ms time units for displaying the mean execution time.
This commit is contained in:
Kai Wolf 2016-03-24 22:18:55 +01:00
parent b2e7340875
commit cded70a166
3 changed files with 27 additions and 4 deletions

View File

@ -36,6 +36,8 @@ class BenchmarkReporter {
// The number of chars in the longest benchmark name.
size_t name_field_width;
// The time unit for displayed execution time.
std::string time_unit;
};
struct Run {
@ -94,6 +96,7 @@ protected:
virtual void PrintRunData(const Run& report);
size_t name_field_width_;
std::string time_unit_;
};
class JSONReporter : public BenchmarkReporter {

View File

@ -64,6 +64,10 @@ DEFINE_int32(benchmark_repetitions, 1,
"The number of runs of each benchmark. If greater than 1, the "
"mean and standard deviation of the runs will be reported.");
DEFINE_string(benchmark_time_unit, "ns",
"The time unit to use for console output. Valid values are "
"'ns', or 'ms'.");
DEFINE_string(benchmark_format, "tabular",
"The format to use for console output. Valid values are "
"'tabular', 'json', or 'csv'.");
@ -779,7 +783,7 @@ void PrintBenchmarkList() {
}
}
void RunMatchingBenchmarks(const std::string& spec,
void RunMatchingBenchmarks(const std::string& spec, const std::string& timeUnit,
BenchmarkReporter* reporter) {
CHECK(reporter != nullptr);
if (spec.empty()) return;
@ -804,6 +808,7 @@ void RunMatchingBenchmarks(const std::string& spec,
context.cpu_scaling_enabled = CpuScalingEnabled();
context.name_field_width = name_field_width;
context.time_unit = timeUnit;
if (reporter->ReportContext(context)) {
for (const auto& benchmark : benchmarks) {
@ -838,6 +843,7 @@ void RunSpecifiedBenchmarks(BenchmarkReporter* reporter) {
internal::PrintBenchmarkList();
return;
}
std::string timeUnit = FLAGS_benchmark_time_unit;
std::string spec = FLAGS_benchmark_filter;
if (spec.empty() || spec == "all")
spec = "."; // Regexp that matches all benchmarks
@ -847,7 +853,7 @@ void RunSpecifiedBenchmarks(BenchmarkReporter* reporter) {
default_reporter = internal::GetDefaultReporter();
reporter = default_reporter.get();
}
internal::RunMatchingBenchmarks(spec, reporter);
internal::RunMatchingBenchmarks(spec, timeUnit, reporter);
reporter->Finalize();
}
@ -860,6 +866,7 @@ void PrintUsageAndExit() {
" [--benchmark_filter=<regex>]\n"
" [--benchmark_min_time=<min_time>]\n"
" [--benchmark_repetitions=<num_repetitions>]\n"
" [--benchmark_time_unit=<ns|ms>]\n"
" [--benchmark_format=<tabular|json|csv>]\n"
" [--color_print={true|false}]\n"
" [--v=<verbosity>]\n");
@ -878,6 +885,8 @@ void ParseCommandLineFlags(int* argc, char** argv) {
&FLAGS_benchmark_min_time) ||
ParseInt32Flag(argv[i], "benchmark_repetitions",
&FLAGS_benchmark_repetitions) ||
ParseStringFlag(argv[i], "benchmark_time_unit",
&FLAGS_benchmark_time_unit) ||
ParseStringFlag(argv[i], "benchmark_format",
&FLAGS_benchmark_format) ||
ParseBoolFlag(argv[i], "color_print",
@ -891,6 +900,12 @@ void ParseCommandLineFlags(int* argc, char** argv) {
PrintUsageAndExit();
}
}
if (FLAGS_benchmark_time_unit != "ns" &&
FLAGS_benchmark_time_unit != "ms") {
PrintUsageAndExit();
}
if (FLAGS_benchmark_format != "tabular" &&
FLAGS_benchmark_format != "json" &&
FLAGS_benchmark_format != "csv") {

View File

@ -29,6 +29,7 @@ namespace benchmark {
bool ConsoleReporter::ReportContext(const Context& context) {
name_field_width_ = context.name_field_width;
time_unit_ = context.time_unit;
std::cerr << "Run on (" << context.num_cpus << " X " << context.mhz_per_cpu
<< " MHz CPU " << ((context.num_cpus > 1) ? "s" : "") << ")\n";
@ -46,9 +47,11 @@ bool ConsoleReporter::ReportContext(const Context& context) {
"affected.\n";
#endif
std::string timeLabel = "Time(" + time_unit_ + ")";
std::string cpuLabel = "CPU(" + time_unit_ + ")";
int output_width = fprintf(stdout, "%-*s %10s %10s %10s\n",
static_cast<int>(name_field_width_), "Benchmark",
"Time(ns)", "CPU(ns)", "Iterations");
timeLabel.c_str(), cpuLabel.c_str(), "Iterations");
std::cout << std::string(output_width - 1, '-') << "\n";
return true;
@ -92,7 +95,9 @@ void ConsoleReporter::PrintRunData(const Run& result) {
" items/s");
}
double const multiplier = 1e9; // nano second multiplier
double const multiplier = time_unit_ == "ns" ? 1e9 : 1e3; // nano second or
// millis multiplier
ColorPrintf(COLOR_GREEN, "%-*s ",
name_field_width_, result.benchmark_name.c_str());
if (result.iterations == 0) {