isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 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 | |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 11 | #include "webrtc/modules/pacing/alr_detector.h" |
| 12 | |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 13 | #include "webrtc/test/gtest.h" |
| 14 | |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 15 | namespace { |
| 16 | |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 17 | constexpr int kEstimatedBitrateBps = 300000; |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 18 | |
| 19 | } // namespace |
| 20 | |
| 21 | namespace webrtc { |
| 22 | |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 23 | class AlrDetectorTest : public testing::Test { |
| 24 | public: |
| 25 | void SetUp() override { |
| 26 | alr_detector_.SetEstimatedBitrate(kEstimatedBitrateBps); |
| 27 | } |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 28 | |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 29 | void SimulateOutgoingTraffic(int interval_ms, int usage_percentage) { |
| 30 | const int kTimeStepMs = 10; |
| 31 | for (int t = 0; t < interval_ms; t += kTimeStepMs) { |
| 32 | now_ms += kTimeStepMs; |
| 33 | alr_detector_.OnBytesSent(kEstimatedBitrateBps * usage_percentage * |
| 34 | kTimeStepMs / (8 * 100 * 1000), |
| 35 | now_ms); |
| 36 | } |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 37 | |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 38 | int remainder_ms = interval_ms % kTimeStepMs; |
| 39 | now_ms += remainder_ms; |
| 40 | if (remainder_ms > 0) { |
| 41 | alr_detector_.OnBytesSent(kEstimatedBitrateBps * usage_percentage * |
| 42 | remainder_ms / (8 * 100 * 1000), |
| 43 | remainder_ms); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | protected: |
| 48 | AlrDetector alr_detector_; |
| 49 | int64_t now_ms = 1; |
| 50 | }; |
| 51 | |
| 52 | TEST_F(AlrDetectorTest, AlrDetection) { |
| 53 | // Start in non-ALR state. |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 54 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 55 | |
| 56 | // Stay in non-ALR state when usage is close to 100%. |
| 57 | SimulateOutgoingTraffic(500, 90); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 58 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 59 | |
| 60 | // Verify that we ALR starts when bitrate drops below 20%. |
| 61 | SimulateOutgoingTraffic(500, 20); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 62 | EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 63 | |
stefan | f00497c | 2017-01-27 02:27:33 -0800 | [diff] [blame^] | 64 | // Verify that we remain in ALR state while usage is still below 70%. |
| 65 | SimulateOutgoingTraffic(500, 69); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 66 | EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 67 | |
stefan | f00497c | 2017-01-27 02:27:33 -0800 | [diff] [blame^] | 68 | // Verify that ALR ends when usage is above 70%. |
| 69 | SimulateOutgoingTraffic(500, 75); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 70 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 73 | TEST_F(AlrDetectorTest, ShortSpike) { |
| 74 | // Start in non-ALR state. |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 75 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 76 | |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 77 | // Verify that we ALR starts when bitrate drops below 20%. |
| 78 | SimulateOutgoingTraffic(500, 20); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 79 | EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 80 | |
| 81 | // Verify that we stay in ALR region even after a short bitrate spike. |
| 82 | SimulateOutgoingTraffic(100, 150); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 83 | EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 84 | |
| 85 | SimulateOutgoingTraffic(200, 20); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 86 | EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 87 | |
stefan | f00497c | 2017-01-27 02:27:33 -0800 | [diff] [blame^] | 88 | // ALR ends when usage is above 70%. |
| 89 | SimulateOutgoingTraffic(500, 75); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 90 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | TEST_F(AlrDetectorTest, BandwidthEstimateChanges) { |
| 94 | // Start in non-ALR state. |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 95 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 96 | |
| 97 | // ALR starts when bitrate drops below 20%. |
| 98 | SimulateOutgoingTraffic(500, 20); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 99 | EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 100 | |
| 101 | // When bandwidth estimate drops the detector should stay in ALR mode and quit |
| 102 | // it shortly afterwards as the sender continues sending the same amount of |
| 103 | // traffic. This is necessary to ensure that ProbeController can still react |
| 104 | // to the BWE drop by initiating a new probe. |
| 105 | alr_detector_.SetEstimatedBitrate(kEstimatedBitrateBps / 5); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 106 | EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
Sergey Ulanov | 0182f85 | 2016-11-16 15:42:11 -0800 | [diff] [blame] | 107 | SimulateOutgoingTraffic(10, 20); |
sergeyu | 80ed35e | 2016-11-28 13:11:13 -0800 | [diff] [blame] | 108 | EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime()); |
isheriff | 3168781 | 2016-10-04 08:43:09 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | } // namespace webrtc |