Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 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 | |
Artem Titov | 9d77762 | 2020-09-18 18:23:08 +0200 | [diff] [blame] | 11 | #ifndef API_NUMERICS_SAMPLES_STATS_COUNTER_H_ |
| 12 | #define API_NUMERICS_SAMPLES_STATS_COUNTER_H_ |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 13 | |
Artem Titov | 44161f5 | 2022-09-19 12:25:19 +0200 | [diff] [blame] | 14 | #include <map> |
| 15 | #include <string> |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 16 | #include <vector> |
| 17 | |
Artem Titov | 89eaf16 | 2019-04-25 12:23:40 +0200 | [diff] [blame] | 18 | #include "api/array_view.h" |
Artem Titov | 6fcdbc1 | 2019-09-11 11:45:40 +0200 | [diff] [blame] | 19 | #include "api/units/timestamp.h" |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 21 | #include "rtc_base/numerics/running_statistics.h" |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 25 | // This class extends RunningStatistics by providing GetPercentile() method, |
| 26 | // while slightly adapting the interface. |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 27 | class SamplesStatsCounter { |
| 28 | public: |
Artem Titov | 6fcdbc1 | 2019-09-11 11:45:40 +0200 | [diff] [blame] | 29 | struct StatsSample { |
| 30 | double value; |
| 31 | Timestamp time; |
Artem Titov | 44161f5 | 2022-09-19 12:25:19 +0200 | [diff] [blame] | 32 | // Sample's specific metadata. |
| 33 | std::map<std::string, std::string> metadata; |
Artem Titov | 6fcdbc1 | 2019-09-11 11:45:40 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 36 | SamplesStatsCounter(); |
Artem Titov | 7fee2f7 | 2022-09-26 16:29:48 +0200 | [diff] [blame] | 37 | explicit SamplesStatsCounter(size_t expected_samples_count); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 38 | ~SamplesStatsCounter(); |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 39 | SamplesStatsCounter(const SamplesStatsCounter&); |
| 40 | SamplesStatsCounter& operator=(const SamplesStatsCounter&); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 41 | SamplesStatsCounter(SamplesStatsCounter&&); |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 42 | SamplesStatsCounter& operator=(SamplesStatsCounter&&); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 43 | |
| 44 | // Adds sample to the stats in amortized O(1) time. |
| 45 | void AddSample(double value); |
Artem Titov | 6fcdbc1 | 2019-09-11 11:45:40 +0200 | [diff] [blame] | 46 | void AddSample(StatsSample sample); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 47 | |
Sebastian Jansson | d93a004 | 2019-04-09 15:38:07 +0200 | [diff] [blame] | 48 | // Adds samples from another counter. |
| 49 | void AddSamples(const SamplesStatsCounter& other); |
| 50 | |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 51 | // Returns if there are any values in O(1) time. |
| 52 | bool IsEmpty() const { return samples_.empty(); } |
Artem Titov | cbe6e8a | 2020-09-22 15:45:00 +0200 | [diff] [blame] | 53 | // Returns the amount of samples added into counter in O(1) time. |
| 54 | int64_t NumSamples() const { return stats_.Size(); } |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 55 | |
| 56 | // Returns min in O(1) time. This function may not be called if there are no |
| 57 | // samples. |
| 58 | double GetMin() const { |
| 59 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 60 | return *stats_.GetMin(); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 61 | } |
| 62 | // Returns max in O(1) time. This function may not be called if there are no |
| 63 | // samples. |
| 64 | double GetMax() const { |
| 65 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 66 | return *stats_.GetMax(); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 67 | } |
| 68 | // Returns average in O(1) time. This function may not be called if there are |
| 69 | // no samples. |
| 70 | double GetAverage() const { |
| 71 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 72 | return *stats_.GetMean(); |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 73 | } |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 74 | // Returns variance in O(1) time. This function may not be called if there are |
| 75 | // no samples. |
| 76 | double GetVariance() const { |
| 77 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 78 | return *stats_.GetVariance(); |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 79 | } |
| 80 | // Returns standard deviation in O(1) time. This function may not be called if |
| 81 | // there are no samples. |
| 82 | double GetStandardDeviation() const { |
| 83 | RTC_DCHECK(!IsEmpty()); |
Yves Gerey | 890f62b | 2019-04-10 17:18:48 +0200 | [diff] [blame] | 84 | return *stats_.GetStandardDeviation(); |
Artem Titov | e6f6a0c | 2019-02-07 12:14:35 +0100 | [diff] [blame] | 85 | } |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 86 | // Returns percentile in O(nlogn) on first call and in O(1) after, if no |
| 87 | // additions were done. This function may not be called if there are no |
| 88 | // samples. |
| 89 | // |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 90 | // `percentile` has to be in [0; 1]. 0 percentile is the min in the array and |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 91 | // 1 percentile is the max in the array. |
| 92 | double GetPercentile(double percentile); |
Artem Titov | 89eaf16 | 2019-04-25 12:23:40 +0200 | [diff] [blame] | 93 | // Returns array view with all samples added into counter. There are no |
| 94 | // guarantees of order, so samples can be in different order comparing to in |
| 95 | // which they were added into counter. Also return value will be invalidate |
| 96 | // after call to any non const method. |
Artem Titov | 6fcdbc1 | 2019-09-11 11:45:40 +0200 | [diff] [blame] | 97 | rtc::ArrayView<const StatsSample> GetTimedSamples() const { return samples_; } |
| 98 | std::vector<double> GetSamples() const { |
| 99 | std::vector<double> out; |
| 100 | out.reserve(samples_.size()); |
| 101 | for (const auto& sample : samples_) { |
| 102 | out.push_back(sample.value); |
| 103 | } |
| 104 | return out; |
| 105 | } |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 106 | |
| 107 | private: |
Artem Titov | 9d77762 | 2020-09-18 18:23:08 +0200 | [diff] [blame] | 108 | webrtc_impl::RunningStatistics<double> stats_; |
Artem Titov | 6fcdbc1 | 2019-09-11 11:45:40 +0200 | [diff] [blame] | 109 | std::vector<StatsSample> samples_; |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 110 | bool sorted_ = false; |
| 111 | }; |
| 112 | |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 113 | // Multiply all sample values on `value` and return new SamplesStatsCounter |
Artem Titov | 2c5af4f | 2019-07-03 10:40:16 +0200 | [diff] [blame] | 114 | // with resulted samples. Doesn't change origin SamplesStatsCounter. |
| 115 | SamplesStatsCounter operator*(const SamplesStatsCounter& counter, double value); |
| 116 | inline SamplesStatsCounter operator*(double value, |
| 117 | const SamplesStatsCounter& counter) { |
| 118 | return counter * value; |
| 119 | } |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame] | 120 | // Divide all sample values on `value` and return new SamplesStatsCounter with |
Artem Titov | 2c5af4f | 2019-07-03 10:40:16 +0200 | [diff] [blame] | 121 | // resulted samples. Doesn't change origin SamplesStatsCounter. |
| 122 | SamplesStatsCounter operator/(const SamplesStatsCounter& counter, double value); |
| 123 | |
Artem Titov | e4ed6ea | 2019-01-11 11:02:19 +0100 | [diff] [blame] | 124 | } // namespace webrtc |
| 125 | |
Artem Titov | 9d77762 | 2020-09-18 18:23:08 +0200 | [diff] [blame] | 126 | #endif // API_NUMERICS_SAMPLES_STATS_COUNTER_H_ |