1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
|
/*
* Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "video/stats_counter.h"
#include "system_wrappers/include/clock.h"
#include "test/gtest.h"
namespace webrtc {
namespace {
const int kDefaultProcessIntervalMs = 2000;
const uint32_t kStreamId = 123456;
class StatsCounterObserverImpl : public StatsCounterObserver {
public:
StatsCounterObserverImpl() : num_calls_(0), last_sample_(-1) {}
void OnMetricUpdated(int sample) override {
++num_calls_;
last_sample_ = sample;
}
int num_calls_;
int last_sample_;
};
} // namespace
class StatsCounterTest : public ::testing::Test {
protected:
StatsCounterTest() : clock_(1234) {}
void AddSampleAndAdvance(int sample, int interval_ms, AvgCounter* counter) {
counter->Add(sample);
clock_.AdvanceTimeMilliseconds(interval_ms);
}
void SetSampleAndAdvance(int sample,
int interval_ms,
RateAccCounter* counter) {
counter->Set(sample, kStreamId);
clock_.AdvanceTimeMilliseconds(interval_ms);
}
void VerifyStatsIsNotSet(const AggregatedStats& stats) {
EXPECT_EQ(0, stats.num_samples);
EXPECT_EQ(-1, stats.min);
EXPECT_EQ(-1, stats.max);
EXPECT_EQ(-1, stats.average);
}
SimulatedClock clock_;
};
TEST_F(StatsCounterTest, NoSamples) {
AvgCounter counter(&clock_, nullptr, false);
VerifyStatsIsNotSet(counter.GetStats());
}
TEST_F(StatsCounterTest, TestRegisterObserver) {
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
const int kSample = 22;
AvgCounter counter(&clock_, observer, false);
AddSampleAndAdvance(kSample, kDefaultProcessIntervalMs, &counter);
// Trigger process (sample included in next interval).
counter.Add(111);
EXPECT_EQ(1, observer->num_calls_);
}
TEST_F(StatsCounterTest, HasSample) {
AvgCounter counter(&clock_, nullptr, false);
EXPECT_FALSE(counter.HasSample());
counter.Add(1);
EXPECT_TRUE(counter.HasSample());
}
TEST_F(StatsCounterTest, VerifyProcessInterval) {
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
AvgCounter counter(&clock_, observer, false);
counter.Add(4);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs - 1);
// Try trigger process (interval has not passed).
counter.Add(8);
EXPECT_EQ(0, observer->num_calls_);
VerifyStatsIsNotSet(counter.GetStats());
// Make process interval pass.
clock_.AdvanceTimeMilliseconds(1);
// Trigger process (sample included in next interval).
counter.Add(111);
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(6, observer->last_sample_);
// Aggregated stats.
AggregatedStats stats = counter.GetStats();
EXPECT_EQ(1, stats.num_samples);
}
TEST_F(StatsCounterTest, TestMetric_AvgCounter) {
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
AvgCounter counter(&clock_, observer, false);
counter.Add(4);
counter.Add(8);
counter.Add(9);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
// Trigger process (sample included in next interval).
counter.Add(111);
// Average per interval.
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(7, observer->last_sample_);
// Aggregated stats.
AggregatedStats stats = counter.GetStats();
EXPECT_EQ(1, stats.num_samples);
EXPECT_EQ(7, stats.min);
EXPECT_EQ(7, stats.max);
EXPECT_EQ(7, stats.average);
}
TEST_F(StatsCounterTest, TestMetric_MaxCounter) {
const int64_t kProcessIntervalMs = 1000;
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
MaxCounter counter(&clock_, observer, kProcessIntervalMs);
counter.Add(4);
counter.Add(9);
counter.Add(8);
clock_.AdvanceTimeMilliseconds(kProcessIntervalMs);
// Trigger process (sample included in next interval).
counter.Add(111);
// Average per interval.
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(9, observer->last_sample_);
// Aggregated stats.
AggregatedStats stats = counter.GetStats();
EXPECT_EQ(1, stats.num_samples);
EXPECT_EQ(9, stats.min);
EXPECT_EQ(9, stats.max);
EXPECT_EQ(9, stats.average);
}
TEST_F(StatsCounterTest, TestMetric_PercentCounter) {
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
PercentCounter counter(&clock_, observer);
counter.Add(true);
counter.Add(false);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
// Trigger process (sample included in next interval).
counter.Add(false);
// Percentage per interval.
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(50, observer->last_sample_);
// Aggregated stats.
AggregatedStats stats = counter.GetStats();
EXPECT_EQ(1, stats.num_samples);
EXPECT_EQ(50, stats.min);
EXPECT_EQ(50, stats.max);
}
TEST_F(StatsCounterTest, TestMetric_PermilleCounter) {
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
PermilleCounter counter(&clock_, observer);
counter.Add(true);
counter.Add(false);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
// Trigger process (sample included in next interval).
counter.Add(false);
// Permille per interval.
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(500, observer->last_sample_);
// Aggregated stats.
AggregatedStats stats = counter.GetStats();
EXPECT_EQ(1, stats.num_samples);
EXPECT_EQ(500, stats.min);
EXPECT_EQ(500, stats.max);
}
TEST_F(StatsCounterTest, TestMetric_RateCounter) {
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
RateCounter counter(&clock_, observer, true);
counter.Add(186);
counter.Add(350);
counter.Add(22);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
// Trigger process (sample included in next interval).
counter.Add(111);
// Rate per interval, (186 + 350 + 22) / 2 sec = 279 samples/sec
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(279, observer->last_sample_);
// Aggregated stats.
AggregatedStats stats = counter.GetStats();
EXPECT_EQ(1, stats.num_samples);
EXPECT_EQ(279, stats.min);
EXPECT_EQ(279, stats.max);
}
TEST_F(StatsCounterTest, TestMetric_RateAccCounter) {
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
RateAccCounter counter(&clock_, observer, true);
counter.Set(175, kStreamId);
counter.Set(188, kStreamId);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
// Trigger process (sample included in next interval).
counter.Set(192, kStreamId);
// Rate per interval: (188 - 0) / 2 sec = 94 samples/sec
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(94, observer->last_sample_);
// Aggregated stats.
AggregatedStats stats = counter.GetStats();
EXPECT_EQ(1, stats.num_samples);
EXPECT_EQ(94, stats.min);
EXPECT_EQ(94, stats.max);
}
TEST_F(StatsCounterTest, TestMetric_RateAccCounterWithSetLast) {
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
RateAccCounter counter(&clock_, observer, true);
counter.SetLast(98, kStreamId);
counter.Set(175, kStreamId);
counter.Set(188, kStreamId);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
// Trigger process (sample included in next interval).
counter.Set(192, kStreamId);
// Rate per interval: (188 - 98) / 2 sec = 45 samples/sec
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(45, observer->last_sample_);
}
TEST_F(StatsCounterTest, TestMetric_RateAccCounterWithMultipleStreamIds) {
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
RateAccCounter counter(&clock_, observer, true);
counter.Set(175, kStreamId);
counter.Set(188, kStreamId);
counter.Set(100, kStreamId + 1);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
// Trigger process (sample included in next interval).
counter.Set(150, kStreamId + 1);
// Rate per interval: ((188 - 0) + (100 - 0)) / 2 sec = 144 samples/sec
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(144, observer->last_sample_);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
// Trigger process (sample included in next interval).
counter.Set(198, kStreamId);
// Rate per interval: (0 + (150 - 100)) / 2 sec = 25 samples/sec
EXPECT_EQ(2, observer->num_calls_);
EXPECT_EQ(25, observer->last_sample_);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
// Trigger process (sample included in next interval).
counter.Set(200, kStreamId);
// Rate per interval: ((198 - 188) + (0)) / 2 sec = 5 samples/sec
EXPECT_EQ(3, observer->num_calls_);
EXPECT_EQ(5, observer->last_sample_);
// Aggregated stats.
AggregatedStats stats = counter.GetStats();
EXPECT_EQ(3, stats.num_samples);
EXPECT_EQ(5, stats.min);
EXPECT_EQ(144, stats.max);
}
TEST_F(StatsCounterTest, TestGetStats_MultipleIntervals) {
AvgCounter counter(&clock_, nullptr, false);
const int kSample1 = 1;
const int kSample2 = 5;
const int kSample3 = 8;
const int kSample4 = 11;
const int kSample5 = 50;
AddSampleAndAdvance(kSample1, kDefaultProcessIntervalMs, &counter);
AddSampleAndAdvance(kSample2, kDefaultProcessIntervalMs, &counter);
AddSampleAndAdvance(kSample3, kDefaultProcessIntervalMs, &counter);
AddSampleAndAdvance(kSample4, kDefaultProcessIntervalMs, &counter);
AddSampleAndAdvance(kSample5, kDefaultProcessIntervalMs, &counter);
// Trigger process (sample included in next interval).
counter.Add(111);
AggregatedStats stats = counter.GetStats();
EXPECT_EQ(5, stats.num_samples);
EXPECT_EQ(kSample1, stats.min);
EXPECT_EQ(kSample5, stats.max);
EXPECT_EQ(15, stats.average);
}
TEST_F(StatsCounterTest, TestGetStatsTwice) {
const int kSample1 = 4;
const int kSample2 = 7;
AvgCounter counter(&clock_, nullptr, false);
AddSampleAndAdvance(kSample1, kDefaultProcessIntervalMs, &counter);
// Trigger process (sample included in next interval).
counter.Add(kSample2);
AggregatedStats stats = counter.GetStats();
EXPECT_EQ(1, stats.num_samples);
EXPECT_EQ(kSample1, stats.min);
EXPECT_EQ(kSample1, stats.max);
// Trigger process (sample included in next interval).
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
counter.Add(111);
stats = counter.GetStats();
EXPECT_EQ(2, stats.num_samples);
EXPECT_EQ(kSample1, stats.min);
EXPECT_EQ(kSample2, stats.max);
EXPECT_EQ(6, stats.average);
}
TEST_F(StatsCounterTest, TestRateAccCounter_NegativeRateIgnored) {
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
const int kSample1 = 200; // 200 / 2 sec
const int kSample2 = 100; // -100 / 2 sec - negative ignored
const int kSample3 = 700; // 600 / 2 sec
RateAccCounter counter(&clock_, observer, true);
SetSampleAndAdvance(kSample1, kDefaultProcessIntervalMs, &counter);
SetSampleAndAdvance(kSample2, kDefaultProcessIntervalMs, &counter);
SetSampleAndAdvance(kSample3, kDefaultProcessIntervalMs, &counter);
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(100, observer->last_sample_);
// Trigger process (sample included in next interval).
counter.Set(2000, kStreamId);
EXPECT_EQ(2, observer->num_calls_);
EXPECT_EQ(300, observer->last_sample_);
// Aggregated stats.
AggregatedStats stats = counter.GetStats();
EXPECT_EQ(2, stats.num_samples);
EXPECT_EQ(100, stats.min);
EXPECT_EQ(300, stats.max);
EXPECT_EQ(200, stats.average);
}
TEST_F(StatsCounterTest, TestAvgCounter_IntervalsWithoutSamplesIncluded) {
// Samples: | 6 | x | x | 8 | // x: empty interval
// Stats: | 6 | 6 | 6 | 8 | // x -> last value reported
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
AvgCounter counter(&clock_, observer, true);
AddSampleAndAdvance(6, kDefaultProcessIntervalMs * 4 - 1, &counter);
// Trigger process (sample included in next interval).
counter.Add(8);
// [6:3], 3 intervals passed (2 without samples -> last value reported).
AggregatedStats stats = counter.ProcessAndGetStats();
EXPECT_EQ(3, stats.num_samples);
EXPECT_EQ(6, stats.min);
EXPECT_EQ(6, stats.max);
// Make next interval pass and verify stats: [6:3],[8:1]
clock_.AdvanceTimeMilliseconds(1);
counter.ProcessAndGetStats();
EXPECT_EQ(4, observer->num_calls_);
EXPECT_EQ(8, observer->last_sample_);
}
TEST_F(StatsCounterTest, TestAvgCounter_WithPause) {
// Samples: | 6 | x | x | x | - | 22 | x | // x: empty interval, -: paused
// Stats: | 6 | 6 | 6 | 6 | - | 22 | 22 | // x -> last value reported
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
AvgCounter counter(&clock_, observer, true);
// Add sample and advance 3 intervals (2 w/o samples -> last value reported).
AddSampleAndAdvance(6, kDefaultProcessIntervalMs * 4 - 1, &counter);
// Trigger process and verify stats: [6:3]
counter.ProcessAndGetStats();
EXPECT_EQ(3, observer->num_calls_);
EXPECT_EQ(6, observer->last_sample_);
// Make next interval pass (1 without samples).
// Process and pause. Verify stats: [6:4].
clock_.AdvanceTimeMilliseconds(1);
counter.ProcessAndPause();
EXPECT_EQ(4, observer->num_calls_); // Last value reported.
EXPECT_EQ(6, observer->last_sample_);
// Make next interval pass (1 without samples -> ignored while paused).
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs * 2 - 1);
counter.Add(22); // Stops pause.
EXPECT_EQ(4, observer->num_calls_);
EXPECT_EQ(6, observer->last_sample_);
// Make next interval pass, [6:4][22:1]
clock_.AdvanceTimeMilliseconds(1);
counter.ProcessAndGetStats();
EXPECT_EQ(5, observer->num_calls_);
EXPECT_EQ(22, observer->last_sample_);
// Make 1 interval pass (1 w/o samples -> pause stopped, last value reported).
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
counter.ProcessAndGetStats();
EXPECT_EQ(6, observer->num_calls_);
EXPECT_EQ(22, observer->last_sample_);
}
TEST_F(StatsCounterTest, TestRateAccCounter_AddSampleStopsPause) {
// Samples: | 12 | 24 | // -: paused
// Stats: | 6 | 6 |
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
RateAccCounter counter(&clock_, observer, true);
// Add sample and advance 1 intervals.
counter.Set(12, kStreamId);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
// Trigger process and verify stats: [6:1]
counter.ProcessAndPause();
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(6, observer->last_sample_);
// Add sample and advance 1 intervals.
counter.Set(24, kStreamId); // Pause stopped.
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
counter.ProcessAndGetStats();
EXPECT_EQ(2, observer->num_calls_);
EXPECT_EQ(6, observer->last_sample_);
}
TEST_F(StatsCounterTest, TestRateAccCounter_AddSameSampleDoesNotStopPause) {
// Samples: | 12 | 12 | 24 | // -: paused
// Stats: | 6 | - | 6 |
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
RateAccCounter counter(&clock_, observer, true);
// Add sample and advance 1 intervals.
counter.Set(12, kStreamId);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
// Trigger process and verify stats: [6:1]
counter.ProcessAndPause();
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(6, observer->last_sample_);
// Add same sample and advance 1 intervals.
counter.Set(12, kStreamId); // Pause not stopped.
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
counter.ProcessAndGetStats();
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(6, observer->last_sample_);
// Add new sample and advance 1 intervals.
counter.Set(24, kStreamId); // Pause stopped.
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
counter.ProcessAndGetStats();
EXPECT_EQ(2, observer->num_calls_);
EXPECT_EQ(6, observer->last_sample_);
}
TEST_F(StatsCounterTest, TestRateAccCounter_PauseAndStopPause) {
// Samples: | 12 | 12 | 12 | // -: paused
// Stats: | 6 | - | 0 |
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
RateAccCounter counter(&clock_, observer, true);
// Add sample and advance 1 intervals.
counter.Set(12, kStreamId);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
// Trigger process and verify stats: [6:1]
counter.ProcessAndPause();
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(6, observer->last_sample_);
// Add same sample and advance 1 intervals.
counter.Set(12, kStreamId); // Pause not stopped.
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
counter.ProcessAndGetStats();
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(6, observer->last_sample_);
// Stop pause, add sample and advance 1 intervals.
counter.ProcessAndStopPause();
counter.Set(12, kStreamId);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
counter.ProcessAndGetStats();
EXPECT_EQ(2, observer->num_calls_);
EXPECT_EQ(0, observer->last_sample_);
}
TEST_F(StatsCounterTest, TestAvgCounter_WithoutMinPauseTimePassed) {
// Samples: | 6 | 2 | - | // x: empty interval, -: paused
// Stats: | 6 | 2 | - | // x -> last value reported
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
AvgCounter counter(&clock_, observer, true);
// Add sample and advance 1 intervals.
AddSampleAndAdvance(6, kDefaultProcessIntervalMs, &counter);
// Process and pause. Verify stats: [6:1].
const int64_t kMinMs = 500;
counter.ProcessAndPauseForDuration(kMinMs);
EXPECT_EQ(1, observer->num_calls_); // Last value reported.
EXPECT_EQ(6, observer->last_sample_);
// Min pause time has not pass.
clock_.AdvanceTimeMilliseconds(kMinMs - 1);
counter.Add(2); // Pause not stopped.
// Make two intervals pass (1 without samples -> ignored while paused).
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs * 2 - (kMinMs - 1));
counter.ProcessAndGetStats();
EXPECT_EQ(2, observer->num_calls_);
EXPECT_EQ(2, observer->last_sample_);
}
TEST_F(StatsCounterTest, TestAvgCounter_WithMinPauseTimePassed) {
// Samples: | 6 | 2 | x | // x: empty interval, -: paused
// Stats: | 6 | 2 | 2 | // x -> last value reported
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
AvgCounter counter(&clock_, observer, true);
// Add sample and advance 1 intervals.
AddSampleAndAdvance(6, kDefaultProcessIntervalMs, &counter);
// Process and pause. Verify stats: [6:1].
const int64_t kMinMs = 500;
counter.ProcessAndPauseForDuration(kMinMs);
EXPECT_EQ(1, observer->num_calls_); // Last value reported.
EXPECT_EQ(6, observer->last_sample_);
// Make min pause time pass.
clock_.AdvanceTimeMilliseconds(kMinMs);
counter.Add(2); // Stop pause.
// Make two intervals pass (1 without samples -> last value reported).
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs * 2 - kMinMs);
counter.ProcessAndGetStats();
EXPECT_EQ(3, observer->num_calls_);
EXPECT_EQ(2, observer->last_sample_);
}
TEST_F(StatsCounterTest, TestRateCounter_IntervalsWithoutSamplesIgnored) {
// Samples: | 50 | x | 20 | // x: empty interval
// Stats: | 25 | x | 10 | // x -> ignored
const bool kIncludeEmptyIntervals = false;
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
const int kSample1 = 50; // 50 / 2 sec
const int kSample2 = 20; // 20 / 2 sec
RateCounter counter(&clock_, observer, kIncludeEmptyIntervals);
counter.Add(kSample1);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs * 3 - 1);
// Trigger process (sample included in next interval).
counter.Add(kSample2);
// [25:1], 2 intervals passed (1 without samples -> ignored).
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(25, observer->last_sample_);
// Make next interval pass and verify stats: [10:1],[25:1]
clock_.AdvanceTimeMilliseconds(1);
counter.ProcessAndGetStats();
EXPECT_EQ(2, observer->num_calls_);
EXPECT_EQ(10, observer->last_sample_);
}
TEST_F(StatsCounterTest, TestRateCounter_IntervalsWithoutSamplesIncluded) {
// Samples: | 50 | x | 20 | // x: empty interval
// Stats: | 25 | 0 | 10 | // x -> zero reported
const bool kIncludeEmptyIntervals = true;
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
const int kSample1 = 50; // 50 / 2 sec
const int kSample2 = 20; // 20 / 2 sec
RateCounter counter(&clock_, observer, kIncludeEmptyIntervals);
counter.Add(kSample1);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs * 3 - 1);
// Trigger process (sample included in next interval).
counter.Add(kSample2);
// [0:1],[25:1], 2 intervals passed (1 without samples -> zero reported).
EXPECT_EQ(2, observer->num_calls_);
EXPECT_EQ(0, observer->last_sample_);
// Make last interval pass and verify stats: [0:1],[10:1],[25:1]
clock_.AdvanceTimeMilliseconds(1);
AggregatedStats stats = counter.ProcessAndGetStats();
EXPECT_EQ(25, stats.max);
EXPECT_EQ(3, observer->num_calls_);
EXPECT_EQ(10, observer->last_sample_);
}
TEST_F(StatsCounterTest, TestRateAccCounter_IntervalsWithoutSamplesIncluded) {
// Samples: | 12 | x | x | x | 60 | // x: empty interval
// Stats: | 6 | 0 | 0 | 0 | 24 | // x -> zero reported
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
RateAccCounter counter(&clock_, observer, true);
VerifyStatsIsNotSet(counter.ProcessAndGetStats());
// Advance one interval and verify stats.
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs);
VerifyStatsIsNotSet(counter.ProcessAndGetStats());
// Add sample and advance 3 intervals (2 w/o samples -> zero reported).
counter.Set(12, kStreamId);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs * 4 - 1);
// Trigger process and verify stats: [0:2][6:1]
counter.ProcessAndGetStats();
EXPECT_EQ(3, observer->num_calls_);
EXPECT_EQ(0, observer->last_sample_);
// Make next interval pass (1 w/o samples -> zero reported), [0:3][6:1]
clock_.AdvanceTimeMilliseconds(1);
counter.ProcessAndGetStats();
EXPECT_EQ(4, observer->num_calls_);
EXPECT_EQ(0, observer->last_sample_);
// Insert sample and advance non-complete interval, no change, [0:3][6:1]
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs - 1);
counter.Set(60, kStreamId);
EXPECT_EQ(4, observer->num_calls_);
// Make next interval pass, [0:3][6:1][24:1]
clock_.AdvanceTimeMilliseconds(1);
AggregatedStats stats = counter.ProcessAndGetStats();
EXPECT_EQ(5, observer->num_calls_);
EXPECT_EQ(24, observer->last_sample_);
EXPECT_EQ(6, stats.average);
}
TEST_F(StatsCounterTest, TestRateAccCounter_IntervalsWithoutSamplesIgnored) {
// Samples: | 12 | x | x | x | 60 | // x: empty interval
// Stats: | 6 | x | x | x | 24 | // x -> ignored
StatsCounterObserverImpl* observer = new StatsCounterObserverImpl();
RateAccCounter counter(&clock_, observer, false);
// Add sample and advance 3 intervals (2 w/o samples -> ignored).
counter.Set(12, kStreamId);
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs * 4 - 1);
// Trigger process and verify stats: [6:1]
counter.ProcessAndGetStats();
EXPECT_EQ(1, observer->num_calls_);
EXPECT_EQ(6, observer->last_sample_);
// Make next interval pass (1 w/o samples -> ignored), [6:1]
clock_.AdvanceTimeMilliseconds(1);
counter.ProcessAndGetStats();
EXPECT_EQ(1, observer->num_calls_);
// Insert sample and advance non-complete interval, no change, [6:1]
clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs - 1);
counter.Set(60, kStreamId);
counter.ProcessAndGetStats();
EXPECT_EQ(1, observer->num_calls_);
// Make next interval pass, [6:1][24:1]
clock_.AdvanceTimeMilliseconds(1);
counter.ProcessAndGetStats();
EXPECT_EQ(2, observer->num_calls_);
EXPECT_EQ(24, observer->last_sample_);
}
} // namespace webrtc
|