Commit cded70a1 by Kai Wolf

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.
parent b2e73408
...@@ -36,6 +36,8 @@ class BenchmarkReporter { ...@@ -36,6 +36,8 @@ class BenchmarkReporter {
// The number of chars in the longest benchmark name. // The number of chars in the longest benchmark name.
size_t name_field_width; size_t name_field_width;
// The time unit for displayed execution time.
std::string time_unit;
}; };
struct Run { struct Run {
...@@ -94,6 +96,7 @@ protected: ...@@ -94,6 +96,7 @@ protected:
virtual void PrintRunData(const Run& report); virtual void PrintRunData(const Run& report);
size_t name_field_width_; size_t name_field_width_;
std::string time_unit_;
}; };
class JSONReporter : public BenchmarkReporter { class JSONReporter : public BenchmarkReporter {
......
...@@ -64,6 +64,10 @@ DEFINE_int32(benchmark_repetitions, 1, ...@@ -64,6 +64,10 @@ DEFINE_int32(benchmark_repetitions, 1,
"The number of runs of each benchmark. If greater than 1, the " "The number of runs of each benchmark. If greater than 1, the "
"mean and standard deviation of the runs will be reported."); "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", DEFINE_string(benchmark_format, "tabular",
"The format to use for console output. Valid values are " "The format to use for console output. Valid values are "
"'tabular', 'json', or 'csv'."); "'tabular', 'json', or 'csv'.");
...@@ -779,7 +783,7 @@ void PrintBenchmarkList() { ...@@ -779,7 +783,7 @@ void PrintBenchmarkList() {
} }
} }
void RunMatchingBenchmarks(const std::string& spec, void RunMatchingBenchmarks(const std::string& spec, const std::string& timeUnit,
BenchmarkReporter* reporter) { BenchmarkReporter* reporter) {
CHECK(reporter != nullptr); CHECK(reporter != nullptr);
if (spec.empty()) return; if (spec.empty()) return;
...@@ -804,6 +808,7 @@ void RunMatchingBenchmarks(const std::string& spec, ...@@ -804,6 +808,7 @@ void RunMatchingBenchmarks(const std::string& spec,
context.cpu_scaling_enabled = CpuScalingEnabled(); context.cpu_scaling_enabled = CpuScalingEnabled();
context.name_field_width = name_field_width; context.name_field_width = name_field_width;
context.time_unit = timeUnit;
if (reporter->ReportContext(context)) { if (reporter->ReportContext(context)) {
for (const auto& benchmark : benchmarks) { for (const auto& benchmark : benchmarks) {
...@@ -838,6 +843,7 @@ void RunSpecifiedBenchmarks(BenchmarkReporter* reporter) { ...@@ -838,6 +843,7 @@ void RunSpecifiedBenchmarks(BenchmarkReporter* reporter) {
internal::PrintBenchmarkList(); internal::PrintBenchmarkList();
return; return;
} }
std::string timeUnit = FLAGS_benchmark_time_unit;
std::string spec = FLAGS_benchmark_filter; std::string spec = FLAGS_benchmark_filter;
if (spec.empty() || spec == "all") if (spec.empty() || spec == "all")
spec = "."; // Regexp that matches all benchmarks spec = "."; // Regexp that matches all benchmarks
...@@ -847,7 +853,7 @@ void RunSpecifiedBenchmarks(BenchmarkReporter* reporter) { ...@@ -847,7 +853,7 @@ void RunSpecifiedBenchmarks(BenchmarkReporter* reporter) {
default_reporter = internal::GetDefaultReporter(); default_reporter = internal::GetDefaultReporter();
reporter = default_reporter.get(); reporter = default_reporter.get();
} }
internal::RunMatchingBenchmarks(spec, reporter); internal::RunMatchingBenchmarks(spec, timeUnit, reporter);
reporter->Finalize(); reporter->Finalize();
} }
...@@ -860,6 +866,7 @@ void PrintUsageAndExit() { ...@@ -860,6 +866,7 @@ void PrintUsageAndExit() {
" [--benchmark_filter=<regex>]\n" " [--benchmark_filter=<regex>]\n"
" [--benchmark_min_time=<min_time>]\n" " [--benchmark_min_time=<min_time>]\n"
" [--benchmark_repetitions=<num_repetitions>]\n" " [--benchmark_repetitions=<num_repetitions>]\n"
" [--benchmark_time_unit=<ns|ms>]\n"
" [--benchmark_format=<tabular|json|csv>]\n" " [--benchmark_format=<tabular|json|csv>]\n"
" [--color_print={true|false}]\n" " [--color_print={true|false}]\n"
" [--v=<verbosity>]\n"); " [--v=<verbosity>]\n");
...@@ -878,6 +885,8 @@ void ParseCommandLineFlags(int* argc, char** argv) { ...@@ -878,6 +885,8 @@ void ParseCommandLineFlags(int* argc, char** argv) {
&FLAGS_benchmark_min_time) || &FLAGS_benchmark_min_time) ||
ParseInt32Flag(argv[i], "benchmark_repetitions", ParseInt32Flag(argv[i], "benchmark_repetitions",
&FLAGS_benchmark_repetitions) || &FLAGS_benchmark_repetitions) ||
ParseStringFlag(argv[i], "benchmark_time_unit",
&FLAGS_benchmark_time_unit) ||
ParseStringFlag(argv[i], "benchmark_format", ParseStringFlag(argv[i], "benchmark_format",
&FLAGS_benchmark_format) || &FLAGS_benchmark_format) ||
ParseBoolFlag(argv[i], "color_print", ParseBoolFlag(argv[i], "color_print",
...@@ -891,6 +900,12 @@ void ParseCommandLineFlags(int* argc, char** argv) { ...@@ -891,6 +900,12 @@ void ParseCommandLineFlags(int* argc, char** argv) {
PrintUsageAndExit(); PrintUsageAndExit();
} }
} }
if (FLAGS_benchmark_time_unit != "ns" &&
FLAGS_benchmark_time_unit != "ms") {
PrintUsageAndExit();
}
if (FLAGS_benchmark_format != "tabular" && if (FLAGS_benchmark_format != "tabular" &&
FLAGS_benchmark_format != "json" && FLAGS_benchmark_format != "json" &&
FLAGS_benchmark_format != "csv") { FLAGS_benchmark_format != "csv") {
......
...@@ -29,6 +29,7 @@ namespace benchmark { ...@@ -29,6 +29,7 @@ namespace benchmark {
bool ConsoleReporter::ReportContext(const Context& context) { bool ConsoleReporter::ReportContext(const Context& context) {
name_field_width_ = context.name_field_width; name_field_width_ = context.name_field_width;
time_unit_ = context.time_unit;
std::cerr << "Run on (" << context.num_cpus << " X " << context.mhz_per_cpu std::cerr << "Run on (" << context.num_cpus << " X " << context.mhz_per_cpu
<< " MHz CPU " << ((context.num_cpus > 1) ? "s" : "") << ")\n"; << " MHz CPU " << ((context.num_cpus > 1) ? "s" : "") << ")\n";
...@@ -46,9 +47,11 @@ bool ConsoleReporter::ReportContext(const Context& context) { ...@@ -46,9 +47,11 @@ bool ConsoleReporter::ReportContext(const Context& context) {
"affected.\n"; "affected.\n";
#endif #endif
std::string timeLabel = "Time(" + time_unit_ + ")";
std::string cpuLabel = "CPU(" + time_unit_ + ")";
int output_width = fprintf(stdout, "%-*s %10s %10s %10s\n", int output_width = fprintf(stdout, "%-*s %10s %10s %10s\n",
static_cast<int>(name_field_width_), "Benchmark", 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"; std::cout << std::string(output_width - 1, '-') << "\n";
return true; return true;
...@@ -92,7 +95,9 @@ void ConsoleReporter::PrintRunData(const Run& result) { ...@@ -92,7 +95,9 @@ void ConsoleReporter::PrintRunData(const Run& result) {
" items/s"); " 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 ", ColorPrintf(COLOR_GREEN, "%-*s ",
name_field_width_, result.benchmark_name.c_str()); name_field_width_, result.benchmark_name.c_str());
if (result.iterations == 0) { if (result.iterations == 0) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment