Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: webrtc/video/stats_counter_unittest.cc

Issue 1640053003: Add class which periodically computes statistics. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: address comment Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/video/stats_counter.cc ('k') | webrtc/video/webrtc_video.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "webrtc/video/stats_counter.h"
12
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 #include "webrtc/system_wrappers/include/clock.h"
16
17 namespace webrtc {
18 namespace {
19 const int kProcessIntervalMs = 2000;
20
21 class StatsCounterObserverImpl : public StatsCounterObserver {
22 public:
23 StatsCounterObserverImpl() : num_calls_(0), last_sample_(-1) {}
24 void OnMetricUpdated(int sample) override {
25 ++num_calls_;
26 last_sample_ = sample;
27 }
28 int num_calls_;
29 int last_sample_;
30 };
31 } // namespace
32
33 class StatsCounterTest : public ::testing::Test {
34 protected:
35 StatsCounterTest()
36 : clock_(1234) {}
37
38 void AddSampleAndAdvance(int sample, int interval_ms, AvgCounter* counter) {
39 counter->Add(sample);
40 clock_.AdvanceTimeMilliseconds(interval_ms);
41 }
42
43 void SetSampleAndAdvance(int sample,
44 int interval_ms,
45 RateAccCounter* counter) {
46 counter->Set(sample);
47 clock_.AdvanceTimeMilliseconds(interval_ms);
48 }
49
50 void VerifyStatsIsNotSet(const AggregatedStats& stats) {
51 EXPECT_EQ(0, stats.num_samples);
52 EXPECT_EQ(-1, stats.min);
53 EXPECT_EQ(-1, stats.max);
54 EXPECT_EQ(-1, stats.average);
55 }
56
57 SimulatedClock clock_;
58 };
59
60 TEST_F(StatsCounterTest, NoSamples) {
61 AvgCounter counter(&clock_, nullptr);
62 VerifyStatsIsNotSet(counter.GetStats());
63 }
64
65 TEST_F(StatsCounterTest, TestRegisterObserver) {
66 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
67 const int kSample = 22;
68 AvgCounter counter(&clock_, observer);
69 AddSampleAndAdvance(kSample, kProcessIntervalMs, &counter);
70 // Trigger process (sample included in next interval).
71 counter.Add(111);
72 EXPECT_EQ(1, observer->num_calls_);
73 }
74
75 TEST_F(StatsCounterTest, VerifyProcessInterval) {
76 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
77 AvgCounter counter(&clock_, observer);
78 counter.Add(4);
79 clock_.AdvanceTimeMilliseconds(kProcessIntervalMs - 1);
80 // Try trigger process (interval has not passed).
81 counter.Add(8);
82 EXPECT_EQ(0, observer->num_calls_);
83 VerifyStatsIsNotSet(counter.GetStats());
84 // Make process interval pass.
85 clock_.AdvanceTimeMilliseconds(1);
86 // Trigger process (sample included in next interval).
87 counter.Add(111);
88 EXPECT_EQ(1, observer->num_calls_);
89 EXPECT_EQ(6, observer->last_sample_);
90 // Aggregated stats.
91 AggregatedStats stats = counter.GetStats();
92 EXPECT_EQ(1, stats.num_samples);
93 }
94
95 TEST_F(StatsCounterTest, TestMetric_AvgCounter) {
96 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
97 AvgCounter counter(&clock_, observer);
98 counter.Add(4);
99 counter.Add(8);
100 counter.Add(9);
101 clock_.AdvanceTimeMilliseconds(kProcessIntervalMs);
102 // Trigger process (sample included in next interval).
103 counter.Add(111);
104 // Average per interval.
105 EXPECT_EQ(1, observer->num_calls_);
106 EXPECT_EQ(7, observer->last_sample_);
107 // Aggregated stats.
108 AggregatedStats stats = counter.GetStats();
109 EXPECT_EQ(1, stats.num_samples);
110 EXPECT_EQ(7, stats.min);
111 EXPECT_EQ(7, stats.max);
112 EXPECT_EQ(7, stats.average);
113 }
114
115 TEST_F(StatsCounterTest, TestMetric_MaxCounter) {
116 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
117 MaxCounter counter(&clock_, observer);
118 counter.Add(4);
119 counter.Add(9);
120 counter.Add(8);
121 clock_.AdvanceTimeMilliseconds(kProcessIntervalMs);
122 // Trigger process (sample included in next interval).
123 counter.Add(111);
124 // Average per interval.
125 EXPECT_EQ(1, observer->num_calls_);
126 EXPECT_EQ(9, observer->last_sample_);
127 // Aggregated stats.
128 AggregatedStats stats = counter.GetStats();
129 EXPECT_EQ(1, stats.num_samples);
130 EXPECT_EQ(9, stats.min);
131 EXPECT_EQ(9, stats.max);
132 EXPECT_EQ(9, stats.average);
133 }
134
135 TEST_F(StatsCounterTest, TestMetric_PercentCounter) {
136 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
137 PercentCounter counter(&clock_, observer);
138 counter.Add(true);
139 counter.Add(false);
140 clock_.AdvanceTimeMilliseconds(kProcessIntervalMs);
141 // Trigger process (sample included in next interval).
142 counter.Add(false);
143 // Percentage per interval.
144 EXPECT_EQ(1, observer->num_calls_);
145 EXPECT_EQ(50, observer->last_sample_);
146 // Aggregated stats.
147 AggregatedStats stats = counter.GetStats();
148 EXPECT_EQ(1, stats.num_samples);
149 EXPECT_EQ(50, stats.min);
150 EXPECT_EQ(50, stats.max);
151 }
152
153 TEST_F(StatsCounterTest, TestMetric_PermilleCounter) {
154 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
155 PermilleCounter counter(&clock_, observer);
156 counter.Add(true);
157 counter.Add(false);
158 clock_.AdvanceTimeMilliseconds(kProcessIntervalMs);
159 // Trigger process (sample included in next interval).
160 counter.Add(false);
161 // Permille per interval.
162 EXPECT_EQ(1, observer->num_calls_);
163 EXPECT_EQ(500, observer->last_sample_);
164 // Aggregated stats.
165 AggregatedStats stats = counter.GetStats();
166 EXPECT_EQ(1, stats.num_samples);
167 EXPECT_EQ(500, stats.min);
168 EXPECT_EQ(500, stats.max);
169 }
170
171 TEST_F(StatsCounterTest, TestMetric_RateCounter) {
172 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
173 RateCounter counter(&clock_, observer);
174 counter.Add(186);
175 counter.Add(350);
176 counter.Add(22);
177 clock_.AdvanceTimeMilliseconds(kProcessIntervalMs);
178 // Trigger process (sample included in next interval).
179 counter.Add(111);
180 // Rate per interval, (186 + 350 + 22) / 2 sec = 279 samples/sec
181 EXPECT_EQ(1, observer->num_calls_);
182 EXPECT_EQ(279, observer->last_sample_);
183 // Aggregated stats.
184 AggregatedStats stats = counter.GetStats();
185 EXPECT_EQ(1, stats.num_samples);
186 EXPECT_EQ(279, stats.min);
187 EXPECT_EQ(279, stats.max);
188 }
189
190 TEST_F(StatsCounterTest, TestMetric_RateAccCounter) {
191 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
192 RateAccCounter counter(&clock_, observer);
193 counter.Set(175);
194 counter.Set(188);
195 clock_.AdvanceTimeMilliseconds(kProcessIntervalMs);
196 // Trigger process (sample included in next interval).
197 counter.Set(192);
198 // Rate per interval: (188 - 0) / 2 sec = 94 samples/sec
199 EXPECT_EQ(1, observer->num_calls_);
200 EXPECT_EQ(94, observer->last_sample_);
201 // Aggregated stats.
202 AggregatedStats stats = counter.GetStats();
203 EXPECT_EQ(1, stats.num_samples);
204 EXPECT_EQ(94, stats.min);
205 EXPECT_EQ(94, stats.max);
206 }
207
208 TEST_F(StatsCounterTest, TestGetStats_MultipleIntervals) {
209 AvgCounter counter(&clock_, nullptr);
210 const int kSample1 = 1;
211 const int kSample2 = 5;
212 const int kSample3 = 8;
213 const int kSample4 = 11;
214 const int kSample5 = 50;
215 AddSampleAndAdvance(kSample1, kProcessIntervalMs, &counter);
216 AddSampleAndAdvance(kSample2, kProcessIntervalMs, &counter);
217 AddSampleAndAdvance(kSample3, kProcessIntervalMs, &counter);
218 AddSampleAndAdvance(kSample4, kProcessIntervalMs, &counter);
219 AddSampleAndAdvance(kSample5, kProcessIntervalMs, &counter);
220 // Trigger process (sample included in next interval).
221 counter.Add(111);
222 AggregatedStats stats = counter.GetStats();
223 EXPECT_EQ(5, stats.num_samples);
224 EXPECT_EQ(kSample1, stats.min);
225 EXPECT_EQ(kSample5, stats.max);
226 EXPECT_EQ(15, stats.average);
227 }
228
229 TEST_F(StatsCounterTest, TestGetStatsTwice) {
230 const int kSample1 = 4;
231 const int kSample2 = 7;
232 AvgCounter counter(&clock_, nullptr);
233 AddSampleAndAdvance(kSample1, kProcessIntervalMs, &counter);
234 // Trigger process (sample included in next interval).
235 counter.Add(kSample2);
236 AggregatedStats stats = counter.GetStats();
237 EXPECT_EQ(1, stats.num_samples);
238 EXPECT_EQ(kSample1, stats.min);
239 EXPECT_EQ(kSample1, stats.max);
240 // Trigger process (sample included in next interval).
241 clock_.AdvanceTimeMilliseconds(kProcessIntervalMs);
242 counter.Add(111);
243 stats = counter.GetStats();
244 EXPECT_EQ(2, stats.num_samples);
245 EXPECT_EQ(kSample1, stats.min);
246 EXPECT_EQ(kSample2, stats.max);
247 EXPECT_EQ(6, stats.average);
248 }
249
250 TEST_F(StatsCounterTest, TestRateAccCounter_NegativeRateIgnored) {
251 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
252 const int kSample1 = 200; // 200 / 2 sec
253 const int kSample2 = 100; // -100 / 2 sec - negative ignored
254 const int kSample3 = 700; // 600 / 2 sec
255 RateAccCounter counter(&clock_, observer);
256 SetSampleAndAdvance(kSample1, kProcessIntervalMs, &counter);
257 SetSampleAndAdvance(kSample2, kProcessIntervalMs, &counter);
258 SetSampleAndAdvance(kSample3, kProcessIntervalMs, &counter);
259 EXPECT_EQ(1, observer->num_calls_);
260 EXPECT_EQ(100, observer->last_sample_);
261 // Trigger process (sample included in next interval).
262 counter.Set(2000);
263 EXPECT_EQ(2, observer->num_calls_);
264 EXPECT_EQ(300, observer->last_sample_);
265 // Aggregated stats.
266 AggregatedStats stats = counter.GetStats();
267 EXPECT_EQ(2, stats.num_samples);
268 EXPECT_EQ(100, stats.min);
269 EXPECT_EQ(300, stats.max);
270 EXPECT_EQ(200, stats.average);
271 }
272
273 TEST_F(StatsCounterTest, TestAvgCounter_IntervalsWithoutSamplesIgnored) {
274 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
275 AvgCounter counter(&clock_, observer);
276 AddSampleAndAdvance(6, kProcessIntervalMs * 4 - 1, &counter);
277 // Trigger process (sample included in next interval).
278 counter.Add(8);
279 // [6:1], two intervals without samples passed.
280 EXPECT_EQ(1, observer->num_calls_);
281 EXPECT_EQ(6, observer->last_sample_);
282 // Make last interval pass.
283 clock_.AdvanceTimeMilliseconds(1);
284 counter.Add(111); // Trigger process (sample included in next interval).
285 // [6:1],[8:1]
286 EXPECT_EQ(2, observer->num_calls_);
287 EXPECT_EQ(8, observer->last_sample_);
288 // Aggregated stats.
289 AggregatedStats stats = counter.GetStats();
290 EXPECT_EQ(2, stats.num_samples);
291 EXPECT_EQ(6, stats.min);
292 EXPECT_EQ(8, stats.max);
293 }
294
295 TEST_F(StatsCounterTest, TestRateCounter_IntervalsWithoutSamplesIncluded) {
296 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
297 const int kSample1 = 50; // 50 / 2 sec
298 const int kSample2 = 20; // 20 / 2 sec
299 RateCounter counter(&clock_, observer);
300 counter.Add(kSample1);
301 clock_.AdvanceTimeMilliseconds(kProcessIntervalMs * 3 - 1);
302 // Trigger process (sample included in next interval).
303 counter.Add(kSample2);
304 // [0:1],[25:1], one interval without samples passed.
305 EXPECT_EQ(2, observer->num_calls_);
306 EXPECT_EQ(25, observer->last_sample_);
307 // Make last interval pass.
308 clock_.AdvanceTimeMilliseconds(1);
309 counter.Add(111); // Trigger process (sample included in next interval).
310 // [0:1],[10:1],[25:1]
311 EXPECT_EQ(3, observer->num_calls_);
312 EXPECT_EQ(10, observer->last_sample_);
313 // Aggregated stats.
314 AggregatedStats stats = counter.GetStats();
315 EXPECT_EQ(3, stats.num_samples);
316 EXPECT_EQ(0, stats.min);
317 EXPECT_EQ(25, stats.max);
318 }
319
320 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/video/stats_counter.cc ('k') | webrtc/video/webrtc_video.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698