CppBenchmark  1.0.4.0
C++ Benchmark Library
benchmark_base.cpp
Go to the documentation of this file.
1 
10 
11 #include <algorithm>
12 
13 namespace CppBenchmark {
14 
16 {
17  std::string name = _name + context.description();
18  std::shared_ptr<PhaseCore> result;
19 
20  // Find or create root phase for the given context
21  auto it = std::find_if(_phases.begin(), _phases.end(), [&name] (const std::shared_ptr<PhaseCore>& item) { return item->name() == name; });
22  if (it == _phases.end())
23  {
24  result = std::make_shared<PhaseCore>(name);
25  _phases.emplace_back(result);
26  }
27  else
28  result = *it;
29 
30  // Update the current benchmark
31  context._current = result.get();
32  context._metrics = &result->current();
33 }
34 
35 void BenchmarkBase::UpdateBenchmarkMetrics(std::vector<std::shared_ptr<PhaseCore>>& phases)
36 {
37  for (const auto& phase : phases)
38  UpdateBenchmarkMetrics(*phase);
39 }
40 
42 {
43  for (const auto& child : phase._child)
44  UpdateBenchmarkMetrics(*child);
45  phase.MergeMetrics();
46  phase.ResetMetrics();
47 }
48 
49 void BenchmarkBase::UpdateBenchmarkThreads(std::vector<std::shared_ptr<PhaseCore>>& phases)
50 {
51  // Merge benchmark phases with a same name for different threads
52  for (auto it = phases.begin(); it != phases.end(); ++it)
53  {
54  for (auto next = it + 1; next != phases.end(); ++next)
55  {
56  if ((*it)->name() == (*next)->name())
57  {
58  // Merge metrics results
59  (*it)->MergeMetrics(**next);
60 
61  // Append child phases
62  (*it)->_child.insert((*it)->_child.end(), (*next)->_child.begin(), (*next)->_child.end());
63 
64  // Mark to delete
65  (*next)->_thread = 0;
66  }
67  }
68  }
69 
70  // Remove phases marked to delete
71  phases.erase(std::remove_if(phases.begin(), phases.end(), [](const std::shared_ptr<PhaseCore>& p) { return p->_thread == 0; }), phases.end());
72 
73  // Perform the same operation for child phases
74  for (const auto& phase : phases)
75  UpdateBenchmarkThreads(phase->_child);
76 }
77 
78 void BenchmarkBase::UpdateBenchmarkNames(std::vector<std::shared_ptr<PhaseCore>>& phases)
79 {
80  for (const auto& phase : phases)
81  UpdateBenchmarkNames(*phase, phase->name());
82 }
83 
84 void BenchmarkBase::UpdateBenchmarkNames(PhaseCore& phase, const std::string& name)
85 {
86  for (const auto& child : phase._child)
87  UpdateBenchmarkNames(*child, name + "." + child->name());
88  phase._name = name;
89 }
90 
91 void BenchmarkBase::UpdateBenchmarkOperations(std::vector<std::shared_ptr<PhaseCore>>& phases)
92 {
93  for (const auto& phase : phases)
95 }
96 
98 {
100  for (const auto& child : phase._child)
101  phase._metrics_result.AddOperations(child->metrics().threads() * child->metrics().total_operations());
102 }
103 
104 } // namespace CppBenchmark
Benchmark base definition.
static void UpdateBenchmarkNames(std::vector< std::shared_ptr< PhaseCore >> &phases)
Update benchmark names for the given benchmark phases collection.
void InitBenchmarkContext(Context &context)
Initialize benchmark context.
static void UpdateBenchmarkThreads(std::vector< std::shared_ptr< PhaseCore >> &phases)
Update benchmark threads metrics for the given benchmark phases collection.
const std::string & name() const
Get benchmark name.
std::string _name
Benchmark name.
static void UpdateBenchmarkOperations(std::vector< std::shared_ptr< PhaseCore >> &phases)
Update benchmark operations 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.
std::vector< std::shared_ptr< PhaseCore > > _phases
Benchmark phases.
Benchmark running context.
Definition: context.h:27
PhaseCore * _current
Current benchmark phase.
Definition: context.h:80
virtual std::string description() const
Get description of the current benchmark running context.
Definition: context.cpp:13
PhaseMetrics * _metrics
Current benchmark metrics.
Definition: context.h:82
Benchmark phase core.
Definition: phase_core.h:27
void ResetMetrics() noexcept
Reset current phase metrics.
Definition: phase_core.h:105
PhaseMetrics _metrics_result
Result phase metrics.
Definition: phase_core.h:75
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
void MergeMetrics()
Merge phase metrics (current to result)
Definition: phase_core.h:99
void AddOperations(int64_t operations) noexcept
Increase operations count of the current phase.
C++ Benchmark project definitions.
Definition: barrier.h:15