CppBenchmark  1.0.4.0
C++ Benchmark Library
executor.cpp
Go to the documentation of this file.
1 
9 #include "benchmark/executor.h"
10 
11 #include <algorithm>
12 
13 namespace CppBenchmark {
14 
16 namespace Internals {
17 
19 
22 class DynamicBenchmark : public BenchmarkBase
23 {
24 public:
26  typedef Settings TSettings;
27 
29 
33  explicit DynamicBenchmark(const std::string& name, const TSettings& settings = TSettings())
34  : BenchmarkBase(name, settings)
35  {}
36  DynamicBenchmark(const DynamicBenchmark&) = delete;
37  DynamicBenchmark(DynamicBenchmark&&) = delete;
38  virtual ~DynamicBenchmark() = default;
39 
40  DynamicBenchmark& operator=(const DynamicBenchmark&) = delete;
41  DynamicBenchmark& operator=(DynamicBenchmark&&) = delete;
42 };
43 
44 } // namespace Internals
46 
47 std::shared_ptr<Phase> Executor::StartBenchmark(const std::string& benchmark)
48 {
49  Executor& instance = GetInstance();
50 
51  std::shared_ptr<PhaseCore> result;
52 
53  // Update dynamic benchmarks collection under lock guard...
54  {
55  std::scoped_lock lock(instance._mutex);
56 
57  // Find or create a dynamic benchmark with the given name
58  auto it = std::find_if(instance._benchmarks.begin(), instance._benchmarks.end(), [&benchmark](const std::shared_ptr<PhaseCore>& item)
59  {
60  return ((item->name() == benchmark) && (item->_thread == System::CurrentThreadId()));
61  });
62  if (it == instance._benchmarks.end())
63  {
64  result = std::make_shared<PhaseCore>(benchmark);
65  instance._benchmarks.emplace_back(result);
66  }
67  else
68  result = *it;
69  }
70 
71  // Start new operation for the child dynamic benchmark
72  result->StartCollectingMetrics();
73 
74  // Add new metrics operation
75  result->_metrics_current.AddOperations(1);
76 
77  return result;
78 }
79 
80 void Executor::StopBenchmark(const std::string& benchmark)
81 {
82  Executor& instance = GetInstance();
83 
84  // Update dynamic benchmarks collection under lock guard...
85  {
86  std::scoped_lock lock(instance._mutex);
87 
88  // Find dynamic benchmark with the given name
89  auto it = std::find_if(instance._benchmarks.begin(), instance._benchmarks.end(), [&benchmark](const std::shared_ptr<PhaseCore>& item)
90  {
91  return ((item->name() == benchmark) && (item->_thread == System::CurrentThreadId()));
92  });
93  if (it != instance._benchmarks.end())
94  (*it)->StopCollectingMetrics();
95  }
96 }
97 
98 void Executor::Report(Reporter& reporter)
99 {
100  Executor& instance = GetInstance();
101 
102  std::scoped_lock lock(instance._mutex);
103 
106 
107  // Report header, system & environment
108  reporter.ReportHeader();
109  reporter.ReportSystem();
110  reporter.ReportEnvironment();
111  reporter.ReportBenchmarksHeader();
112 
113  // For all registered benchmarks...
114  for (const auto& benchmark : instance._benchmarks)
115  {
116  // Create dynamic benchmark wrapper
117  Internals::DynamicBenchmark result(benchmark->name(), Settings().Attempts(1));
118 
119  // Report benchmark results
120  reporter.ReportBenchmarkHeader();
121  reporter.ReportBenchmark(result, result.settings());
122  reporter.ReportPhasesHeader();
123  instance.ReportPhase(reporter, *benchmark, benchmark->name());
124  reporter.ReportPhasesFooter();
125  reporter.ReportBenchmarkFooter();
126  }
127 
128  // Report footer
129  reporter.ReportBenchmarksFooter();
130  reporter.ReportFooter();
131 }
132 
133 void Executor::ReportPhase(Reporter& reporter, const PhaseCore& phase, const std::string& name) const
134 {
135  reporter.ReportPhaseHeader();
136  reporter.ReportPhase(phase, phase.metrics());
137  reporter.ReportPhaseFooter();
138  for (const auto& child : phase._child)
139  {
140  std::string child_name = name + "." + child->name();
141  ReportPhase(reporter, *child, child_name);
142  }
143 }
144 
145 } // namespace CppBenchmark
static void UpdateBenchmarkThreads(std::vector< std::shared_ptr< PhaseCore >> &phases)
Update benchmark threads metrics for the given benchmark phases collection.
static void UpdateBenchmarkMetrics(std::vector< std::shared_ptr< PhaseCore >> &phases)
Update benchmark metrics for the given benchmark phases collection.
Dynamic benchmarks executor class.
Definition: executor.h:22
std::mutex _mutex
Synchronization mutex.
Definition: executor.h:71
static void StopBenchmark(const std::string &benchmark)
Stop dynamic benchmark with a given name.
Definition: executor.cpp:80
static std::shared_ptr< Phase > StartBenchmark(const std::string &benchmark)
Start a new dynamic benchmark with a given name.
Definition: executor.cpp:47
static void Report(Reporter &reporter)
Report benchmarks results using the given reporter.
Definition: executor.cpp:98
std::vector< std::shared_ptr< PhaseCore > > _benchmarks
Registered benchmarks collection.
Definition: executor.h:73
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
Benchmark settings.
Definition: settings.h:32
Dynamic benchmarks executor definition.
C++ Benchmark project definitions.
Definition: barrier.h:15