OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 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 WEBRTC_VIDEO_STATS_COUNTER_H_ |
| 12 #define WEBRTC_VIDEO_STATS_COUNTER_H_ |
| 13 |
| 14 #include <memory> |
| 15 |
| 16 #include "webrtc/base/constructormagic.h" |
| 17 #include "webrtc/typedefs.h" |
| 18 |
| 19 namespace webrtc { |
| 20 |
| 21 class AggregatedCounter; |
| 22 class Clock; |
| 23 |
| 24 // |StatsCounterObserver| is called periodically when a metric is updated. |
| 25 class StatsCounterObserver { |
| 26 public: |
| 27 virtual void OnMetricUpdated(int sample) = 0; |
| 28 |
| 29 virtual ~StatsCounterObserver() {} |
| 30 }; |
| 31 |
| 32 struct AggregatedStats { |
| 33 int64_t num_samples = 0; |
| 34 int min = -1; |
| 35 int max = -1; |
| 36 int average = -1; |
| 37 // TODO(asapersson): Consider adding median/percentiles. |
| 38 }; |
| 39 |
| 40 // Classes which periodically computes a metric. |
| 41 // |
| 42 // During a period, |kProcessIntervalMs|, different metrics can be computed e.g: |
| 43 // - |AvgCounter|: average of samples |
| 44 // - |PercentCounter|: percentage of samples |
| 45 // - |PermilleCounter|: permille of samples |
| 46 // |
| 47 // Each periodic metric can be either: |
| 48 // - reported to an |observer| each period |
| 49 // - aggregated during the call (e.g. min, max, average) |
| 50 // |
| 51 // periodically computed |
| 52 // GetMetric() GetMetric() => AggregatedStats |
| 53 // ^ ^ (e.g. min/max/avg) |
| 54 // | | |
| 55 // | * * * * | ** * * * * | ... |
| 56 // |<- process interval ->| |
| 57 // |
| 58 // (*) - samples |
| 59 // |
| 60 // |
| 61 // Example usage: |
| 62 // |
| 63 // AvgCounter counter(&clock, nullptr); |
| 64 // counter.Add(5); |
| 65 // counter.Add(1); |
| 66 // counter.Add(6); // process interval passed -> GetMetric() avg:4 |
| 67 // counter.Add(7); |
| 68 // counter.Add(3); // process interval passed -> GetMetric() avg:5 |
| 69 // counter.Add(10); |
| 70 // counter.Add(20); // process interval passed -> GetMetric() avg:15 |
| 71 // AggregatedStats stats = counter.GetStats(); |
| 72 // stats: {min:4, max:15, avg:8} |
| 73 // |
| 74 |
| 75 // Note: StatsCounter takes ownership of |observer|. |
| 76 |
| 77 class StatsCounter { |
| 78 public: |
| 79 virtual ~StatsCounter(); |
| 80 |
| 81 virtual bool GetMetric(int* metric) const = 0; |
| 82 |
| 83 AggregatedStats GetStats(); |
| 84 |
| 85 protected: |
| 86 StatsCounter(Clock* clock, |
| 87 bool include_empty_intervals, |
| 88 StatsCounterObserver* observer); |
| 89 |
| 90 void Add(int sample); |
| 91 void Set(int sample); |
| 92 |
| 93 int max_; |
| 94 int64_t sum_; |
| 95 int64_t num_samples_; |
| 96 int64_t last_sum_; |
| 97 |
| 98 private: |
| 99 bool TimeToProcess(); |
| 100 void TryProcess(); |
| 101 |
| 102 Clock* const clock_; |
| 103 const bool include_empty_intervals_; |
| 104 const std::unique_ptr<StatsCounterObserver> observer_; |
| 105 const std::unique_ptr<AggregatedCounter> aggregated_counter_; |
| 106 int64_t last_process_time_ms_; |
| 107 }; |
| 108 |
| 109 // AvgCounter: average of samples |
| 110 // |
| 111 // | * * * | * * | ... |
| 112 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
| 113 // GetMetric | (5 + 1 + 6) / 3 | (5 + 5) / 2 | |
| 114 // |
| 115 class AvgCounter : public StatsCounter { |
| 116 public: |
| 117 AvgCounter(Clock* clock, StatsCounterObserver* observer); |
| 118 ~AvgCounter() override {} |
| 119 |
| 120 void Add(int sample); |
| 121 |
| 122 private: |
| 123 bool GetMetric(int* metric) const override; |
| 124 |
| 125 RTC_DISALLOW_COPY_AND_ASSIGN(AvgCounter); |
| 126 }; |
| 127 |
| 128 // MaxCounter: maximum of samples |
| 129 // |
| 130 // | * * * | * * | ... |
| 131 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
| 132 // GetMetric | max: (5, 1, 6) | max: (5, 5) | |
| 133 // |
| 134 class MaxCounter : public StatsCounter { |
| 135 public: |
| 136 MaxCounter(Clock* clock, StatsCounterObserver* observer); |
| 137 ~MaxCounter() override {} |
| 138 |
| 139 void Add(int sample); |
| 140 |
| 141 private: |
| 142 bool GetMetric(int* metric) const override; |
| 143 |
| 144 RTC_DISALLOW_COPY_AND_ASSIGN(MaxCounter); |
| 145 }; |
| 146 |
| 147 // PercentCounter: percentage of samples |
| 148 // |
| 149 // | * * * | * * | ... |
| 150 // | Add(T) Add(F) Add(T) | Add(F) Add(T) | |
| 151 // GetMetric | 100 * 2 / 3 | 100 * 1 / 2 | |
| 152 // |
| 153 class PercentCounter : public StatsCounter { |
| 154 public: |
| 155 PercentCounter(Clock* clock, StatsCounterObserver* observer); |
| 156 ~PercentCounter() override {} |
| 157 |
| 158 void Add(bool sample); |
| 159 |
| 160 private: |
| 161 bool GetMetric(int* metric) const override; |
| 162 |
| 163 RTC_DISALLOW_COPY_AND_ASSIGN(PercentCounter); |
| 164 }; |
| 165 |
| 166 // PermilleCounter: permille of samples |
| 167 // |
| 168 // | * * * | * * | ... |
| 169 // | Add(T) Add(F) Add(T) | Add(F) Add(T) | |
| 170 // GetMetric | 1000 * 2 / 3 | 1000 * 1 / 2 | |
| 171 // |
| 172 class PermilleCounter : public StatsCounter { |
| 173 public: |
| 174 PermilleCounter(Clock* clock, StatsCounterObserver* observer); |
| 175 ~PermilleCounter() override {} |
| 176 |
| 177 void Add(bool sample); |
| 178 |
| 179 private: |
| 180 bool GetMetric(int* metric) const override; |
| 181 |
| 182 RTC_DISALLOW_COPY_AND_ASSIGN(PermilleCounter); |
| 183 }; |
| 184 |
| 185 // RateCounter: units per second |
| 186 // |
| 187 // | * * * | * * | ... |
| 188 // | Add(5) Add(1) Add(6) | Add(5) Add(5) | |
| 189 // |<------ 2 sec ------->| | |
| 190 // GetMetric | (5 + 1 + 6) / 2 | (5 + 5) / 2 | |
| 191 // |
| 192 class RateCounter : public StatsCounter { |
| 193 public: |
| 194 RateCounter(Clock* clock, StatsCounterObserver* observer); |
| 195 ~RateCounter() override {} |
| 196 |
| 197 void Add(int sample); |
| 198 |
| 199 private: |
| 200 bool GetMetric(int* metric) const override; |
| 201 |
| 202 RTC_DISALLOW_COPY_AND_ASSIGN(RateCounter); |
| 203 }; |
| 204 |
| 205 // RateAccCounter: units per second (used for counters) |
| 206 // |
| 207 // | * * * | * * | ... |
| 208 // | Set(5) Set(6) Set(8) | Set(11) Set(13) | |
| 209 // |<------ 2 sec ------->| | |
| 210 // GetMetric | 8 / 2 | (13 - 8) / 2 | |
| 211 // |
| 212 class RateAccCounter : public StatsCounter { |
| 213 public: |
| 214 RateAccCounter(Clock* clock, StatsCounterObserver* observer); |
| 215 ~RateAccCounter() override {} |
| 216 |
| 217 void Set(int sample); |
| 218 |
| 219 private: |
| 220 bool GetMetric(int* metric) const override; |
| 221 |
| 222 RTC_DISALLOW_COPY_AND_ASSIGN(RateAccCounter); |
| 223 }; |
| 224 |
| 225 } // namespace webrtc |
| 226 |
| 227 #endif // WEBRTC_VIDEO_STATS_COUNTER_H_ |
OLD | NEW |