CppBenchmark  1.0.4.0
C++ Benchmark Library
phase_core.cpp
Go to the documentation of this file.
1 
9 #include "benchmark/phase_core.h"
10 
11 #include <algorithm>
12 
13 namespace CppBenchmark {
14 
15 std::shared_ptr<Phase> PhaseCore::StartPhase(const std::string& phase)
16 {
17  std::shared_ptr<PhaseCore> result;
18 
19  // Find or create a sub phase with the given name
20  auto it = std::find_if(_child.begin(), _child.end(), [&phase](const std::shared_ptr<PhaseCore>& item) { return item->name() == phase; });
21  if (it == _child.end())
22  {
23  result = std::make_shared<PhaseCore>(phase);
24  _child.emplace_back(result);
25  }
26  else
27  result = *it;
28 
29  // Start new operation for the child phase
30  result->StartCollectingMetrics();
31 
32  // Add new metrics operation
33  result->_metrics_current.AddOperations(1);
34 
35  return result;
36 }
37 
38 std::shared_ptr<Phase> PhaseCore::StartPhaseThreadSafe(const std::string& phase)
39 {
40  std::shared_ptr<PhaseCore> result;
41 
42  // Update phase collection under lock guard...
43  {
44  std::scoped_lock lock(_mutex);
45 
46  // Find or create a sub phase with the given name
47  auto it = std::find_if(_child.begin(), _child.end(), [&phase](const std::shared_ptr<PhaseCore>& item)
48  {
49  return ((item->name() == phase) && (item->_thread == System::CurrentThreadId()));
50  });
51  if (it == _child.end())
52  {
53  result = std::make_shared<PhaseCore>(phase);
54  _child.emplace_back(result);
55  }
56  else
57  result = *it;
58  }
59 
60  // Start new operation for the child phase
61  result->StartCollectingMetrics();
62 
63  // Add new metrics operation
64  result->_metrics_current.AddOperations(1);
65 
66  return result;
67 }
68 
69 } // namespace CppBenchmark
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
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
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
C++ Benchmark project definitions.
Definition: barrier.h:15
Benchmark phase core definition.