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