CppBenchmark  1.0.4.0
C++ Benchmark Library
launcher.cpp
Go to the documentation of this file.
1 
9 #include "benchmark/launcher.h"
10 
11 #include <algorithm>
12 #include <regex>
13 
14 namespace CppBenchmark {
15 
16 void Launcher::Launch(const std::string& pattern)
17 {
18  int current = 0;
19  int total = 0;
20  std::vector<std::shared_ptr<BenchmarkBase>> benchmarks;
21 
22  // Build pending benchmark
23  for (const auto& builder : _builders)
24  AddBenchmark(builder());
25 
26  // Filter benchmarks
27  std::regex matcher(pattern);
28  for (const auto& benchmark : _benchmarks)
29  {
30  // Match benchmark name with the given pattern
31  if (pattern.empty() || std::regex_match(benchmark->name(), matcher))
32  {
33  total += benchmark->CountLaunches();
34  benchmarks.push_back(benchmark);
35  }
36  }
37 
38  // Launch filtered benchmarks
39  for (const auto& benchmark : benchmarks)
40  benchmark->Launch(current, total, *this);
41 }
42 
43 void Launcher::Report(Reporter& reporter) const
44 {
45  // Report header, system & environment
46  reporter.ReportHeader();
47  reporter.ReportSystem();
48  reporter.ReportEnvironment();
49  reporter.ReportBenchmarksHeader();
50 
51  // For all registered benchmarks...
52  for (const auto& benchmark : _benchmarks)
53  {
54  // Filter performed benchmarks
55  if (benchmark->_launched)
56  {
57  // Report benchmark results
58  reporter.ReportBenchmarkHeader();
59  reporter.ReportBenchmark(*benchmark, benchmark->settings());
60  reporter.ReportPhasesHeader();
61  for (const auto& root_phase : benchmark->_phases)
62  ReportPhase(reporter, *root_phase, root_phase->name());
63  reporter.ReportPhasesFooter();
64  reporter.ReportBenchmarkFooter();
65  }
66  }
67 
68  // Report footer
69  reporter.ReportBenchmarksFooter();
70  reporter.ReportFooter();
71 }
72 
73 void Launcher::ReportPhase(Reporter& reporter, const PhaseCore& phase, const std::string& name) const
74 {
75  reporter.ReportPhaseHeader();
76  reporter.ReportPhase(phase, phase.metrics());
77  reporter.ReportPhaseFooter();
78  for (const auto& child : phase._child)
79  {
80  std::string child_name = name + "." + child->name();
81  ReportPhase(reporter, *child, child_name);
82  }
83 }
84 
85 void Launcher::ReportHistograms(int32_t resolution) const
86 {
87  // For all registered benchmarks...
88  for (const auto& benchmark : _benchmarks)
89  {
90  // Filter performed benchmarks
91  if (benchmark->_launched)
92  {
93  // Report benchmark histograms
94  for (const auto& root_phase : benchmark->_phases)
95  ReportPhaseHistograms(resolution, *root_phase, root_phase->name());
96  }
97  }
98 }
99 
100 void Launcher::ReportPhaseHistograms(int32_t resolution, const PhaseCore& phase, const std::string& name) const
101 {
102  ReportPhaseHistogram(resolution, phase, name);
103  for (const auto& child : phase._child)
104  {
105  std::string child_name = name + "." + child->name();
106  ReportPhaseHistograms(resolution, *child, child_name);
107  }
108 }
109 
110 void Launcher::ReportPhaseHistogram(int32_t resolution, const PhaseCore& phase, const std::string& name) const
111 {
112  if (phase.metrics().latency())
113  {
114  const char deprecated[] = "\\/?%*:|\"<>";
115 
116  // Validate filename
117  std::string filename(name + ".hdr");
118  for (auto ch : filename)
119  if ((ch != '\\') && (ch != '/') && (std::find(deprecated, deprecated + sizeof(deprecated), ch) != (deprecated + sizeof(deprecated))))
120  ch = '_';
121 
122  // Open histogram filename
123  FILE* file = fopen(filename.c_str(), "w");
124  if (file != nullptr)
125  {
126  // Print histogram
127  phase.PrintLatencyHistogram(file, resolution);
128 
129  // Close file
130  fclose(file);
131  }
132  }
133 }
134 
135 } // namespace CppBenchmark
virtual void Launch(const std::string &pattern="")
Launch registered benchmarks.
Definition: launcher.cpp:16
std::vector< std::function< std::shared_ptr< BenchmarkBase >)> > _builders
Benchmark builders collection.
Definition: launcher.h:75
void Report(Reporter &reporter) const
Report benchmarks results using the given reporter.
Definition: launcher.cpp:43
void ReportHistograms(int32_t resolution) const
Report benchmarks High Dynamic Range (HDR) Histograms.
Definition: launcher.cpp:85
std::vector< std::shared_ptr< BenchmarkBase > > _benchmarks
Registered benchmarks collection.
Definition: launcher.h:73
void AddBenchmark(const std::shared_ptr< BenchmarkBase > &benchmark)
Add the given benchmark to the benchmarks collection.
Definition: launcher.h:39
Benchmark phase core.
Definition: phase_core.h:27
const PhaseMetrics & metrics() const noexcept override
Get phase metrics.
Definition: phase_core.h:54
std::vector< std::shared_ptr< PhaseCore > > _child
Child phases container.
Definition: phase_core.h:71
Reporter base class.
Definition: reporter.h:21
virtual void ReportBenchmarksFooter()
Report all benchmarks footer.
Definition: reporter.h:40
virtual void ReportPhase(const PhaseCore &phase, const PhaseMetrics &metrics)=0
Report current phase information.
virtual void ReportPhaseHeader()
Report current phase header.
Definition: reporter.h:56
virtual void ReportBenchmarkHeader()
Report current benchmark header.
Definition: reporter.h:42
virtual void ReportEnvironment()
Report environment information.
Definition: reporter.h:36
virtual void ReportHeader()
Report header.
Definition: reporter.h:32
virtual void ReportFooter()
Report footer.
Definition: reporter.h:66
virtual void ReportPhasesFooter()
Report all phases footer.
Definition: reporter.h:54
virtual void ReportBenchmarkFooter()
Report current benchmark footer.
Definition: reporter.h:44
virtual void ReportBenchmark(const BenchmarkBase &benchmark, const Settings &settings)
Report current benchmark information.
Definition: reporter.h:50
virtual void ReportBenchmarksHeader()
Report all benchmarks header.
Definition: reporter.h:38
virtual void ReportPhaseFooter()
Report current phase footer.
Definition: reporter.h:58
virtual void ReportPhasesHeader()
Report all phases header.
Definition: reporter.h:52
virtual void ReportSystem()
Report system information.
Definition: reporter.h:34
Launcher base definition.
C++ Benchmark project definitions.
Definition: barrier.h:15