diff --git a/include/benchmark/reporter.h b/include/benchmark/reporter.h index d23ab657..f2a8dc25 100644 --- a/include/benchmark/reporter.h +++ b/include/benchmark/reporter.h @@ -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 { diff --git a/src/benchmark.cc b/src/benchmark.cc index 08b180e3..dd372021 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -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=]\n" " [--benchmark_min_time=]\n" " [--benchmark_repetitions=]\n" + " [--benchmark_time_unit=]\n" " [--benchmark_format=]\n" " [--color_print={true|false}]\n" " [--v=]\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") { diff --git a/src/console_reporter.cc b/src/console_reporter.cc index 092936d5..6af51571 100644 --- a/src/console_reporter.cc +++ b/src/console_reporter.cc @@ -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(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) {