samples_stats_counter.h (4742B)
1 /* 2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef API_NUMERICS_SAMPLES_STATS_COUNTER_H_ 12 #define API_NUMERICS_SAMPLES_STATS_COUNTER_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <map> 18 #include <string> 19 #include <vector> 20 21 #include "api/array_view.h" 22 #include "api/units/timestamp.h" 23 #include "rtc_base/checks.h" 24 #include "rtc_base/numerics/running_statistics.h" 25 26 namespace webrtc { 27 28 // This class extends RunningStatistics by providing GetPercentile() method, 29 // while slightly adapting the interface. 30 class SamplesStatsCounter { 31 public: 32 struct StatsSample { 33 double value; 34 Timestamp time; 35 // Sample's specific metadata. 36 std::map<std::string, std::string> metadata; 37 }; 38 39 SamplesStatsCounter(); 40 explicit SamplesStatsCounter(size_t expected_samples_count); 41 ~SamplesStatsCounter(); 42 SamplesStatsCounter(const SamplesStatsCounter&); 43 SamplesStatsCounter& operator=(const SamplesStatsCounter&); 44 SamplesStatsCounter(SamplesStatsCounter&&); 45 SamplesStatsCounter& operator=(SamplesStatsCounter&&); 46 47 // Adds sample to the stats in amortized O(1) time. 48 [[deprecated("Use AddSample(StatsSample) instead.")]] 49 void AddSample(double value); 50 void AddSample(StatsSample sample); 51 52 // Adds samples from another counter. 53 void AddSamples(const SamplesStatsCounter& other); 54 55 // Returns if there are any values in O(1) time. 56 bool IsEmpty() const { return samples_.empty(); } 57 // Returns the amount of samples added into counter in O(1) time. 58 int64_t NumSamples() const { return stats_.Size(); } 59 60 // Returns min in O(1) time. This function may not be called if there are no 61 // samples. 62 double GetMin() const { 63 RTC_DCHECK(!IsEmpty()); 64 return *stats_.GetMin(); 65 } 66 // Returns max in O(1) time. This function may not be called if there are no 67 // samples. 68 double GetMax() const { 69 RTC_DCHECK(!IsEmpty()); 70 return *stats_.GetMax(); 71 } 72 // Returns sum in O(1) time. This function may not be called if there are 73 // no samples. 74 double GetSum() const { 75 RTC_DCHECK(!IsEmpty()); 76 return *stats_.GetSum(); 77 } 78 // Returns average in O(1) time. This function may not be called if there are 79 // no samples. 80 double GetAverage() const { 81 RTC_DCHECK(!IsEmpty()); 82 return *stats_.GetMean(); 83 } 84 // Returns variance in O(1) time. This function may not be called if there are 85 // no samples. 86 double GetVariance() const { 87 RTC_DCHECK(!IsEmpty()); 88 return *stats_.GetVariance(); 89 } 90 // Returns standard deviation in O(1) time. This function may not be called if 91 // there are no samples. 92 double GetStandardDeviation() const { 93 RTC_DCHECK(!IsEmpty()); 94 return *stats_.GetStandardDeviation(); 95 } 96 // Returns percentile in O(nlogn) on first call and in O(1) after, if no 97 // additions were done. This function may not be called if there are no 98 // samples. 99 // 100 // `percentile` has to be in [0; 1]. 0 percentile is the min in the array and 101 // 1 percentile is the max in the array. 102 double GetPercentile(double percentile); 103 // Returns array view with all samples added into counter. There are no 104 // guarantees of order, so samples can be in different order comparing to in 105 // which they were added into counter. Also return value will be invalidate 106 // after call to any non const method. 107 ArrayView<const StatsSample> GetTimedSamples() const { return samples_; } 108 std::vector<double> GetSamples() const { 109 std::vector<double> out; 110 out.reserve(samples_.size()); 111 for (const auto& sample : samples_) { 112 out.push_back(sample.value); 113 } 114 return out; 115 } 116 117 private: 118 webrtc_impl::RunningStatistics<double> stats_; 119 std::vector<StatsSample> samples_; 120 bool sorted_ = false; 121 }; 122 123 // Multiply all sample values on `value` and return new SamplesStatsCounter 124 // with resulted samples. Doesn't change origin SamplesStatsCounter. 125 SamplesStatsCounter operator*(const SamplesStatsCounter& counter, double value); 126 inline SamplesStatsCounter operator*(double value, 127 const SamplesStatsCounter& counter) { 128 return counter * value; 129 } 130 // Divide all sample values on `value` and return new SamplesStatsCounter with 131 // resulted samples. Doesn't change origin SamplesStatsCounter. 132 SamplesStatsCounter operator/(const SamplesStatsCounter& counter, double value); 133 134 } // namespace webrtc 135 136 #endif // API_NUMERICS_SAMPLES_STATS_COUNTER_H_