reporter.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef BENCHMARK_REPORTER_H_
  15. #define BENCHMARK_REPORTER_H_
  16. #include <string>
  17. #include <utility>
  18. #include <vector>
  19. #include "benchmark_api.h" // For forward declaration of BenchmarkReporter
  20. namespace benchmark {
  21. // Interface for custom benchmark result printers.
  22. // By default, benchmark reports are printed to stdout. However an application
  23. // can control the destination of the reports by calling
  24. // RunSpecifiedBenchmarks and passing it a custom reporter object.
  25. // The reporter object must implement the following interface.
  26. class BenchmarkReporter {
  27. public:
  28. struct Context {
  29. int num_cpus;
  30. double mhz_per_cpu;
  31. bool cpu_scaling_enabled;
  32. // The number of chars in the longest benchmark name.
  33. size_t name_field_width;
  34. };
  35. struct Run {
  36. Run() :
  37. iterations(1),
  38. real_accumulated_time(0),
  39. cpu_accumulated_time(0),
  40. bytes_per_second(0),
  41. items_per_second(0),
  42. max_heapbytes_used(0) {}
  43. std::string benchmark_name;
  44. std::string report_label; // Empty if not set by benchmark.
  45. size_t iterations;
  46. double real_accumulated_time;
  47. double cpu_accumulated_time;
  48. // Zero if not set by benchmark.
  49. double bytes_per_second;
  50. double items_per_second;
  51. // This is set to 0.0 if memory tracing is not enabled.
  52. double max_heapbytes_used;
  53. };
  54. // Called once for every suite of benchmarks run.
  55. // The parameter "context" contains information that the
  56. // reporter may wish to use when generating its report, for example the
  57. // platform under which the benchmarks are running. The benchmark run is
  58. // never started if this function returns false, allowing the reporter
  59. // to skip runs based on the context information.
  60. virtual bool ReportContext(const Context& context) = 0;
  61. // Called once for each group of benchmark runs, gives information about
  62. // cpu-time and heap memory usage during the benchmark run.
  63. // Note that all the grouped benchmark runs should refer to the same
  64. // benchmark, thus have the same name.
  65. virtual void ReportRuns(const std::vector<Run>& report) = 0;
  66. // Called once and only once after ever group of benchmarks is run and
  67. // reported.
  68. virtual void Finalize();
  69. virtual ~BenchmarkReporter();
  70. protected:
  71. static void ComputeStats(std::vector<Run> const& reports, Run* mean, Run* stddev);
  72. };
  73. // Simple reporter that outputs benchmark data to the console. This is the
  74. // default reporter used by RunSpecifiedBenchmarks().
  75. class ConsoleReporter : public BenchmarkReporter {
  76. public:
  77. virtual bool ReportContext(const Context& context);
  78. virtual void ReportRuns(const std::vector<Run>& reports);
  79. protected:
  80. virtual void PrintRunData(const Run& report);
  81. size_t name_field_width_;
  82. };
  83. class JSONReporter : public BenchmarkReporter {
  84. public:
  85. JSONReporter() : first_report_(true) {}
  86. virtual bool ReportContext(const Context& context);
  87. virtual void ReportRuns(const std::vector<Run>& reports);
  88. virtual void Finalize();
  89. private:
  90. void PrintRunData(const Run& report);
  91. bool first_report_;
  92. };
  93. class CSVReporter : public BenchmarkReporter {
  94. public:
  95. virtual bool ReportContext(const Context& context);
  96. virtual void ReportRuns(const std::vector<Run>& reports);
  97. private:
  98. void PrintRunData(const Run& report);
  99. };
  100. } // end namespace benchmark
  101. #endif // BENCHMARK_REPORTER_H_