OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 // Rate per interval: (188 - 0) / 2 sec = 94 samples/sec | 206 // Rate per interval: (188 - 0) / 2 sec = 94 samples/sec |
207 EXPECT_EQ(1, observer->num_calls_); | 207 EXPECT_EQ(1, observer->num_calls_); |
208 EXPECT_EQ(94, observer->last_sample_); | 208 EXPECT_EQ(94, observer->last_sample_); |
209 // Aggregated stats. | 209 // Aggregated stats. |
210 AggregatedStats stats = counter.GetStats(); | 210 AggregatedStats stats = counter.GetStats(); |
211 EXPECT_EQ(1, stats.num_samples); | 211 EXPECT_EQ(1, stats.num_samples); |
212 EXPECT_EQ(94, stats.min); | 212 EXPECT_EQ(94, stats.min); |
213 EXPECT_EQ(94, stats.max); | 213 EXPECT_EQ(94, stats.max); |
214 } | 214 } |
215 | 215 |
| 216 TEST_F(StatsCounterTest, TestMetric_RateAccCounterWithSetLast) { |
| 217 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); |
| 218 RateAccCounter counter(&clock_, observer, true); |
| 219 counter.SetLast(98, kStreamId); |
| 220 counter.Set(175, kStreamId); |
| 221 counter.Set(188, kStreamId); |
| 222 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); |
| 223 // Trigger process (sample included in next interval). |
| 224 counter.Set(192, kStreamId); |
| 225 // Rate per interval: (188 - 98) / 2 sec = 45 samples/sec |
| 226 EXPECT_EQ(1, observer->num_calls_); |
| 227 EXPECT_EQ(45, observer->last_sample_); |
| 228 } |
| 229 |
216 TEST_F(StatsCounterTest, TestMetric_RateAccCounterWithMultipleStreamIds) { | 230 TEST_F(StatsCounterTest, TestMetric_RateAccCounterWithMultipleStreamIds) { |
217 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); | 231 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); |
218 RateAccCounter counter(&clock_, observer, true); | 232 RateAccCounter counter(&clock_, observer, true); |
219 counter.Set(175, kStreamId); | 233 counter.Set(175, kStreamId); |
220 counter.Set(188, kStreamId); | 234 counter.Set(188, kStreamId); |
221 counter.Set(100, kStreamId + 1); | 235 counter.Set(100, kStreamId + 1); |
222 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); | 236 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); |
223 // Trigger process (sample included in next interval). | 237 // Trigger process (sample included in next interval). |
224 counter.Set(150, kStreamId + 1); | 238 counter.Set(150, kStreamId + 1); |
225 // Rate per interval: ((188 - 0) + (100 - 0)) / 2 sec = 144 samples/sec | 239 // Rate per interval: ((188 - 0) + (100 - 0)) / 2 sec = 144 samples/sec |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 counter.ProcessAndGetStats(); | 370 counter.ProcessAndGetStats(); |
357 EXPECT_EQ(5, observer->num_calls_); | 371 EXPECT_EQ(5, observer->num_calls_); |
358 EXPECT_EQ(22, observer->last_sample_); | 372 EXPECT_EQ(22, observer->last_sample_); |
359 // Make 1 interval pass (1 w/o samples -> pause stopped, last value reported). | 373 // Make 1 interval pass (1 w/o samples -> pause stopped, last value reported). |
360 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); | 374 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); |
361 counter.ProcessAndGetStats(); | 375 counter.ProcessAndGetStats(); |
362 EXPECT_EQ(6, observer->num_calls_); | 376 EXPECT_EQ(6, observer->num_calls_); |
363 EXPECT_EQ(22, observer->last_sample_); | 377 EXPECT_EQ(22, observer->last_sample_); |
364 } | 378 } |
365 | 379 |
| 380 TEST_F(StatsCounterTest, TestRateAccCounter_AddSampleStopsPause) { |
| 381 // Samples: | 12 | 24 | // -: paused |
| 382 // Stats: | 6 | 6 | |
| 383 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); |
| 384 RateAccCounter counter(&clock_, observer, true); |
| 385 // Add sample and advance 1 intervals. |
| 386 counter.Set(12, kStreamId); |
| 387 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); |
| 388 // Trigger process and verify stats: [6:1] |
| 389 counter.ProcessAndPause(); |
| 390 EXPECT_EQ(1, observer->num_calls_); |
| 391 EXPECT_EQ(6, observer->last_sample_); |
| 392 // Add sample and advance 1 intervals. |
| 393 counter.Set(24, kStreamId); // Pause stopped. |
| 394 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); |
| 395 counter.ProcessAndGetStats(); |
| 396 EXPECT_EQ(2, observer->num_calls_); |
| 397 EXPECT_EQ(6, observer->last_sample_); |
| 398 } |
| 399 |
| 400 TEST_F(StatsCounterTest, TestRateAccCounter_AddSameSampleDoesNotStopPause) { |
| 401 // Samples: | 12 | 12 | 24 | // -: paused |
| 402 // Stats: | 6 | - | 6 | |
| 403 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); |
| 404 RateAccCounter counter(&clock_, observer, true); |
| 405 // Add sample and advance 1 intervals. |
| 406 counter.Set(12, kStreamId); |
| 407 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); |
| 408 // Trigger process and verify stats: [6:1] |
| 409 counter.ProcessAndPause(); |
| 410 EXPECT_EQ(1, observer->num_calls_); |
| 411 EXPECT_EQ(6, observer->last_sample_); |
| 412 // Add same sample and advance 1 intervals. |
| 413 counter.Set(12, kStreamId); // Pause not stopped. |
| 414 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); |
| 415 counter.ProcessAndGetStats(); |
| 416 EXPECT_EQ(1, observer->num_calls_); |
| 417 EXPECT_EQ(6, observer->last_sample_); |
| 418 // Add new sample and advance 1 intervals. |
| 419 counter.Set(24, kStreamId); // Pause stopped. |
| 420 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); |
| 421 counter.ProcessAndGetStats(); |
| 422 EXPECT_EQ(2, observer->num_calls_); |
| 423 EXPECT_EQ(6, observer->last_sample_); |
| 424 } |
| 425 |
| 426 TEST_F(StatsCounterTest, TestRateAccCounter_PauseAndStopPause) { |
| 427 // Samples: | 12 | 12 | 12 | // -: paused |
| 428 // Stats: | 6 | - | 0 | |
| 429 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); |
| 430 RateAccCounter counter(&clock_, observer, true); |
| 431 // Add sample and advance 1 intervals. |
| 432 counter.Set(12, kStreamId); |
| 433 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); |
| 434 // Trigger process and verify stats: [6:1] |
| 435 counter.ProcessAndPause(); |
| 436 EXPECT_EQ(1, observer->num_calls_); |
| 437 EXPECT_EQ(6, observer->last_sample_); |
| 438 // Add same sample and advance 1 intervals. |
| 439 counter.Set(12, kStreamId); // Pause not stopped. |
| 440 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); |
| 441 counter.ProcessAndGetStats(); |
| 442 EXPECT_EQ(1, observer->num_calls_); |
| 443 EXPECT_EQ(6, observer->last_sample_); |
| 444 // Stop pause, add sample and advance 1 intervals. |
| 445 counter.ProcessAndStopPause(); |
| 446 counter.Set(12, kStreamId); |
| 447 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs); |
| 448 counter.ProcessAndGetStats(); |
| 449 EXPECT_EQ(2, observer->num_calls_); |
| 450 EXPECT_EQ(0, observer->last_sample_); |
| 451 } |
| 452 |
| 453 TEST_F(StatsCounterTest, TestAvgCounter_WithoutMinPauseTimePassed) { |
| 454 // Samples: | 6 | 2 | - | // x: empty interval, -: paused |
| 455 // Stats: | 6 | 2 | - | // x -> last value reported |
| 456 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); |
| 457 AvgCounter counter(&clock_, observer, true); |
| 458 // Add sample and advance 1 intervals. |
| 459 AddSampleAndAdvance(6, kDefaultProcessIntervalMs, &counter); |
| 460 // Process and pause. Verify stats: [6:1]. |
| 461 const int64_t kMinMs = 500; |
| 462 counter.ProcessAndPauseForDuration(kMinMs); |
| 463 EXPECT_EQ(1, observer->num_calls_); // Last value reported. |
| 464 EXPECT_EQ(6, observer->last_sample_); |
| 465 // Min pause time has not pass. |
| 466 clock_.AdvanceTimeMilliseconds(kMinMs - 1); |
| 467 counter.Add(2); // Pause not stopped. |
| 468 // Make two intervals pass (1 without samples -> ignored while paused). |
| 469 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs * 2 - (kMinMs - 1)); |
| 470 counter.ProcessAndGetStats(); |
| 471 EXPECT_EQ(2, observer->num_calls_); |
| 472 EXPECT_EQ(2, observer->last_sample_); |
| 473 } |
| 474 |
| 475 TEST_F(StatsCounterTest, TestAvgCounter_WithMinPauseTimePassed) { |
| 476 // Samples: | 6 | 2 | x | // x: empty interval, -: paused |
| 477 // Stats: | 6 | 2 | 2 | // x -> last value reported |
| 478 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); |
| 479 AvgCounter counter(&clock_, observer, true); |
| 480 // Add sample and advance 1 intervals. |
| 481 AddSampleAndAdvance(6, kDefaultProcessIntervalMs, &counter); |
| 482 // Process and pause. Verify stats: [6:1]. |
| 483 const int64_t kMinMs = 500; |
| 484 counter.ProcessAndPauseForDuration(kMinMs); |
| 485 EXPECT_EQ(1, observer->num_calls_); // Last value reported. |
| 486 EXPECT_EQ(6, observer->last_sample_); |
| 487 // Make min pause time pass. |
| 488 clock_.AdvanceTimeMilliseconds(kMinMs); |
| 489 counter.Add(2); // Stop pause. |
| 490 // Make two intervals pass (1 without samples -> last value reported). |
| 491 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs * 2 - kMinMs); |
| 492 counter.ProcessAndGetStats(); |
| 493 EXPECT_EQ(3, observer->num_calls_); |
| 494 EXPECT_EQ(2, observer->last_sample_); |
| 495 } |
| 496 |
366 TEST_F(StatsCounterTest, TestRateCounter_IntervalsWithoutSamplesIgnored) { | 497 TEST_F(StatsCounterTest, TestRateCounter_IntervalsWithoutSamplesIgnored) { |
367 // Samples: | 50 | x | 20 | // x: empty interval | 498 // Samples: | 50 | x | 20 | // x: empty interval |
368 // Stats: | 25 | x | 10 | // x -> ignored | 499 // Stats: | 25 | x | 10 | // x -> ignored |
369 const bool kIncludeEmptyIntervals = false; | 500 const bool kIncludeEmptyIntervals = false; |
370 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); | 501 StatsCounterObserverImpl* observer = new StatsCounterObserverImpl(); |
371 const int kSample1 = 50; // 50 / 2 sec | 502 const int kSample1 = 50; // 50 / 2 sec |
372 const int kSample2 = 20; // 20 / 2 sec | 503 const int kSample2 = 20; // 20 / 2 sec |
373 RateCounter counter(&clock_, observer, kIncludeEmptyIntervals); | 504 RateCounter counter(&clock_, observer, kIncludeEmptyIntervals); |
374 counter.Add(kSample1); | 505 counter.Add(kSample1); |
375 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs * 3 - 1); | 506 clock_.AdvanceTimeMilliseconds(kDefaultProcessIntervalMs * 3 - 1); |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 counter.ProcessAndGetStats(); | 594 counter.ProcessAndGetStats(); |
464 EXPECT_EQ(1, observer->num_calls_); | 595 EXPECT_EQ(1, observer->num_calls_); |
465 // Make next interval pass, [6:1][24:1] | 596 // Make next interval pass, [6:1][24:1] |
466 clock_.AdvanceTimeMilliseconds(1); | 597 clock_.AdvanceTimeMilliseconds(1); |
467 counter.ProcessAndGetStats(); | 598 counter.ProcessAndGetStats(); |
468 EXPECT_EQ(2, observer->num_calls_); | 599 EXPECT_EQ(2, observer->num_calls_); |
469 EXPECT_EQ(24, observer->last_sample_); | 600 EXPECT_EQ(24, observer->last_sample_); |
470 } | 601 } |
471 | 602 |
472 } // namespace webrtc | 603 } // namespace webrtc |
OLD | NEW |