blob: 3c912772235a13b94b705de1f1cb2e2855e50089 [file] [log] [blame]
isheriff31687812016-10-04 08:43:09 -07001/*
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
isheriff31687812016-10-04 08:43:09 -070011#include "webrtc/modules/pacing/alr_detector.h"
12
Sergey Ulanov0182f852016-11-16 15:42:11 -080013#include "webrtc/test/gtest.h"
14
isheriff31687812016-10-04 08:43:09 -070015namespace {
16
isheriff31687812016-10-04 08:43:09 -070017constexpr int kEstimatedBitrateBps = 300000;
isheriff31687812016-10-04 08:43:09 -070018
19} // namespace
20
21namespace webrtc {
22
Sergey Ulanov0182f852016-11-16 15:42:11 -080023class AlrDetectorTest : public testing::Test {
24 public:
25 void SetUp() override {
26 alr_detector_.SetEstimatedBitrate(kEstimatedBitrateBps);
27 }
isheriff31687812016-10-04 08:43:09 -070028
Sergey Ulanov0182f852016-11-16 15:42:11 -080029 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 }
isheriff31687812016-10-04 08:43:09 -070037
Sergey Ulanov0182f852016-11-16 15:42:11 -080038 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
52TEST_F(AlrDetectorTest, AlrDetection) {
53 // Start in non-ALR state.
sergeyu80ed35e2016-11-28 13:11:13 -080054 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -080055
56 // Stay in non-ALR state when usage is close to 100%.
57 SimulateOutgoingTraffic(500, 90);
sergeyu80ed35e2016-11-28 13:11:13 -080058 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -080059
60 // Verify that we ALR starts when bitrate drops below 20%.
61 SimulateOutgoingTraffic(500, 20);
sergeyu80ed35e2016-11-28 13:11:13 -080062 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -080063
stefanf00497c2017-01-27 02:27:33 -080064 // Verify that we remain in ALR state while usage is still below 70%.
65 SimulateOutgoingTraffic(500, 69);
sergeyu80ed35e2016-11-28 13:11:13 -080066 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -080067
stefanf00497c2017-01-27 02:27:33 -080068 // Verify that ALR ends when usage is above 70%.
69 SimulateOutgoingTraffic(500, 75);
sergeyu80ed35e2016-11-28 13:11:13 -080070 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
isheriff31687812016-10-04 08:43:09 -070071}
72
Sergey Ulanov0182f852016-11-16 15:42:11 -080073TEST_F(AlrDetectorTest, ShortSpike) {
74 // Start in non-ALR state.
sergeyu80ed35e2016-11-28 13:11:13 -080075 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
isheriff31687812016-10-04 08:43:09 -070076
Sergey Ulanov0182f852016-11-16 15:42:11 -080077 // Verify that we ALR starts when bitrate drops below 20%.
78 SimulateOutgoingTraffic(500, 20);
sergeyu80ed35e2016-11-28 13:11:13 -080079 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -080080
81 // Verify that we stay in ALR region even after a short bitrate spike.
82 SimulateOutgoingTraffic(100, 150);
sergeyu80ed35e2016-11-28 13:11:13 -080083 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -080084
85 SimulateOutgoingTraffic(200, 20);
sergeyu80ed35e2016-11-28 13:11:13 -080086 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -080087
stefanf00497c2017-01-27 02:27:33 -080088 // ALR ends when usage is above 70%.
89 SimulateOutgoingTraffic(500, 75);
sergeyu80ed35e2016-11-28 13:11:13 -080090 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -080091}
92
93TEST_F(AlrDetectorTest, BandwidthEstimateChanges) {
94 // Start in non-ALR state.
sergeyu80ed35e2016-11-28 13:11:13 -080095 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -080096
97 // ALR starts when bitrate drops below 20%.
98 SimulateOutgoingTraffic(500, 20);
sergeyu80ed35e2016-11-28 13:11:13 -080099 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -0800100
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);
sergeyu80ed35e2016-11-28 13:11:13 -0800106 EXPECT_TRUE(alr_detector_.GetApplicationLimitedRegionStartTime());
Sergey Ulanov0182f852016-11-16 15:42:11 -0800107 SimulateOutgoingTraffic(10, 20);
sergeyu80ed35e2016-11-28 13:11:13 -0800108 EXPECT_FALSE(alr_detector_.GetApplicationLimitedRegionStartTime());
isheriff31687812016-10-04 08:43:09 -0700109}
110
111} // namespace webrtc