From c4858d8012acd54d8ef89053c74e9cdcf0fa6649 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Thu, 19 Apr 2018 18:40:08 +0100 Subject: [PATCH] Report the actual iterations run. (#572) Before this change, we would report the number of requested iterations passed to the state. After, we will report the actual number run. As a side-effect, instead of multiplying the expected iterations by the number of threads to get the total number, we can report the actual number of iterations across all threads, which takes into account the situation where some threads might run more iterations than others. --- src/benchmark.cc | 16 ++++++++-------- src/thread_manager.h | 4 ++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/benchmark.cc b/src/benchmark.cc index 7b0d1136..82b15ac7 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -115,7 +115,7 @@ namespace { BenchmarkReporter::Run CreateRunReport( const benchmark::internal::Benchmark::Instance& b, - const internal::ThreadManager::Result& results, size_t iters, + const internal::ThreadManager::Result& results, double seconds) { // Create report about this benchmark run. BenchmarkReporter::Run report; @@ -124,8 +124,8 @@ BenchmarkReporter::Run CreateRunReport( report.error_occurred = results.has_error_; report.error_message = results.error_message_; report.report_label = results.report_label_; - // Report the total iterations across all threads. - report.iterations = static_cast(iters) * b.threads; + // This is the total iterations across all threads. + report.iterations = results.iterations; report.time_unit = b.time_unit; if (!report.error_occurred) { @@ -169,6 +169,7 @@ void RunInThread(const benchmark::internal::Benchmark::Instance* b, { MutexLock l(manager->GetBenchmarkMutex()); internal::ThreadManager::Result& results = manager->results; + results.iterations += st.iterations(); results.cpu_time_used += timer.cpu_time_used(); results.real_time_used += timer.real_time_used(); results.manual_time_used += timer.manual_time_used(); @@ -236,18 +237,17 @@ std::vector RunBenchmark( // Determine if this run should be reported; Either it has // run for a sufficient amount of time or because an error was reported. const bool should_report = repetition_num > 0 - || has_explicit_iteration_count // An exact iteration count was requested + || has_explicit_iteration_count // An exact iteration count was requested || results.has_error_ - || iters >= kMaxIterations - || seconds >= min_time // the elapsed time is large enough + || iters >= kMaxIterations // No chance to try again, we hit the limit. + || seconds >= min_time // the elapsed time is large enough // CPU time is specified but the elapsed real time greatly exceeds the // minimum time. Note that user provided timers are except from this // sanity check. || ((results.real_time_used >= 5 * min_time) && !b.use_manual_time); if (should_report) { - BenchmarkReporter::Run report = - CreateRunReport(b, results, iters, seconds); + BenchmarkReporter::Run report = CreateRunReport(b, results, seconds); if (!report.error_occurred && b.complexity != oNone) complexity_reports->push_back(report); reports.push_back(report); diff --git a/src/thread_manager.h b/src/thread_manager.h index 61755a80..82b4d72b 100644 --- a/src/thread_manager.h +++ b/src/thread_manager.h @@ -1,6 +1,9 @@ #ifndef BENCHMARK_THREAD_MANAGER_H #define BENCHMARK_THREAD_MANAGER_H +#include + +#include "benchmark/benchmark.h" #include "mutex.h" namespace benchmark { @@ -35,6 +38,7 @@ class ThreadManager { public: struct Result { + int64_t iterations = 0; double real_time_used = 0; double cpu_time_used = 0; double manual_time_used = 0;