Apply reporter interface changes. Make report methods non-const and add a Finalize method.

This commit is contained in:
Eric Fiselier 2015-03-17 16:16:36 -04:00
parent b260cf7698
commit 20f1c0e2a8
5 changed files with 28 additions and 19 deletions

View File

@ -149,7 +149,7 @@ void Initialize(int* argc, const char** argv);
// Otherwise, run all benchmarks specified by the --benchmark_filter flag, // Otherwise, run all benchmarks specified by the --benchmark_filter flag,
// and exit after running the benchmarks. // and exit after running the benchmarks.
void RunSpecifiedBenchmarks(); void RunSpecifiedBenchmarks();
void RunSpecifiedBenchmarks(const BenchmarkReporter* reporter); void RunSpecifiedBenchmarks(BenchmarkReporter* reporter);
// If this routine is called, peak memory allocation past this point in the // 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 // benchmark is reported at the end of the benchmark report line. (It is

View File

@ -67,13 +67,17 @@ class BenchmarkReporter {
// platform under which the benchmarks are running. The benchmark run is // platform under which the benchmarks are running. The benchmark run is
// never started if this function returns false, allowing the reporter // never started if this function returns false, allowing the reporter
// to skip runs based on the context information. // 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 // Called once for each group of benchmark runs, gives information about
// cpu-time and heap memory usage during the benchmark run. // cpu-time and heap memory usage during the benchmark run.
// Note that all the grouped benchmark runs should refer to the same // Note that all the grouped benchmark runs should refer to the same
// benchmark, thus have the same name. // benchmark, thus have the same name.
virtual void ReportRuns(const std::vector<Run>& report) const = 0; virtual void ReportRuns(const std::vector<Run>& report) = 0;
// Called once and only once after ever group of benchmarks is run and
// reported.
virtual void Finalize();
virtual ~BenchmarkReporter(); virtual ~BenchmarkReporter();
}; };
@ -82,12 +86,12 @@ class BenchmarkReporter {
// default reporter used by RunSpecifiedBenchmarks(). // default reporter used by RunSpecifiedBenchmarks().
class ConsoleReporter : public BenchmarkReporter { class ConsoleReporter : public BenchmarkReporter {
public: public:
virtual bool ReportContext(const Context& context) const; virtual bool ReportContext(const Context& context);
virtual void ReportRuns(const std::vector<Run>& reports) const; virtual void ReportRuns(const std::vector<Run>& reports);
private: private:
virtual void PrintRunData(const Run& report) const; virtual void PrintRunData(const Run& report);
// TODO(ericwf): Find a better way to share this information.
mutable size_t name_field_width_; size_t name_field_width_;
}; };
} // end namespace benchmark } // end namespace benchmark

View File

@ -606,7 +606,7 @@ void RunInThread(const benchmark::internal::Benchmark::Instance* b,
} }
void RunBenchmark(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 int iters = FLAGS_benchmark_iterations ? FLAGS_benchmark_iterations
: 1; : 1;
std::vector<BenchmarkReporter::Run> reports; std::vector<BenchmarkReporter::Run> reports;
@ -763,7 +763,7 @@ void State::SetLabel(const char* label) {
namespace internal { namespace internal {
void RunMatchingBenchmarks(const std::string& spec, void RunMatchingBenchmarks(const std::string& spec,
const BenchmarkReporter* reporter) { BenchmarkReporter* reporter) {
CHECK(reporter != nullptr); CHECK(reporter != nullptr);
if (spec.empty()) return; if (spec.empty()) return;
@ -813,12 +813,15 @@ void RunSpecifiedBenchmarks() {
RunSpecifiedBenchmarks(nullptr); RunSpecifiedBenchmarks(nullptr);
} }
void RunSpecifiedBenchmarks(const BenchmarkReporter* reporter) { void RunSpecifiedBenchmarks(BenchmarkReporter* provided_reporter) {
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
ConsoleReporter default_reporter; 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 { namespace internal {

View File

@ -85,10 +85,13 @@ void ComputeStats(const std::vector<BenchmarkReporter::Run>& reports,
} // end namespace } // 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; name_field_width_ = context.name_field_width;
fprintf(stdout, fprintf(stdout,
@ -125,8 +128,7 @@ bool ConsoleReporter::ReportContext(const Context& context) const {
return true; return true;
} }
void ConsoleReporter::ReportRuns( void ConsoleReporter::ReportRuns(const std::vector<Run>& reports) {
const std::vector<Run>& reports) const {
if (reports.empty()) { if (reports.empty()) {
return; return;
} }
@ -151,7 +153,7 @@ void ConsoleReporter::ReportRuns(
fprintf(stdout, "\n"); fprintf(stdout, "\n");
} }
void ConsoleReporter::PrintRunData(const Run& result) const { void ConsoleReporter::PrintRunData(const Run& result) {
// Format bytes per second // Format bytes per second
std::string rate; std::string rate;
if (result.bytes_per_second > 0) { if (result.bytes_per_second > 0) {

View File

@ -23,11 +23,11 @@ double CalculatePi(int depth) {
class TestReporter : public benchmark::ConsoleReporter { class TestReporter : public benchmark::ConsoleReporter {
public: public:
virtual bool ReportContext(const Context& context) const { virtual bool ReportContext(const Context& context) {
return ConsoleReporter::ReportContext(context); return ConsoleReporter::ReportContext(context);
}; };
virtual void ReportRuns(const std::vector<Run>& report) const { virtual void ReportRuns(const std::vector<Run>& report) {
++count_; ++count_;
ConsoleReporter::ReportRuns(report); ConsoleReporter::ReportRuns(report);
}; };