OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 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 |
11 #include <limits> | 11 #include <limits> |
| 12 #include <vector> |
12 | 13 |
13 #include "webrtc/modules/pacing/bitrate_prober.h" | 14 #include "webrtc/modules/pacing/bitrate_prober.h" |
| 15 #include "webrtc/system_wrappers/include/clock.h" |
14 #include "webrtc/test/gtest.h" | 16 #include "webrtc/test/gtest.h" |
15 | 17 |
16 namespace webrtc { | 18 namespace webrtc { |
17 | 19 |
18 TEST(BitrateProberTest, VerifyStatesAndTimeBetweenProbes) { | 20 class BitrateProberTest : public ::testing::Test, |
19 BitrateProber prober; | 21 public ProbeClusterCreatedObserver { |
20 EXPECT_FALSE(prober.IsProbing()); | 22 public: |
21 int64_t now_ms = 0; | 23 struct ProbeInfo { |
22 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms)); | 24 int cluster_id = 0; |
| 25 int min_bytes = 0; |
| 26 int min_probes = 0; |
| 27 }; |
| 28 |
| 29 void OnProbingClusterCreated(int cluster_id, |
| 30 int min_bytes, |
| 31 int min_probes) override { |
| 32 ProbeInfo info; |
| 33 info.cluster_id = cluster_id; |
| 34 info.min_bytes = min_bytes; |
| 35 info.min_probes = min_probes; |
| 36 probe_info_.push_back(info); |
| 37 } |
| 38 |
| 39 protected: |
| 40 BitrateProberTest() : prober_(this), clock_(0) {} |
| 41 BitrateProber prober_; |
| 42 SimulatedClock clock_; |
| 43 std::vector<ProbeInfo> probe_info_; |
| 44 }; |
| 45 |
| 46 TEST_F(BitrateProberTest, VerifyStatesAndTimeBetweenProbes) { |
| 47 int64_t now_ms = clock_.TimeInMilliseconds(); |
| 48 EXPECT_FALSE(prober_.IsProbing()); |
| 49 EXPECT_EQ(-1, prober_.TimeUntilNextProbe(now_ms)); |
23 | 50 |
24 const int kTestBitrate1 = 900000; | 51 const int kTestBitrate1 = 900000; |
25 const int kTestBitrate2 = 1800000; | 52 const int kTestBitrate2 = 1800000; |
26 const int kClusterSize = 5; | 53 const int kClusterSize = 5; |
27 const int kProbeSize = 1000; | 54 const int kProbeSize = 1000; |
28 const int kMinProbeDurationMs = 15; | 55 const int kMinProbeDurationMs = 15; |
29 | 56 |
30 prober.CreateProbeCluster(kTestBitrate1); | 57 prober_.CreateProbeCluster(kTestBitrate1); |
31 prober.CreateProbeCluster(kTestBitrate2); | 58 prober_.CreateProbeCluster(kTestBitrate2); |
32 EXPECT_FALSE(prober.IsProbing()); | 59 EXPECT_FALSE(prober_.IsProbing()); |
33 | 60 |
34 prober.OnIncomingPacket(kProbeSize); | 61 prober_.OnIncomingPacket(kProbeSize); |
35 EXPECT_TRUE(prober.IsProbing()); | 62 EXPECT_TRUE(prober_.IsProbing()); |
36 EXPECT_EQ(0, prober.CurrentClusterId()); | 63 EXPECT_EQ(0, prober_.CurrentClusterId()); |
37 | 64 |
38 // First packet should probe as soon as possible. | 65 // First packet should probe as soon as possible. |
39 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms)); | 66 EXPECT_EQ(0, prober_.TimeUntilNextProbe(now_ms)); |
40 | 67 |
41 for (int i = 0; i < kClusterSize; ++i) { | 68 for (int i = 0; i < kClusterSize; ++i) { |
42 now_ms += prober.TimeUntilNextProbe(now_ms); | 69 now_ms += prober_.TimeUntilNextProbe(now_ms); |
43 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms)); | 70 EXPECT_EQ(0, prober_.TimeUntilNextProbe(now_ms)); |
44 EXPECT_EQ(0, prober.CurrentClusterId()); | 71 EXPECT_EQ(0, prober_.CurrentClusterId()); |
45 prober.ProbeSent(now_ms, kProbeSize); | 72 prober_.ProbeSent(now_ms, kProbeSize); |
46 } | 73 } |
47 | 74 |
48 EXPECT_GE(now_ms, kMinProbeDurationMs); | 75 EXPECT_GE(now_ms, kMinProbeDurationMs); |
49 // Verify that the actual bitrate is withing 10% of the target. | 76 // Verify that the actual bitrate is withing 10% of the target. |
50 double bitrate = kProbeSize * (kClusterSize - 1) * 8 * 1000.0 / now_ms; | 77 double bitrate = kProbeSize * (kClusterSize - 1) * 8 * 1000.0 / now_ms; |
51 EXPECT_GT(bitrate, kTestBitrate1 * 0.9); | 78 EXPECT_GT(bitrate, kTestBitrate1 * 0.9); |
52 EXPECT_LT(bitrate, kTestBitrate1 * 1.1); | 79 EXPECT_LT(bitrate, kTestBitrate1 * 1.1); |
53 | 80 |
54 now_ms += prober.TimeUntilNextProbe(now_ms); | 81 now_ms += prober_.TimeUntilNextProbe(now_ms); |
55 int64_t probe2_started = now_ms; | 82 int64_t probe2_started = now_ms; |
56 | 83 |
57 for (int i = 0; i < kClusterSize; ++i) { | 84 for (int i = 0; i < kClusterSize; ++i) { |
58 now_ms += prober.TimeUntilNextProbe(now_ms); | 85 now_ms += prober_.TimeUntilNextProbe(now_ms); |
59 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms)); | 86 EXPECT_EQ(0, prober_.TimeUntilNextProbe(now_ms)); |
60 EXPECT_EQ(1, prober.CurrentClusterId()); | 87 EXPECT_EQ(1, prober_.CurrentClusterId()); |
61 prober.ProbeSent(now_ms, kProbeSize); | 88 prober_.ProbeSent(now_ms, kProbeSize); |
62 } | 89 } |
63 | 90 |
64 // Verify that the actual bitrate is withing 10% of the target. | 91 // Verify that the actual bitrate is withing 10% of the target. |
65 int duration = now_ms - probe2_started; | 92 int duration = now_ms - probe2_started; |
66 EXPECT_GE(duration, kMinProbeDurationMs); | 93 EXPECT_GE(duration, kMinProbeDurationMs); |
67 bitrate = kProbeSize * (kClusterSize - 1) * 8 * 1000.0 / duration; | 94 bitrate = kProbeSize * (kClusterSize - 1) * 8 * 1000.0 / duration; |
68 EXPECT_GT(bitrate, kTestBitrate2 * 0.9); | 95 EXPECT_GT(bitrate, kTestBitrate2 * 0.9); |
69 EXPECT_LT(bitrate, kTestBitrate2 * 1.1); | 96 EXPECT_LT(bitrate, kTestBitrate2 * 1.1); |
70 | 97 |
71 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms)); | 98 EXPECT_EQ(-1, prober_.TimeUntilNextProbe(now_ms)); |
72 EXPECT_FALSE(prober.IsProbing()); | 99 EXPECT_FALSE(prober_.IsProbing()); |
73 } | 100 } |
74 | 101 |
75 TEST(BitrateProberTest, DoesntProbeWithoutRecentPackets) { | 102 TEST_F(BitrateProberTest, DoesntProbeWithoutRecentPackets) { |
76 BitrateProber prober; | 103 EXPECT_FALSE(prober_.IsProbing()); |
77 EXPECT_FALSE(prober.IsProbing()); | |
78 int64_t now_ms = 0; | 104 int64_t now_ms = 0; |
79 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms)); | 105 EXPECT_EQ(-1, prober_.TimeUntilNextProbe(now_ms)); |
80 | 106 |
81 prober.CreateProbeCluster(900000); | 107 prober_.CreateProbeCluster(900000); |
82 EXPECT_FALSE(prober.IsProbing()); | 108 EXPECT_FALSE(prober_.IsProbing()); |
83 | 109 |
84 prober.OnIncomingPacket(1000); | 110 prober_.OnIncomingPacket(1000); |
85 EXPECT_TRUE(prober.IsProbing()); | 111 EXPECT_TRUE(prober_.IsProbing()); |
86 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms)); | 112 EXPECT_EQ(0, prober_.TimeUntilNextProbe(now_ms)); |
87 prober.ProbeSent(now_ms, 1000); | 113 prober_.ProbeSent(now_ms, 1000); |
88 // Let time pass, no large enough packets put into prober. | 114 // Let time pass, no large enough packets put into prober_. |
89 now_ms += 6000; | 115 now_ms += 6000; |
90 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms)); | 116 EXPECT_EQ(-1, prober_.TimeUntilNextProbe(now_ms)); |
91 // Insert a small packet, not a candidate for probing. | 117 // Insert a small packet, not a candidate for probing. |
92 prober.OnIncomingPacket(100); | 118 prober_.OnIncomingPacket(100); |
93 EXPECT_FALSE(prober.IsProbing()); | 119 EXPECT_FALSE(prober_.IsProbing()); |
94 EXPECT_EQ(-1, prober.TimeUntilNextProbe(now_ms)); | 120 EXPECT_EQ(-1, prober_.TimeUntilNextProbe(now_ms)); |
95 // Insert a large-enough packet after downtime while probing should reset to | 121 // Insert a large-enough packet after downtime while probing should reset to |
96 // perform a new probe since the requested one didn't finish. | 122 // perform a new probe since the requested one didn't finish. |
97 prober.OnIncomingPacket(1000); | 123 prober_.OnIncomingPacket(1000); |
98 EXPECT_EQ(0, prober.TimeUntilNextProbe(now_ms)); | 124 EXPECT_EQ(0, prober_.TimeUntilNextProbe(now_ms)); |
99 prober.ProbeSent(now_ms, 1000); | 125 prober_.ProbeSent(now_ms, 1000); |
100 // Next packet should be part of new probe and be sent with non-zero delay. | 126 // Next packet should be part of new probe and be sent with non-zero delay. |
101 prober.OnIncomingPacket(1000); | 127 prober_.OnIncomingPacket(1000); |
102 EXPECT_GT(prober.TimeUntilNextProbe(now_ms), 0); | 128 EXPECT_GT(prober_.TimeUntilNextProbe(now_ms), 0); |
103 } | 129 } |
104 | 130 |
105 TEST(BitrateProberTest, DoesntInitializeProbingForSmallPackets) { | 131 TEST_F(BitrateProberTest, DoesntInitializeProbingForSmallPackets) { |
106 BitrateProber prober; | 132 prober_.SetEnabled(true); |
107 prober.SetEnabled(true); | 133 EXPECT_FALSE(prober_.IsProbing()); |
108 EXPECT_FALSE(prober.IsProbing()); | |
109 | 134 |
110 prober.OnIncomingPacket(100); | 135 prober_.OnIncomingPacket(100); |
111 EXPECT_FALSE(prober.IsProbing()); | 136 EXPECT_FALSE(prober_.IsProbing()); |
112 } | 137 } |
113 | 138 |
114 TEST(BitrateProberTest, VerifyProbeSizeOnHighBitrate) { | 139 TEST_F(BitrateProberTest, VerifyProbeSizeOnHighBitrate) { |
115 BitrateProber prober; | |
116 constexpr unsigned kHighBitrateBps = 10000000; // 10 Mbps | 140 constexpr unsigned kHighBitrateBps = 10000000; // 10 Mbps |
117 | 141 |
118 prober.CreateProbeCluster(kHighBitrateBps); | 142 prober_.CreateProbeCluster(kHighBitrateBps); |
119 // Probe size should ensure a minimum of 1 ms interval. | 143 // Probe size should ensure a minimum of 1 ms interval. |
120 EXPECT_GT(prober.RecommendedMinProbeSize(), kHighBitrateBps / 8000); | 144 EXPECT_GT(prober_.RecommendedMinProbeSize(), kHighBitrateBps / 8000); |
121 } | 145 } |
122 | 146 |
123 TEST(BitrateProberTest, MinumumNumberOfProbingPackets) { | 147 TEST_F(BitrateProberTest, MinProbingPacketsUsedLowBitrates) { |
124 BitrateProber prober; | |
125 // Even when probing at a low bitrate we expect a minimum number | 148 // Even when probing at a low bitrate we expect a minimum number |
126 // of packets to be sent. | 149 // of packets to be sent. |
127 constexpr int kBitrateBps = 100000; // 100 kbps | 150 constexpr int kLowBitrate = 100000; // 100 kbps |
128 constexpr int kPacketSizeBytes = 1000; | 151 constexpr int kPacketSize = 1400; |
| 152 int64_t now_ms = clock_.TimeInMilliseconds(); |
| 153 prober_.SetEnabled(true); |
| 154 prober_.CreateProbeCluster(kLowBitrate); |
| 155 prober_.OnIncomingPacket(200); |
| 156 EXPECT_TRUE(prober_.IsProbing()); |
129 | 157 |
130 prober.CreateProbeCluster(kBitrateBps); | 158 EXPECT_GT(probe_info_[0].min_probes, 0); |
131 prober.OnIncomingPacket(kPacketSizeBytes); | 159 int probes_sent = 0; |
132 for (int i = 0; i < 5; ++i) { | 160 int bytes_sent = 0l; |
133 EXPECT_TRUE(prober.IsProbing()); | 161 while (prober_.IsProbing()) { |
134 prober.ProbeSent(0, kPacketSizeBytes); | 162 prober_.ProbeSent(now_ms, kPacketSize); |
| 163 ++probes_sent; |
| 164 bytes_sent += kPacketSize; |
135 } | 165 } |
136 | 166 |
137 EXPECT_FALSE(prober.IsProbing()); | 167 EXPECT_EQ(probes_sent, probe_info_[0].min_probes); |
138 } | 168 } |
139 | 169 |
140 TEST(BitrateProberTest, ScaleBytesUsedForProbing) { | 170 TEST_F(BitrateProberTest, ScaleBytesUsedForProbing) { |
141 BitrateProber prober; | |
142 constexpr int kBitrateBps = 10000000; // 10 Mbps | 171 constexpr int kBitrateBps = 10000000; // 10 Mbps |
143 constexpr int kPacketSizeBytes = 1000; | 172 constexpr int kPacketSizeBytes = 1000; |
144 constexpr int kExpectedBytesSent = kBitrateBps * 15 / 8000; | 173 constexpr int kExpectedBytesSent = kBitrateBps * 15 / 8000; |
145 | 174 |
146 prober.CreateProbeCluster(kBitrateBps); | 175 prober_.CreateProbeCluster(kBitrateBps); |
147 prober.OnIncomingPacket(kPacketSizeBytes); | 176 prober_.OnIncomingPacket(kPacketSizeBytes); |
148 int bytes_sent = 0; | 177 int bytes_sent = 0; |
149 while (bytes_sent < kExpectedBytesSent) { | 178 while (bytes_sent < kExpectedBytesSent) { |
150 EXPECT_TRUE(prober.IsProbing()); | 179 EXPECT_TRUE(prober_.IsProbing()); |
151 prober.ProbeSent(0, kPacketSizeBytes); | 180 prober_.ProbeSent(0, kPacketSizeBytes); |
152 bytes_sent += kPacketSizeBytes; | 181 bytes_sent += kPacketSizeBytes; |
153 } | 182 } |
154 | 183 |
155 EXPECT_FALSE(prober.IsProbing()); | 184 EXPECT_FALSE(prober_.IsProbing()); |
156 } | 185 } |
157 | 186 |
158 } // namespace webrtc | 187 } // namespace webrtc |
OLD | NEW |