From 20f1c0e2a8e692076dd7a5586f73ab8e2d726c12 Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Tue, 17 Mar 2015 16:16:36 -0400 Subject: [PATCH] Apply reporter interface changes. Make report methods non-const and add a Finalize method. --- include/benchmark/benchmark_api.h | 2 +- include/benchmark/reporter.h | 18 +++++++++++------- src/benchmark.cc | 11 +++++++---- src/reporter.cc | 12 +++++++----- test/filter_test.cc | 4 ++-- 5 files changed, 28 insertions(+), 19 deletions(-) diff --git a/include/benchmark/benchmark_api.h b/include/benchmark/benchmark_api.h index 78a9210a..41b8b4f0 100644 --- a/include/benchmark/benchmark_api.h +++ b/include/benchmark/benchmark_api.h @@ -149,7 +149,7 @@ void Initialize(int* argc, const char** argv); // Otherwise, run all benchmarks specified by the --benchmark_filter flag, // and exit after running the benchmarks. void RunSpecifiedBenchmarks(); -void RunSpecifiedBenchmarks(const BenchmarkReporter* reporter); +void RunSpecifiedBenchmarks(BenchmarkReporter* reporter); // If this routine is called, peak memory allocation past this point in the // benchmark is reported at the end of the benchmark report line. (It is diff --git a/include/benchmark/reporter.h b/include/benchmark/reporter.h index 66dc6dde..b66854c0 100644 --- a/include/benchmark/reporter.h +++ b/include/benchmark/reporter.h @@ -67,13 +67,17 @@ class BenchmarkReporter { // platform under which the benchmarks are running. The benchmark run is // never started if this function returns false, allowing the reporter // to skip runs based on the context information. - virtual bool ReportContext(const Context& context) const = 0; + virtual bool ReportContext(const Context& context) = 0; // Called once for each group of benchmark runs, gives information about // cpu-time and heap memory usage during the benchmark run. // Note that all the grouped benchmark runs should refer to the same // benchmark, thus have the same name. - virtual void ReportRuns(const std::vector& report) const = 0; + virtual void ReportRuns(const std::vector& report) = 0; + + // Called once and only once after ever group of benchmarks is run and + // reported. + virtual void Finalize(); virtual ~BenchmarkReporter(); }; @@ -82,12 +86,12 @@ class BenchmarkReporter { // default reporter used by RunSpecifiedBenchmarks(). class ConsoleReporter : public BenchmarkReporter { public: - virtual bool ReportContext(const Context& context) const; - virtual void ReportRuns(const std::vector& reports) const; + virtual bool ReportContext(const Context& context); + virtual void ReportRuns(const std::vector& reports); private: - virtual void PrintRunData(const Run& report) const; - // TODO(ericwf): Find a better way to share this information. - mutable size_t name_field_width_; + virtual void PrintRunData(const Run& report); + + size_t name_field_width_; }; } // end namespace benchmark diff --git a/src/benchmark.cc b/src/benchmark.cc index 0c3fcb3d..69562fff 100644 --- a/src/benchmark.cc +++ b/src/benchmark.cc @@ -606,7 +606,7 @@ void RunInThread(const benchmark::internal::Benchmark::Instance* b, } void RunBenchmark(const benchmark::internal::Benchmark::Instance& b, - const BenchmarkReporter* br) EXCLUDES(GetBenchmarkLock()) { + BenchmarkReporter* br) EXCLUDES(GetBenchmarkLock()) { int iters = FLAGS_benchmark_iterations ? FLAGS_benchmark_iterations : 1; std::vector reports; @@ -763,7 +763,7 @@ void State::SetLabel(const char* label) { namespace internal { void RunMatchingBenchmarks(const std::string& spec, - const BenchmarkReporter* reporter) { + BenchmarkReporter* reporter) { CHECK(reporter != nullptr); if (spec.empty()) return; @@ -813,12 +813,15 @@ void RunSpecifiedBenchmarks() { RunSpecifiedBenchmarks(nullptr); } -void RunSpecifiedBenchmarks(const BenchmarkReporter* reporter) { +void RunSpecifiedBenchmarks(BenchmarkReporter* provided_reporter) { std::string spec = FLAGS_benchmark_filter; if (spec.empty() || spec == "all") spec = "."; // Regexp that matches all benchmarks ConsoleReporter default_reporter; - internal::RunMatchingBenchmarks(spec, reporter ? reporter : &default_reporter); + BenchmarkReporter* reporter = provided_reporter ? provided_reporter + : &default_reporter; + internal::RunMatchingBenchmarks(spec, reporter); + reporter->Finalize(); } namespace internal { diff --git a/src/reporter.cc b/src/reporter.cc index d9e62edb..27258c22 100644 --- a/src/reporter.cc +++ b/src/reporter.cc @@ -85,10 +85,13 @@ void ComputeStats(const std::vector& reports, } // end namespace +void BenchmarkReporter::Finalize() { +} -BenchmarkReporter::~BenchmarkReporter() {} +BenchmarkReporter::~BenchmarkReporter() { +} -bool ConsoleReporter::ReportContext(const Context& context) const { +bool ConsoleReporter::ReportContext(const Context& context) { name_field_width_ = context.name_field_width; fprintf(stdout, @@ -125,8 +128,7 @@ bool ConsoleReporter::ReportContext(const Context& context) const { return true; } -void ConsoleReporter::ReportRuns( - const std::vector& reports) const { +void ConsoleReporter::ReportRuns(const std::vector& reports) { if (reports.empty()) { return; } @@ -151,7 +153,7 @@ void ConsoleReporter::ReportRuns( fprintf(stdout, "\n"); } -void ConsoleReporter::PrintRunData(const Run& result) const { +void ConsoleReporter::PrintRunData(const Run& result) { // Format bytes per second std::string rate; if (result.bytes_per_second > 0) { diff --git a/test/filter_test.cc b/test/filter_test.cc index d2630891..931a534c 100644 --- a/test/filter_test.cc +++ b/test/filter_test.cc @@ -23,11 +23,11 @@ double CalculatePi(int depth) { class TestReporter : public benchmark::ConsoleReporter { public: - virtual bool ReportContext(const Context& context) const { + virtual bool ReportContext(const Context& context) { return ConsoleReporter::ReportContext(context); }; - virtual void ReportRuns(const std::vector& report) const { + virtual void ReportRuns(const std::vector& report) { ++count_; ConsoleReporter::ReportRuns(report); };