libstatistics_collector  master
Lightweight aggregation utilities to collect statistics and measure message metrics.
moving_average.hpp
Go to the documentation of this file.
1 // Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef LIBSTATISTICS_COLLECTOR__MOVING_AVERAGE_STATISTICS__MOVING_AVERAGE_HPP_
16 #define LIBSTATISTICS_COLLECTOR__MOVING_AVERAGE_STATISTICS__MOVING_AVERAGE_HPP_
17 
18 #include <cmath>
19 
20 #include <algorithm>
21 #include <limits>
22 #include <mutex>
23 #include <numeric>
24 #include <type_traits>
25 
26 #include "types.hpp"
27 
29 
30 #include "rcpputils/thread_safety_annotations.hpp"
31 
33 {
34 namespace moving_average_statistics
35 {
36 
50 {
51 public:
53  MovingAverageStatistics() = default;
54 
56  ~MovingAverageStatistics() = default;
57 
64  double Average() const RCPPUTILS_TSA_REQUIRES(mutex_);
65 
72  double Max() const RCPPUTILS_TSA_REQUIRES(mutex_);
73 
80  double Min() const RCPPUTILS_TSA_REQUIRES(mutex_);
81 
91  double StandardDeviation() const RCPPUTILS_TSA_REQUIRES(mutex_);
92 
103 
108  void Reset();
109 
117  virtual void AddMeasurement(const double item);
118 
125  uint64_t GetCount() const;
126 
127 private:
128  mutable std::mutex mutex_;
129  double average_ RCPPUTILS_TSA_GUARDED_BY(mutex_) = 0;
130  double min_ RCPPUTILS_TSA_GUARDED_BY(mutex_) = std::numeric_limits<double>::max();
131  double max_ RCPPUTILS_TSA_GUARDED_BY(mutex_) = std::numeric_limits<double>::min();
132  double sum_of_square_diff_from_mean_ RCPPUTILS_TSA_GUARDED_BY(mutex_) = 0;
133  uint64_t count_ RCPPUTILS_TSA_GUARDED_BY(mutex_) = 0;
134 };
135 
136 } // namespace moving_average_statistics
137 } // namespace libstatistics_collector
138 
139 #endif // LIBSTATISTICS_COLLECTOR__MOVING_AVERAGE_STATISTICS__MOVING_AVERAGE_HPP_
types.hpp
libstatistics_collector::moving_average_statistics::MovingAverageStatistics::Min
double Min() const RCPPUTILS_TSA_REQUIRES(mutex_)
libstatistics_collector::moving_average_statistics::MovingAverageStatistics::~MovingAverageStatistics
~MovingAverageStatistics()=default
libstatistics_collector::moving_average_statistics::MovingAverageStatistics::Reset
void Reset()
libstatistics_collector
Definition: collector.hpp:29
libstatistics_collector::moving_average_statistics::MovingAverageStatistics::Average
double Average() const RCPPUTILS_TSA_REQUIRES(mutex_)
libstatistics_collector::moving_average_statistics::MovingAverageStatistics::GetCount
uint64_t GetCount() const
libstatistics_collector::moving_average_statistics::MovingAverageStatistics::MovingAverageStatistics
MovingAverageStatistics()=default
libstatistics_collector::moving_average_statistics::MovingAverageStatistics::StandardDeviation
double StandardDeviation() const RCPPUTILS_TSA_REQUIRES(mutex_)
libstatistics_collector::moving_average_statistics::MovingAverageStatistics::AddMeasurement
virtual void AddMeasurement(const double item)
libstatistics_collector::moving_average_statistics::MovingAverageStatistics::GetStatistics
StatisticData GetStatistics() const
visibility_control.hpp
libstatistics_collector::moving_average_statistics::MovingAverageStatistics::Max
double Max() const RCPPUTILS_TSA_REQUIRES(mutex_)
libstatistics_collector::moving_average_statistics::MovingAverageStatistics
Definition: moving_average.hpp:49
LIBSTATISTICS_COLLECTOR_PUBLIC
#define LIBSTATISTICS_COLLECTOR_PUBLIC
Definition: visibility_control.hpp:40
libstatistics_collector::moving_average_statistics::StatisticData
Definition: types.hpp:32