Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
benchmark.h
Go to the documentation of this file.
1 
18 #ifndef ION_ANALYTICS_BENCHMARK_H_
19 #define ION_ANALYTICS_BENCHMARK_H_
20 
21 #include <string>
22 #include <vector>
23 
24 #include "base/integral_types.h"
25 #include "ion/port/timer.h"
26 
27 namespace ion {
28 namespace analytics {
29 
34 class ION_API Benchmark {
35  public:
38  struct Descriptor {
39  Descriptor(const std::string& id_in,
40  const std::string& group_in,
41  const std::string& description_in,
42  const std::string& units_in)
43  : id(id_in),
44  group(group_in),
45  description(description_in),
46  units(units_in) {}
47  std::string id; // (Unique) identifying name.
48  std::string group; // Group the measurement belongs to.
49  std::string description; // Readable description.
50  std::string units; // Description of units.
51  };
52 
54  struct Constant {
55  Constant(const Descriptor& descriptor_in, double value_in)
56  : descriptor(descriptor_in),
57  value(value_in) {}
59  double value;
60  };
61 
65  struct Sample {
66  Sample(uint32 time_offset_ms_in, double value_in)
67  : time_offset_ms(time_offset_ms_in),
68  value(value_in) {}
70  double value;
71  };
72 
75  struct SampledVariable {
76  explicit SampledVariable(const Descriptor& descriptor_in)
77  : descriptor(descriptor_in) {}
79  std::vector<Sample> samples;
80  };
81 
85  AccumulatedVariable(const Descriptor& descriptor_in, size_t samples_in,
86  double minimum_in, double maximum_in,
87  double mean_in, double standard_deviation_in)
88  : descriptor(descriptor_in),
89  samples(samples_in),
90  minimum(minimum_in),
91  maximum(maximum_in),
92  mean(mean_in),
93  standard_deviation(standard_deviation_in) {}
95  size_t samples; // Number of samples taken.
96  double minimum; // Minimum value.
97  double maximum; // Maximum value.
98  double mean; // Average (mean) value.
99  double standard_deviation; // Standard deviation of value.
100  };
101 
103  class ION_API VariableSampler {
104  public:
105  explicit VariableSampler(const Descriptor& descriptor)
106  : variable_(descriptor) {}
107 
109  void AddSample(double value);
110 
112  const SampledVariable Get() { return variable_; }
113 
114  private:
115  SampledVariable variable_;
116 
118  ion::port::Timer timer_;
119  };
120 
122  class ION_API VariableAccumulator {
123  public:
124  explicit VariableAccumulator(const Descriptor& descriptor);
125 
127  void AddSample(double value);
128 
130  const AccumulatedVariable Get();
131 
132  private:
133  AccumulatedVariable variable_;
134  double m2_; // Needed by Welford's algorithm.
135  };
136 
139  void AddConstant(const Constant& constant) { constants_.push_back(constant); }
140  void AddSampledVariable(const SampledVariable& variable) {
141  sampled_variables_.push_back(variable);
142  }
144  accumulated_variables_.push_back(variable);
145  }
146 
148  const std::vector<Constant>& GetConstants() const { return constants_; }
149  const std::vector<SampledVariable>& GetSampledVariables() const {
150  return sampled_variables_;
151  }
152  const std::vector<AccumulatedVariable>& GetAccumulatedVariables() const {
153  return accumulated_variables_;
154  }
155 
158  static const AccumulatedVariable AccumulateSampledVariable(
159  const SampledVariable& sampled_variable);
160 
161  private:
162  std::vector<Constant> constants_;
163  std::vector<SampledVariable> sampled_variables_;
164  std::vector<AccumulatedVariable> accumulated_variables_;
165 };
166 
167 } // namespace analytics
168 } // namespace ion
169 
170 #endif // ION_ANALYTICS_BENCHMARK_H_
Sample(uint32 time_offset_ms_in, double value_in)
Definition: benchmark.h:66
AccumulatedVariable(const Descriptor &descriptor_in, size_t samples_in, double minimum_in, double maximum_in, double mean_in, double standard_deviation_in)
Definition: benchmark.h:85
void AddSampledVariable(const SampledVariable &variable)
Definition: benchmark.h:140
SampledVariable(const Descriptor &descriptor_in)
Definition: benchmark.h:76
double value
const std::vector< Constant > & GetConstants() const
Each of these returns the results for a given type of measurement.
Definition: benchmark.h:148
void AddAccumulatedVariable(const AccumulatedVariable &variable)
Definition: benchmark.h:143
const std::vector< SampledVariable > & GetSampledVariables() const
Definition: benchmark.h:149
This struct represents a single timestamped value of a variable.
Definition: benchmark.h:65
uint32 id
Constant(const Descriptor &descriptor_in, double value_in)
Definition: benchmark.h:55
This class aids in creation of a benchmarked SampledVariable.
Definition: benchmark.h:103
This struct represents a number that is constant over all samples.
Definition: benchmark.h:54
const std::vector< AccumulatedVariable > & GetAccumulatedVariables() const
Definition: benchmark.h:152
const SampledVariable Get()
Returns the resulting SampledVariable.
Definition: benchmark.h:112
VariableSampler(const Descriptor &descriptor)
Definition: benchmark.h:105
void AddConstant(const Constant &constant)
Each of these adds a measurement of a specific type to the benchmark results.
Definition: benchmark.h:139
This struct represents a variable: a number that may vary over samples, such as a count or timing...
Definition: benchmark.h:75
This struct stores information about a measurement computed by benchmarking.
Definition: benchmark.h:38
The Benchmark class provides types and utilities to make it easier to create performance benchmarks...
Definition: benchmark.h:34
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
Descriptor(const std::string &id_in, const std::string &group_in, const std::string &description_in, const std::string &units_in)
Definition: benchmark.h:39