CppBenchmark  1.0.4.0
C++ Benchmark Library
phase_core.h
Go to the documentation of this file.
1 
9 #ifndef CPPBENCHMARK_PHASE_CORE_H
10 #define CPPBENCHMARK_PHASE_CORE_H
11 
13 #include "benchmark/phase_scope.h"
14 #include "benchmark/system.h"
15 
16 #include <limits>
17 #include <mutex>
18 #include <vector>
19 
20 namespace CppBenchmark {
21 
23 
26 class PhaseCore : public Phase
27 {
28  friend class BenchmarkBase;
29  friend class Benchmark;
30  friend class BenchmarkPC;
31  friend class BenchmarkThreads;
32  friend class Executor;
33  friend class Launcher;
34 
35 public:
37 
40  explicit PhaseCore(const std::string& name) : _name(name), _thread(System::CurrentThreadId())
41  { _metrics_result._total_time = std::numeric_limits<int64_t>::max(); }
42  PhaseCore(const PhaseCore&) = delete;
43  PhaseCore(PhaseCore&&) = delete;
44  virtual ~PhaseCore() = default;
45 
46  PhaseCore& operator=(const PhaseCore&) = delete;
48 
51 
52  // Implementation of Phase
53  const std::string& name() const noexcept override { return _name; }
54  const PhaseMetrics& metrics() const noexcept override { return _metrics_result; }
55  std::shared_ptr<Phase> StartPhase(const std::string& phase) override;
56  std::shared_ptr<Phase> StartPhaseThreadSafe(const std::string& phase) override;
57  void StopPhase() override { StopCollectingMetrics(); }
58  std::shared_ptr<PhaseScope> ScopePhase(const std::string& phase) override
59  { return std::make_shared<PhaseScope>(StartPhase(phase)); }
60  std::shared_ptr<PhaseScope> ScopePhaseThreadSafe(const std::string& phase) override
61  { return std::make_shared<PhaseScope>(StartPhaseThreadSafe(phase)); }
62 
63 protected:
65  std::mutex _mutex;
67  std::string _name;
69  uint64_t _thread;
71  std::vector<std::shared_ptr<PhaseCore>> _child;
76 
78 
81  void InitLatencyHistogram(const std::tuple<int64_t, int64_t, int>& latency) noexcept
82  { _metrics_current.InitLatencyHistogram(latency); }
84 
88  void PrintLatencyHistogram(FILE* file, int32_t resolution) const noexcept
89  { _metrics_result.PrintLatencyHistogram(file, resolution); }
90 
92  void StartCollectingMetrics() noexcept
93  { _metrics_current.StartCollecting(); }
95  void StopCollectingMetrics() noexcept
96  { _metrics_current.StopCollecting(); }
97 
99  void MergeMetrics()
100  { _metrics_result.MergeMetrics(_metrics_current); }
102  void MergeMetrics(PhaseCore& phase)
103  { _metrics_result.MergeMetrics(phase._metrics_result); }
105  void ResetMetrics() noexcept
106  { _metrics_current.ResetMetrics(); }
107 };
108 
109 } // namespace CppBenchmark
110 
111 #endif // CPPBENCHMARK_PHASE_CORE_H
Benchmark base class.
Benchmark class.
Definition: benchmark.h:22
Producers/Consumers benchmark base class.
Definition: benchmark_pc.h:26
Threads benchmark base class.
Dynamic benchmarks executor class.
Definition: executor.h:22
Launcher base class.
Definition: launcher.h:25
Benchmark phase core.
Definition: phase_core.h:27
PhaseCore(const PhaseCore &)=delete
void InitLatencyHistogram(const std::tuple< int64_t, int64_t, int > &latency) noexcept
Initialize latency histogram for the current phase.
Definition: phase_core.h:81
void StopPhase() override
Stop measurement of the current phase.
Definition: phase_core.h:57
PhaseCore(const std::string &name)
Create a new benchmark phase core with a given name.
Definition: phase_core.h:40
PhaseMetrics _metrics_current
Current phase metrics.
Definition: phase_core.h:73
void PrintLatencyHistogram(FILE *file, int32_t resolution) const noexcept
Print result latency histogram.
Definition: phase_core.h:88
void ResetMetrics() noexcept
Reset current phase metrics.
Definition: phase_core.h:105
const std::string & name() const noexcept override
Get phase name.
Definition: phase_core.h:53
std::shared_ptr< PhaseScope > ScopePhaseThreadSafe(const std::string &phase) override
Start a new sub-phase with a given name in a multi-thread environment and wrap it in a PhaseScope.
Definition: phase_core.h:60
std::shared_ptr< Phase > StartPhaseThreadSafe(const std::string &phase) override
Start a new sub-phase with a given name in a multi-thread environment.
Definition: phase_core.cpp:38
void StopCollectingMetrics() noexcept
Stop collecting metrics in the current phase.
Definition: phase_core.h:95
PhaseMetrics _metrics_result
Result phase metrics.
Definition: phase_core.h:75
virtual ~PhaseCore()=default
PhaseCore & operator=(const PhaseCore &)=delete
PhaseCore & operator=(PhaseCore &&)=delete
std::shared_ptr< PhaseScope > ScopePhase(const std::string &phase) override
Start a new sub-phase with a given name in a single-thread environment and wrap it in a PhaseScope.
Definition: phase_core.h:58
std::shared_ptr< Phase > StartPhase(const std::string &phase) override
Start a new sub-phase with a given name in a single-thread environment.
Definition: phase_core.cpp:15
const PhaseMetrics & metrics() const noexcept override
Get phase metrics.
Definition: phase_core.h:54
PhaseMetrics & current()
Current benchmark phase metrics.
Definition: phase_core.h:50
uint64_t _thread
Thread Id.
Definition: phase_core.h:69
void MergeMetrics(PhaseCore &phase)
Merge metrics of the two phases.
Definition: phase_core.h:102
std::string _name
Phase name.
Definition: phase_core.h:67
std::vector< std::shared_ptr< PhaseCore > > _child
Child phases container.
Definition: phase_core.h:71
std::mutex _mutex
Synchronization mutex.
Definition: phase_core.h:65
void StartCollectingMetrics() noexcept
Start collecting metrics in the current phase.
Definition: phase_core.h:92
PhaseCore(PhaseCore &&)=delete
void MergeMetrics()
Merge phase metrics (current to result)
Definition: phase_core.h:99
Benchmark phase base class.
Definition: phase.h:26
Benchmark phase metrics.
Definition: phase_metrics.h:37
System management static class.
Definition: system.h:22
C++ Benchmark project definitions.
Definition: barrier.h:15
Benchmark phase metrics definition.
Benchmark phase scope definition.
System management definition.