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