libstatistics_collector  master
Lightweight aggregation utilities to collect statistics and measure message metrics.
All Classes Functions Variables Pages
moving_average.hpp
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 
28 #include "libstatistics_collector/visibility_control.hpp"
29 
30 #include "rcpputils/thread_safety_annotations.hpp"
31 
32 namespace libstatistics_collector
33 {
34 namespace moving_average_statistics
35 {
36 
50 {
51 public:
52  LIBSTATISTICS_COLLECTOR_PUBLIC
53  MovingAverageStatistics() = default;
54 
55  LIBSTATISTICS_COLLECTOR_PUBLIC
56  ~MovingAverageStatistics() = default;
57 
63  LIBSTATISTICS_COLLECTOR_PUBLIC
64  double Average() const RCPPUTILS_TSA_REQUIRES(mutex_);
65 
71  LIBSTATISTICS_COLLECTOR_PUBLIC
72  double Max() const RCPPUTILS_TSA_REQUIRES(mutex_);
73 
79  LIBSTATISTICS_COLLECTOR_PUBLIC
80  double Min() const RCPPUTILS_TSA_REQUIRES(mutex_);
81 
90  LIBSTATISTICS_COLLECTOR_PUBLIC
91  double StandardDeviation() const RCPPUTILS_TSA_REQUIRES(mutex_);
92 
101  LIBSTATISTICS_COLLECTOR_PUBLIC
103 
107  LIBSTATISTICS_COLLECTOR_PUBLIC
108  void Reset();
109 
116  LIBSTATISTICS_COLLECTOR_PUBLIC
117  virtual void AddMeasurement(const double item);
118 
124  LIBSTATISTICS_COLLECTOR_PUBLIC
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_
libstatistics_collector::moving_average_statistics::MovingAverageStatistics::Min
double Min() const RCPPUTILS_TSA_REQUIRES(mutex_)
libstatistics_collector::moving_average_statistics::MovingAverageStatistics::Reset
void Reset()
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::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
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::moving_average_statistics::StatisticData
Definition: types.hpp:32