CppBenchmark  1.0.4.0
C++ Benchmark Library
benchmark.cpp
Go to the documentation of this file.
1 
9 #include "benchmark/benchmark.h"
10 
12 
13 namespace CppBenchmark {
14 
15 int Benchmark::CountLaunches() const
16 {
17  return _settings.attempts() * (_settings.params().empty() ? 1 : (int)_settings.params().size());
18 }
19 
20 void Benchmark::Launch(int& current, int total, LauncherHandler& handler)
21 {
22  // Make several attempts of execution...
23  for (int attempt = 1; attempt <= _settings.attempts(); ++attempt)
24  {
25  // Run benchmark at least once
26  if (_settings._params.empty())
27  _settings._params.emplace_back(-1, -1, -1);
28 
29  // Run benchmark for every input parameter (single, pair, triple)
30  for (const auto& param : _settings.params())
31  {
32  // Prepare benchmark context
33  Context context(std::get<0>(param), std::get<1>(param), std::get<2>(param));
34 
35  // Initialize the current benchmark
36  InitBenchmarkContext(context);
37 
38  // Initialize latency histogram of the current phase
39  std::tuple<int64_t, int64_t, int> latency_params(_settings.latency());
40  bool latency_auto = _settings.latency_auto();
41  context._current->InitLatencyHistogram(latency_params);
42 
43  // Call launching notification...
44  handler.onLaunching(++current, total, *this, context, attempt);
45 
46  // Call initialize benchmark method...
47  Initialize(context);
48 
49  bool infinite = _settings.infinite();
50  int64_t duration = _settings.duration();
51  int64_t operations = _settings.operations();
52 
53  uint64_t timestamp = 0;
54 
55  // Calculate the approximate count of operations which can be performed for the given duration
56  if (duration > 0)
57  {
58  uint64_t count = 0;
59  uint64_t timespan = 0;
60 
61  // Collect data for one second...
62  for (; timespan < 1000000000; ++count)
63  {
64  timestamp = System::Timestamp();
65 
66  // Run benchmark method...
67  Run(context);
68 
69  timespan += System::Timestamp() - timestamp;
70  }
71 
72  // Approximate operations count
73  operations = (1000000000ull * count * duration) / timespan;
74  }
75 
76  context._current->StartCollectingMetrics();
77  while (!context.canceled() && (infinite || (operations > 0)))
78  {
79  // Add new metrics operation
80  context._metrics->AddOperations(1);
81 
82  // Store the timestamp for the automatic latency update
83  if (latency_auto)
84  timestamp = System::Timestamp();
85 
86  // Run benchmark method...
87  Run(context);
88 
89  // Update latency metrics
90  if (latency_auto)
91  context._metrics->AddLatency(System::Timestamp() - timestamp);
92 
93  // Decrement operation counters
94  operations -= 1;
95  }
96  context._current->StopCollectingMetrics();
97 
98  // Call cleanup benchmark method...
99  Cleanup(context);
100 
101  // Call launched notification...
102  handler.onLaunched(current, total, *this, context, attempt);
103  }
104 
105  // Update benchmark root metrics for the current attempt
107  }
108 
109  // Update benchmark threads
111 
112  // Update benchmark names
114 
115  // Update benchmark launched flag
116  _launched = true;
117 }
118 
119 } // namespace CppBenchmark
Benchmark definition.
bool _launched
Benchmark launched flag.
Settings _settings
Benchmark settings.
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.
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.
virtual void Run(Context &context)=0
Benchmark run method.
virtual void Initialize(Context &context)
Initialize benchmark.
Definition: fixture.h:38
virtual void Cleanup(Context &context)
Cleanup benchmark.
Definition: fixture.h:45
const std::vector< std::tuple< int, int, int > > & params() const noexcept
Get collection of independent parameters in a benchmark plan.
Definition: settings.h:65
int attempts() const noexcept
Get count of independent benchmark attempts.
Definition: settings.h:53
int64_t duration() const noexcept
Get benchmark duration in milliseconds.
Definition: settings.h:57
bool infinite() const noexcept
Is benchmark running with infinite count of operations (until cancel)?
Definition: settings.h:55
const std::tuple< int64_t, int64_t, int > & latency() const noexcept
Get latency parameters.
Definition: settings.h:67
int64_t operations() const noexcept
Get count of operations.
Definition: settings.h:59
bool latency_auto() const noexcept
Get automatic latency update flag.
Definition: settings.h:69
static uint64_t Timestamp()
Get the current timestamp in nanoseconds.
Definition: system.cpp:378
Launcher handler definition.
C++ Benchmark project definitions.
Definition: barrier.h:15