diff options
Diffstat (limited to '3rdparty/benchmark/src')
-rw-r--r-- | 3rdparty/benchmark/src/benchmark.cc | 93 | ||||
-rw-r--r-- | 3rdparty/benchmark/src/console_reporter.cc | 42 | ||||
-rw-r--r-- | 3rdparty/benchmark/src/csv_reporter.cc | 9 | ||||
-rw-r--r-- | 3rdparty/benchmark/src/json_reporter.cc | 11 | ||||
-rw-r--r-- | 3rdparty/benchmark/src/reporter.cc | 12 |
5 files changed, 141 insertions, 26 deletions
diff --git a/3rdparty/benchmark/src/benchmark.cc b/3rdparty/benchmark/src/benchmark.cc index 08b180e37e6..1a836eb4abc 100644 --- a/3rdparty/benchmark/src/benchmark.cc +++ b/3rdparty/benchmark/src/benchmark.cc @@ -64,9 +64,9 @@ 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_format, "tabular", +DEFINE_string(benchmark_format, "console", "The format to use for console output. Valid values are " - "'tabular', 'json', or 'csv'."); + "'console', 'json', or 'csv'."); DEFINE_bool(color_print, true, "Enables colorized logging."); @@ -130,6 +130,7 @@ class TimerManager { running_(false), real_time_used_(0), cpu_time_used_(0), + manual_time_used_(0), num_finalized_(0), phase_number_(0), entered_(0) { @@ -170,6 +171,21 @@ class TimerManager { } // Called by each thread + void SetIterationTime(double seconds) EXCLUDES(lock_) { + bool last_thread = false; + { + MutexLock ml(lock_); + last_thread = Barrier(ml); + if (last_thread) { + manual_time_used_ += seconds; + } + } + if (last_thread) { + phase_condition_.notify_all(); + } + } + + // Called by each thread void Finalize() EXCLUDES(lock_) { MutexLock l(lock_); num_finalized_++; @@ -194,6 +210,13 @@ class TimerManager { return cpu_time_used_; } + // REQUIRES: timer is not running + double manual_time_used() EXCLUDES(lock_) { + MutexLock l(lock_); + CHECK(!running_); + return manual_time_used_; + } + private: Mutex lock_; Condition phase_condition_; @@ -207,6 +230,8 @@ class TimerManager { // Accumulated time so far (does not contain current slice if running_) double real_time_used_; double cpu_time_used_; + // Manually set iteration time. User sets this with SetIterationTime(seconds). + double manual_time_used_; // How many threads have called Finalize() int num_finalized_; @@ -261,7 +286,9 @@ struct Benchmark::Instance { int arg1; bool has_arg2; int arg2; + TimeUnit time_unit; bool use_real_time; + bool use_manual_time; double min_time; int threads; // Number of concurrent threads to use bool multithreaded; // Is benchmark multi-threaded? @@ -294,12 +321,14 @@ public: ~BenchmarkImp(); void Arg(int x); + void Unit(TimeUnit unit); void Range(int start, int limit); void DenseRange(int start, int limit); void ArgPair(int start, int limit); void RangePair(int lo1, int hi1, int lo2, int hi2); void MinTime(double n); void UseRealTime(); + void UseManualTime(); void Threads(int t); void ThreadRange(int min_threads, int max_threads); void ThreadPerCpu(); @@ -313,8 +342,10 @@ private: std::string name_; int arg_count_; std::vector< std::pair<int, int> > args_; // Args for all benchmark runs + TimeUnit time_unit_; double min_time_; bool use_real_time_; + bool use_manual_time_; std::vector<int> thread_counts_; BenchmarkImp& operator=(BenchmarkImp const&); @@ -372,8 +403,10 @@ bool BenchmarkFamilies::FindBenchmarks( instance.arg1 = args.first; instance.has_arg2 = family->arg_count_ == 2; instance.arg2 = args.second; + instance.time_unit = family->time_unit_; instance.min_time = family->min_time_; instance.use_real_time = family->use_real_time_; + instance.use_manual_time = family->use_manual_time_; instance.threads = num_threads; instance.multithreaded = !(family->thread_counts_.empty()); @@ -387,7 +420,9 @@ bool BenchmarkFamilies::FindBenchmarks( if (!IsZero(family->min_time_)) { instance.name += StringPrintF("/min_time:%0.3f", family->min_time_); } - if (family->use_real_time_) { + if (family->use_manual_time_) { + instance.name += "/manual_time"; + } else if (family->use_real_time_) { instance.name += "/real_time"; } @@ -406,8 +441,9 @@ bool BenchmarkFamilies::FindBenchmarks( } BenchmarkImp::BenchmarkImp(const char* name) - : name_(name), arg_count_(-1), - min_time_(0.0), use_real_time_(false) { + : name_(name), arg_count_(-1), time_unit_(kNanosecond), + min_time_(0.0), use_real_time_(false), + use_manual_time_(false) { } BenchmarkImp::~BenchmarkImp() { @@ -419,6 +455,10 @@ void BenchmarkImp::Arg(int x) { args_.emplace_back(x, -1); } +void BenchmarkImp::Unit(TimeUnit unit) { + time_unit_ = unit; +} + void BenchmarkImp::Range(int start, int limit) { CHECK(arg_count_ == -1 || arg_count_ == 1); arg_count_ = 1; @@ -466,9 +506,15 @@ void BenchmarkImp::MinTime(double t) { } void BenchmarkImp::UseRealTime() { + CHECK(!use_manual_time_) << "Cannot set UseRealTime and UseManualTime simultaneously."; use_real_time_ = true; } +void BenchmarkImp::UseManualTime() { + CHECK(!use_real_time_) << "Cannot set UseRealTime and UseManualTime simultaneously."; + use_manual_time_ = true; +} + void BenchmarkImp::Threads(int t) { CHECK_GT(t, 0); thread_counts_.push_back(t); @@ -531,6 +577,11 @@ Benchmark* Benchmark::Arg(int x) { return this; } +Benchmark* Benchmark::Unit(TimeUnit unit) { + imp_->Unit(unit); + return this; +} + Benchmark* Benchmark::Range(int start, int limit) { imp_->Range(start, limit); return this; @@ -566,6 +617,11 @@ Benchmark* Benchmark::UseRealTime() { return this; } +Benchmark* Benchmark::UseManualTime() { + imp_->UseManualTime(); + return this; +} + Benchmark* Benchmark::Threads(int t) { imp_->Threads(t); return this; @@ -647,7 +703,7 @@ void RunBenchmark(const benchmark::internal::Benchmark::Instance& b, thread.join(); } for (std::size_t ti = 0; ti < pool.size(); ++ti) { - pool[ti] = std::thread(&RunInThread, &b, iters, ti, &total); + pool[ti] = std::thread(&RunInThread, &b, iters, static_cast<int>(ti), &total); } } else { // Run directly in this thread @@ -658,6 +714,7 @@ void RunBenchmark(const benchmark::internal::Benchmark::Instance& b, const double cpu_accumulated_time = timer_manager->cpu_time_used(); const double real_accumulated_time = timer_manager->real_time_used(); + const double manual_accumulated_time = timer_manager->manual_time_used(); timer_manager.reset(); VLOG(2) << "Ran in " << cpu_accumulated_time << "/" @@ -665,7 +722,9 @@ void RunBenchmark(const benchmark::internal::Benchmark::Instance& b, // Base decisions off of real time if requested by this benchmark. double seconds = cpu_accumulated_time; - if (b.use_real_time) { + if (b.use_manual_time) { + seconds = manual_accumulated_time; + } else if (b.use_real_time) { seconds = real_accumulated_time; } @@ -699,7 +758,12 @@ void RunBenchmark(const benchmark::internal::Benchmark::Instance& b, report.report_label = label; // Report the total iterations across all threads. report.iterations = static_cast<int64_t>(iters) * b.threads; - report.real_accumulated_time = real_accumulated_time; + report.time_unit = b.time_unit; + if (b.use_manual_time) { + report.real_accumulated_time = manual_accumulated_time; + } else { + report.real_accumulated_time = real_accumulated_time; + } report.cpu_accumulated_time = cpu_accumulated_time; report.bytes_per_second = bytes_per_second; report.items_per_second = items_per_second; @@ -760,6 +824,12 @@ void State::ResumeTiming() { timer_manager->StartTimer(); } +void State::SetIterationTime(double seconds) +{ + CHECK(running_benchmark); + timer_manager->SetIterationTime(seconds); +} + void State::SetLabel(const char* label) { CHECK(running_benchmark); MutexLock l(GetBenchmarkLock()); @@ -814,7 +884,7 @@ void RunMatchingBenchmarks(const std::string& spec, std::unique_ptr<BenchmarkReporter> GetDefaultReporter() { typedef std::unique_ptr<BenchmarkReporter> PtrType; - if (FLAGS_benchmark_format == "tabular") { + if (FLAGS_benchmark_format == "console") { return PtrType(new ConsoleReporter); } else if (FLAGS_benchmark_format == "json") { return PtrType(new JSONReporter); @@ -860,7 +930,7 @@ void PrintUsageAndExit() { " [--benchmark_filter=<regex>]\n" " [--benchmark_min_time=<min_time>]\n" " [--benchmark_repetitions=<num_repetitions>]\n" - " [--benchmark_format=<tabular|json|csv>]\n" + " [--benchmark_format=<console|json|csv>]\n" " [--color_print={true|false}]\n" " [--v=<verbosity>]\n"); exit(0); @@ -891,7 +961,8 @@ void ParseCommandLineFlags(int* argc, char** argv) { PrintUsageAndExit(); } } - if (FLAGS_benchmark_format != "tabular" && + + if (FLAGS_benchmark_format != "console" && FLAGS_benchmark_format != "json" && FLAGS_benchmark_format != "csv") { PrintUsageAndExit(); diff --git a/3rdparty/benchmark/src/console_reporter.cc b/3rdparty/benchmark/src/console_reporter.cc index 092936d5935..56bd3ced05b 100644 --- a/3rdparty/benchmark/src/console_reporter.cc +++ b/3rdparty/benchmark/src/console_reporter.cc @@ -18,6 +18,7 @@ #include <cstdio> #include <iostream> #include <string> +#include <tuple> #include <vector> #include "check.h" @@ -46,9 +47,9 @@ bool ConsoleReporter::ReportContext(const Context& context) { "affected.\n"; #endif - int output_width = fprintf(stdout, "%-*s %10s %10s %10s\n", + int output_width = fprintf(stdout, "%-*s %13s %13s %10s\n", static_cast<int>(name_field_width_), "Benchmark", - "Time(ns)", "CPU(ns)", "Iterations"); + "Time", "CPU", "Iterations"); std::cout << std::string(output_width - 1, '-') << "\n"; return true; @@ -92,25 +93,44 @@ void ConsoleReporter::PrintRunData(const Run& result) { " items/s"); } - double const multiplier = 1e9; // nano second multiplier + double multiplier; + const char* timeLabel; + std::tie(timeLabel, multiplier) = GetTimeUnitAndMultiplier(result.time_unit); + ColorPrintf(COLOR_GREEN, "%-*s ", name_field_width_, result.benchmark_name.c_str()); + if (result.iterations == 0) { - ColorPrintf(COLOR_YELLOW, "%10.0f %10.0f ", + ColorPrintf(COLOR_YELLOW, "%10.0f %s %10.0f %s ", result.real_accumulated_time * multiplier, - result.cpu_accumulated_time * multiplier); + timeLabel, + result.cpu_accumulated_time * multiplier, + timeLabel); } else { - ColorPrintf(COLOR_YELLOW, "%10.0f %10.0f ", + ColorPrintf(COLOR_YELLOW, "%10.0f %s %10.0f %s ", (result.real_accumulated_time * multiplier) / (static_cast<double>(result.iterations)), + timeLabel, (result.cpu_accumulated_time * multiplier) / - (static_cast<double>(result.iterations))); + (static_cast<double>(result.iterations)), + timeLabel); } + ColorPrintf(COLOR_CYAN, "%10lld", result.iterations); - ColorPrintf(COLOR_DEFAULT, "%*s %*s %s\n", - 13, rate.c_str(), - 18, items.c_str(), - result.report_label.c_str()); + + if (!rate.empty()) { + ColorPrintf(COLOR_DEFAULT, " %*s", 13, rate.c_str()); + } + + if (!items.empty()) { + ColorPrintf(COLOR_DEFAULT, " %*s", 18, items.c_str()); + } + + if (!result.report_label.empty()) { + ColorPrintf(COLOR_DEFAULT, " %s", result.report_label.c_str()); + } + + ColorPrintf(COLOR_DEFAULT, "\n"); } } // end namespace benchmark diff --git a/3rdparty/benchmark/src/csv_reporter.cc b/3rdparty/benchmark/src/csv_reporter.cc index d78a9dfb267..3f67d1de1e6 100644 --- a/3rdparty/benchmark/src/csv_reporter.cc +++ b/3rdparty/benchmark/src/csv_reporter.cc @@ -17,6 +17,7 @@ #include <cstdint> #include <iostream> #include <string> +#include <tuple> #include <vector> #include "string_util.h" @@ -42,7 +43,7 @@ bool CSVReporter::ReportContext(const Context& context) { std::cerr << "***WARNING*** Library was built as DEBUG. Timings may be " "affected.\n"; #endif - std::cout << "name,iterations,real_time,cpu_time,bytes_per_second," + std::cout << "name,iterations,real_time,cpu_time,time_unit,bytes_per_second," "items_per_second,label\n"; return true; } @@ -66,7 +67,10 @@ void CSVReporter::ReportRuns(std::vector<Run> const& reports) { } void CSVReporter::PrintRunData(Run const& run) { - double const multiplier = 1e9; // nano second multiplier + double multiplier; + const char* timeLabel; + std::tie(timeLabel, multiplier) = GetTimeUnitAndMultiplier(run.time_unit); + double cpu_time = run.cpu_accumulated_time * multiplier; double real_time = run.real_accumulated_time * multiplier; if (run.iterations != 0) { @@ -83,6 +87,7 @@ void CSVReporter::PrintRunData(Run const& run) { std::cout << run.iterations << ","; std::cout << real_time << ","; std::cout << cpu_time << ","; + std::cout << timeLabel << ","; if (run.bytes_per_second > 0.0) { std::cout << run.bytes_per_second; diff --git a/3rdparty/benchmark/src/json_reporter.cc b/3rdparty/benchmark/src/json_reporter.cc index def50ac49cf..7ed141fc179 100644 --- a/3rdparty/benchmark/src/json_reporter.cc +++ b/3rdparty/benchmark/src/json_reporter.cc @@ -17,6 +17,7 @@ #include <cstdint> #include <iostream> #include <string> +#include <tuple> #include <vector> #include "string_util.h" @@ -120,7 +121,10 @@ void JSONReporter::Finalize() { } void JSONReporter::PrintRunData(Run const& run) { - double const multiplier = 1e9; // nano second multiplier + double multiplier; + const char* timeLabel; + std::tie(timeLabel, multiplier) = GetTimeUnitAndMultiplier(run.time_unit); + double cpu_time = run.cpu_accumulated_time * multiplier; double real_time = run.real_accumulated_time * multiplier; if (run.iterations != 0) { @@ -140,7 +144,10 @@ void JSONReporter::PrintRunData(Run const& run) { << FormatKV("real_time", RoundDouble(real_time)) << ",\n"; out << indent - << FormatKV("cpu_time", RoundDouble(cpu_time)); + << FormatKV("cpu_time", RoundDouble(cpu_time)) + << ",\n"; + out << indent + << FormatKV("time_unit", timeLabel); if (run.bytes_per_second > 0.0) { out << ",\n" << indent << FormatKV("bytes_per_second", RoundDouble(run.bytes_per_second)); diff --git a/3rdparty/benchmark/src/reporter.cc b/3rdparty/benchmark/src/reporter.cc index 4b47e3d556c..036546e7669 100644 --- a/3rdparty/benchmark/src/reporter.cc +++ b/3rdparty/benchmark/src/reporter.cc @@ -77,6 +77,18 @@ void BenchmarkReporter::ComputeStats( stddev_data->items_per_second = items_per_second_stat.StdDev(); } +TimeUnitMultiplier BenchmarkReporter::GetTimeUnitAndMultiplier(TimeUnit unit) { + switch (unit) { + case kMillisecond: + return std::make_pair("ms", 1e3); + case kMicrosecond: + return std::make_pair("us", 1e6); + case kNanosecond: + default: + return std::make_pair("ns", 1e9); + } +} + void BenchmarkReporter::Finalize() { } |