Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
benchmark.cc
Go to the documentation of this file.
1 
19 
20 #include <algorithm>
21 #include <chrono> // NOLINT
22 #include <limits>
23 
24 #include "ion/math/utils.h"
25 
26 namespace ion {
27 namespace analytics {
28 
30 
35 
36 
39  if (variable_.samples.empty()) {
40  timer_.Reset();
41  variable_.samples.push_back(Sample(0, value));
42  } else {
43  variable_.samples.push_back(Sample(
44  std::chrono::duration_cast<std::chrono::duration<uint32, std::milli>>(
45  timer_.Get())
46  .count(),
47  value));
48  }
49 }
50 
52 
57 
58 
60  const Descriptor& descriptor)
61  : variable_(descriptor, 0,
62  std::numeric_limits<double>::max(),
63  std::numeric_limits<double>::min(),
64  0.0, 0.0),
65  m2_(0.0) {
66 }
67 
69  ++variable_.samples;
70  variable_.minimum = std::min(variable_.minimum, value);
71  variable_.maximum = std::max(variable_.maximum, value);
72 
74  const double delta = value - variable_.mean;
75  variable_.mean += delta / static_cast<double>(variable_.samples);
76  m2_ += delta * (value - variable_.mean);
77 }
78 
82  variable_.standard_deviation =
83  variable_.samples ?
84  math::Sqrt(m2_ / static_cast<double>(variable_.samples - 1U)) : 0.0;
85 
86  return variable_;
87 }
88 
90 
95 
96 
98  const Benchmark::SampledVariable& sampled_variable) {
99  VariableAccumulator va(sampled_variable.descriptor);
100  const size_t num_samples = sampled_variable.samples.size();
101  for (size_t i = 0; i < num_samples; ++i)
102  va.AddSample(sampled_variable.samples[i].value);
103  return va.Get();
104 }
105 
106 } // namespace analytics
107 } // namespace ion
void AddSample(double value)
Adds one sample of the SampledVariable's value.
Definition: benchmark.cc:37
double value
void AddSample(double value)
Adds one sample of the Variable's value.
Definition: benchmark.cc:68
This struct represents a single timestamped value of a variable.
Definition: benchmark.h:65
T Sqrt(const T &val)
Returns the square root of a value.
Definition: utils.h:59
const AccumulatedVariable Get()
Returns the resulting Variable.
Definition: benchmark.cc:79
void Reset()
Resets the timer.
Definition: timer.cc:83
static const AccumulatedVariable AccumulateSampledVariable(const SampledVariable &sampled_variable)
Converts a SampledVariable to an AccumulatedVariable by accumulating all of the samples.
Definition: benchmark.cc:97
Clock::duration Get() const
Returns the elapsed time since construction or the last Reset().
Definition: timer.cc:85
This struct represents a variable: a number that may vary over samples, such as a count or timing...
Definition: benchmark.h:75
VariableAccumulator(const Descriptor &descriptor)
Benchmark::VariableAccumulator functions.
Definition: benchmark.cc:59
This struct stores information about a measurement computed by benchmarking.
Definition: benchmark.h:38
This class aids in accumulation of a benchmarked AccumulatedVariable.
Definition: benchmark.h:122
This struct represents accumulated values for a variable.
Definition: benchmark.h:84