- add sources.
[platform/framework/web/crosswalk.git] / src / net / quic / congestion_control / inter_arrival_bitrate_ramp_up_test.cc
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/basictypes.h"
6 #include "base/logging.h"
7 #include "net/quic/congestion_control/inter_arrival_bitrate_ramp_up.h"
8 #include "net/quic/test_tools/mock_clock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace net {
12 namespace test {
13
14 class InterArrivalBitrateRampUpTest : public ::testing::Test {
15  protected:
16   InterArrivalBitrateRampUpTest()
17       : one_ms_(QuicTime::Delta::FromMilliseconds(1)),
18         hundred_ms_(QuicTime::Delta::FromMilliseconds(100)),
19         bitrate_ramp_up_(&clock_) {
20   }
21   virtual void SetUp() {
22     clock_.AdvanceTime(one_ms_);
23   }
24   const QuicTime::Delta one_ms_;
25   const QuicTime::Delta hundred_ms_;
26   MockClock clock_;
27   InterArrivalBitrateRampUp bitrate_ramp_up_;
28 };
29
30 TEST_F(InterArrivalBitrateRampUpTest, GoodEstimates) {
31   QuicBandwidth start_rate = QuicBandwidth::FromKBytesPerSecond(100);
32   QuicBandwidth available_channel_estimate =
33       QuicBandwidth::FromKBytesPerSecond(200);
34   QuicBandwidth channel_estimate = QuicBandwidth::FromKBytesPerSecond(400);
35   QuicBandwidth halfway_point = available_channel_estimate.Add(
36       channel_estimate.Subtract(available_channel_estimate).Scale(0.5f));
37   QuicBandwidth sent_bitrate = QuicBandwidth::Zero();
38   bitrate_ramp_up_.Reset(start_rate,
39                          available_channel_estimate,
40                          channel_estimate);
41
42   // First concave growth, towards available_channel_estimate.
43   for (int i = 0; i < 25; ++i) {
44     clock_.AdvanceTime(hundred_ms_);
45     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
46     EXPECT_GE(available_channel_estimate, sent_bitrate);
47     EXPECT_LE(start_rate, sent_bitrate);
48   }
49   clock_.AdvanceTime(hundred_ms_);
50   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
51   EXPECT_LE(available_channel_estimate, sent_bitrate);
52
53   // First convex growth, from available_channel_estimate.
54   for (int j = 0; j < 25; ++j) {
55     clock_.AdvanceTime(hundred_ms_);
56     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
57     EXPECT_LE(available_channel_estimate, sent_bitrate);
58     EXPECT_GE(halfway_point, sent_bitrate);
59   }
60   clock_.AdvanceTime(hundred_ms_);
61   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
62   EXPECT_LE(halfway_point, sent_bitrate);
63
64   // Second concave growth, towards channel_estimate.
65   for (int i = 0; i < 24; ++i) {
66     clock_.AdvanceTime(hundred_ms_);
67     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
68     EXPECT_GE(channel_estimate, sent_bitrate);
69     EXPECT_LE(halfway_point, sent_bitrate);
70   }
71   clock_.AdvanceTime(hundred_ms_);
72   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
73   EXPECT_LE(channel_estimate, sent_bitrate);
74
75   // Second convex growth, from channel_estimate.
76   for (int j = 0; j < 25; ++j) {
77     clock_.AdvanceTime(hundred_ms_);
78     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
79     EXPECT_LE(channel_estimate, sent_bitrate);
80   }
81   clock_.AdvanceTime(hundred_ms_);
82   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
83   EXPECT_NEAR(channel_estimate.ToBytesPerSecond() + 100000,
84               sent_bitrate.ToBytesPerSecond(), 10000);
85
86   // Verify that we increase cubic.
87   for (int j = 0; j < 23; ++j) {
88     clock_.AdvanceTime(hundred_ms_);
89     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
90     EXPECT_LE(channel_estimate, sent_bitrate);
91   }
92   clock_.AdvanceTime(hundred_ms_);
93   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
94   EXPECT_NEAR(channel_estimate.ToBytesPerSecond() + 750000,
95               sent_bitrate.ToBytesPerSecond(), 10000);
96 }
97
98 TEST_F(InterArrivalBitrateRampUpTest, GoodEstimatesLimitedSendRate) {
99   QuicBandwidth start_rate = QuicBandwidth::FromKBytesPerSecond(100);
100   QuicBandwidth available_channel_estimate =
101       QuicBandwidth::FromKBytesPerSecond(200);
102   QuicBandwidth max_sent_rate =
103       QuicBandwidth::FromKBytesPerSecond(125);
104   QuicBandwidth channel_estimate = QuicBandwidth::FromKBytesPerSecond(400);
105   QuicBandwidth halfway_point = available_channel_estimate.Add(
106       channel_estimate.Subtract(available_channel_estimate).Scale(0.5f));
107   QuicBandwidth sent_bitrate = QuicBandwidth::Zero();
108   bitrate_ramp_up_.Reset(start_rate,
109                          available_channel_estimate,
110                          channel_estimate);
111
112   // First concave growth, towards available_channel_estimate.
113   // Should pass without being affected by the max_sent_rate.
114   for (int i = 0; i < 25; ++i) {
115     clock_.AdvanceTime(hundred_ms_);
116     // Cap our previus sent rate.
117     sent_bitrate = std::min(sent_bitrate, max_sent_rate);
118     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
119     EXPECT_GE(available_channel_estimate, sent_bitrate);
120     EXPECT_LE(start_rate, sent_bitrate);
121   }
122   clock_.AdvanceTime(hundred_ms_);
123   // Cap our previus sent rate.
124   sent_bitrate = std::min(sent_bitrate, max_sent_rate);
125   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
126   EXPECT_LE(available_channel_estimate, sent_bitrate);
127
128   // First convex growth, from available_channel_estimate.
129   for (int j = 0; j < 25; ++j) {
130     clock_.AdvanceTime(hundred_ms_);
131     // Cap our previus sent rate.
132     sent_bitrate = std::min(sent_bitrate, max_sent_rate);
133     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
134     EXPECT_LE(available_channel_estimate, sent_bitrate);
135     EXPECT_GE(halfway_point, sent_bitrate);
136   }
137   clock_.AdvanceTime(hundred_ms_);
138   sent_bitrate = std::min(sent_bitrate, max_sent_rate);
139   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
140   // We expect 2 * sent_bitrate to cap the rate.
141   EXPECT_LE(max_sent_rate.Add(max_sent_rate), sent_bitrate);
142   // Remove our sent cap.
143   // Expect bitrate to continue to ramp from its previous rate.
144   for (int j = 0; j < 5; ++j) {
145     clock_.AdvanceTime(hundred_ms_);
146     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
147     EXPECT_LE(available_channel_estimate, sent_bitrate);
148     EXPECT_LE(max_sent_rate.Add(max_sent_rate), sent_bitrate);
149     EXPECT_GE(halfway_point, sent_bitrate);
150   }
151   clock_.AdvanceTime(hundred_ms_);
152   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
153   EXPECT_LE(halfway_point, sent_bitrate);
154 }
155
156 TEST_F(InterArrivalBitrateRampUpTest, GoodEstimatesCloseToChannelEstimate) {
157   QuicBandwidth start_rate = QuicBandwidth::FromKBytesPerSecond(100);
158   QuicBandwidth available_channel_estimate =
159       QuicBandwidth::FromKBytesPerSecond(200);
160   QuicBandwidth channel_estimate = QuicBandwidth::FromKBytesPerSecond(250);
161   QuicBandwidth halfway_point = available_channel_estimate.Add(
162       channel_estimate.Subtract(available_channel_estimate).Scale(0.5f));
163   QuicBandwidth sent_bitrate = QuicBandwidth::Zero();
164   bitrate_ramp_up_.Reset(start_rate,
165                          available_channel_estimate,
166                          channel_estimate);
167
168   // First concave growth, towards available_channel_estimate.
169   for (int i = 0; i < 25; ++i) {
170     clock_.AdvanceTime(hundred_ms_);
171     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
172     EXPECT_GE(available_channel_estimate, sent_bitrate);
173     EXPECT_LE(start_rate, sent_bitrate);
174   }
175   clock_.AdvanceTime(hundred_ms_);
176   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
177   EXPECT_LE(available_channel_estimate, sent_bitrate);
178
179   // First convex growth, from available_channel_estimate.
180   for (int j = 0; j < 15; ++j) {
181     clock_.AdvanceTime(hundred_ms_);
182     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
183     EXPECT_LE(available_channel_estimate, sent_bitrate);
184     EXPECT_GE(halfway_point, sent_bitrate);
185   }
186   clock_.AdvanceTime(hundred_ms_);
187   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
188   EXPECT_LE(halfway_point, sent_bitrate);
189
190   // Second concave growth, towards channel_estimate.
191   for (int i = 0; i < 14; ++i) {
192     clock_.AdvanceTime(hundred_ms_);
193     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
194     EXPECT_GE(channel_estimate, sent_bitrate);
195     EXPECT_LE(halfway_point, sent_bitrate);
196   }
197   clock_.AdvanceTime(hundred_ms_);
198   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
199   EXPECT_LE(channel_estimate, sent_bitrate);
200
201   // Second convex growth, from channel_estimate.
202   for (int j = 0; j < 25; ++j) {
203     clock_.AdvanceTime(hundred_ms_);
204     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
205     EXPECT_LE(channel_estimate, sent_bitrate);
206   }
207   clock_.AdvanceTime(hundred_ms_);
208   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
209   EXPECT_NEAR(channel_estimate.ToBytesPerSecond() + 100000,
210               sent_bitrate.ToBytesPerSecond(), 10000);
211
212   // Verify that we increase cubic.
213   for (int j = 0; j < 24; ++j) {
214     clock_.AdvanceTime(hundred_ms_);
215     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
216     EXPECT_LE(channel_estimate, sent_bitrate);
217   }
218   clock_.AdvanceTime(hundred_ms_);
219   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
220   EXPECT_NEAR(channel_estimate.ToBytesPerSecond() + 780000,
221               sent_bitrate.ToBytesPerSecond(), 20000);
222 }
223
224 TEST_F(InterArrivalBitrateRampUpTest, UncertainEstimates) {
225   QuicBandwidth start_rate = QuicBandwidth::FromKBytesPerSecond(100);
226   QuicBandwidth available_channel_estimate =
227       QuicBandwidth::FromKBytesPerSecond(200);
228   QuicBandwidth channel_estimate =
229       QuicBandwidth::FromKBytesPerSecond(400 * 0.7f);
230   QuicBandwidth halfway_point = available_channel_estimate.Add(
231       channel_estimate.Subtract(available_channel_estimate).Scale(0.5f));
232   QuicBandwidth sent_bitrate = QuicBandwidth::Zero();
233   bitrate_ramp_up_.Reset(start_rate,
234                          available_channel_estimate,
235                          channel_estimate);
236
237   // First concave growth, towards available_channel_estimate.
238   for (int i = 0; i < 20; ++i) {
239     clock_.AdvanceTime(hundred_ms_);
240     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
241     EXPECT_GE(available_channel_estimate, sent_bitrate);
242     EXPECT_LE(start_rate, sent_bitrate);
243   }
244   clock_.AdvanceTime(hundred_ms_);
245   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
246   EXPECT_LE(available_channel_estimate, sent_bitrate);
247
248   // First convex growth, from available_channel_estimate.
249   for (int j = 0; j < 23; ++j) {
250     clock_.AdvanceTime(hundred_ms_);
251     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
252     EXPECT_LE(available_channel_estimate, sent_bitrate);
253     EXPECT_GE(halfway_point, sent_bitrate);
254   }
255   clock_.AdvanceTime(hundred_ms_);
256   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
257   EXPECT_LE(halfway_point, sent_bitrate);
258
259   // Second concave growth, towards channel_estimate.
260   for (int i = 0; i < 12; ++i) {
261     clock_.AdvanceTime(hundred_ms_);
262     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
263     EXPECT_GE(channel_estimate, sent_bitrate);
264     EXPECT_LE(halfway_point, sent_bitrate);
265   }
266   clock_.AdvanceTime(hundred_ms_);
267   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
268   EXPECT_LE(channel_estimate, sent_bitrate);
269
270   // Second convex growth, from channel_estimate.
271   for (int j = 0; j < 30; ++j) {
272     clock_.AdvanceTime(hundred_ms_);
273     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
274     EXPECT_LE(channel_estimate, sent_bitrate);
275   }
276   clock_.AdvanceTime(hundred_ms_);
277   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
278   EXPECT_NEAR(channel_estimate.ToBytesPerSecond() + 100000,
279               sent_bitrate.ToBytesPerSecond(), 10000);
280
281   // Verify that we increase cubic.
282   for (int j = 0; j < 23; ++j) {
283     clock_.AdvanceTime(hundred_ms_);
284     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
285     EXPECT_LE(channel_estimate, sent_bitrate);
286   }
287   clock_.AdvanceTime(hundred_ms_);
288   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
289   EXPECT_NEAR(channel_estimate.ToBytesPerSecond() + 750000,
290               sent_bitrate.ToBytesPerSecond(), 20000);
291 }
292
293 TEST_F(InterArrivalBitrateRampUpTest, UnknownEstimates) {
294   QuicBandwidth start_rate = QuicBandwidth::FromKBytesPerSecond(100);
295   QuicBandwidth available_channel_estimate =
296       QuicBandwidth::FromKBytesPerSecond(200);
297   QuicBandwidth channel_estimate = QuicBandwidth::FromKBytesPerSecond(400);
298   QuicBandwidth sent_bitrate = QuicBandwidth::Zero();
299   bitrate_ramp_up_.Reset(start_rate,
300                          available_channel_estimate,
301                          available_channel_estimate);
302
303   // First convex growth, from start_rate.
304   for (int j = 0; j < 20; ++j) {
305     clock_.AdvanceTime(hundred_ms_);
306     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
307     EXPECT_LE(start_rate, sent_bitrate);
308     EXPECT_GE(available_channel_estimate, sent_bitrate);
309   }
310   clock_.AdvanceTime(hundred_ms_);
311   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
312   EXPECT_NEAR(available_channel_estimate.ToBytesPerSecond(),
313               sent_bitrate.ToBytesPerSecond(), 10000);
314
315   // Verify that we increase cubic.
316   for (int j = 0; j < 31; ++j) {
317     clock_.AdvanceTime(hundred_ms_);
318     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
319     EXPECT_GE(channel_estimate, sent_bitrate);
320   }
321   clock_.AdvanceTime(hundred_ms_);
322   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
323   EXPECT_NEAR(channel_estimate.ToBytesPerSecond(),
324               sent_bitrate.ToBytesPerSecond(), 10000);
325 }
326
327 TEST_F(InterArrivalBitrateRampUpTest, UpdatingChannelEstimateHigher) {
328   QuicBandwidth start_rate = QuicBandwidth::FromKBytesPerSecond(200);
329   QuicBandwidth available_channel_estimate = start_rate;
330   QuicBandwidth channel_estimate = QuicBandwidth::FromKBytesPerSecond(250);
331   QuicBandwidth halfway_point = available_channel_estimate.Add(
332       channel_estimate.Subtract(available_channel_estimate).Scale(0.5f));
333   QuicBandwidth sent_bitrate = QuicBandwidth::Zero();
334   bitrate_ramp_up_.Reset(start_rate,
335                          available_channel_estimate,
336                          channel_estimate);
337
338   // Convex growth, from available_channel_estimate.
339   for (int j = 0; j < 16; ++j) {
340     clock_.AdvanceTime(hundred_ms_);
341     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
342     EXPECT_LE(available_channel_estimate, sent_bitrate);
343     EXPECT_GE(halfway_point, sent_bitrate);
344   }
345   clock_.AdvanceTime(hundred_ms_);
346   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
347   EXPECT_LE(halfway_point, sent_bitrate);
348
349   // Increse channel estimate.
350   channel_estimate = QuicBandwidth::FromKBytesPerSecond(300);
351   bitrate_ramp_up_.UpdateChannelEstimate(channel_estimate);
352
353   // Concave growth, towards channel_estimate.
354   for (int i = 0; i < 22; ++i) {
355     clock_.AdvanceTime(hundred_ms_);
356     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
357     EXPECT_GE(channel_estimate, sent_bitrate);
358     EXPECT_LE(halfway_point, sent_bitrate);
359   }
360   clock_.AdvanceTime(hundred_ms_);
361   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
362   EXPECT_LE(channel_estimate, sent_bitrate);
363 }
364
365 TEST_F(InterArrivalBitrateRampUpTest, UpdatingChannelEstimateLower) {
366   QuicBandwidth start_rate = QuicBandwidth::FromKBytesPerSecond(200);
367   QuicBandwidth available_channel_estimate = start_rate;
368   QuicBandwidth channel_estimate = QuicBandwidth::FromKBytesPerSecond(250);
369   QuicBandwidth halfway_point = available_channel_estimate.Add(
370       channel_estimate.Subtract(available_channel_estimate).Scale(0.5f));
371   QuicBandwidth sent_bitrate = QuicBandwidth::Zero();
372   bitrate_ramp_up_.Reset(start_rate,
373                          available_channel_estimate,
374                          channel_estimate);
375
376   // Convex growth, from available_channel_estimate.
377   for (int j = 0; j < 16; ++j) {
378     clock_.AdvanceTime(hundred_ms_);
379     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
380     EXPECT_LE(available_channel_estimate, sent_bitrate);
381     EXPECT_GE(halfway_point, sent_bitrate);
382   }
383   clock_.AdvanceTime(hundred_ms_);
384   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
385   EXPECT_LE(halfway_point, sent_bitrate);
386
387   // Decrese channel estimate.
388   channel_estimate = QuicBandwidth::FromKBytesPerSecond(240);
389   bitrate_ramp_up_.UpdateChannelEstimate(channel_estimate);
390
391   // Concave growth, towards channel_estimate.
392   for (int i = 0; i < 11; ++i) {
393     clock_.AdvanceTime(hundred_ms_);
394     sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
395     EXPECT_GE(channel_estimate, sent_bitrate);
396     EXPECT_LE(halfway_point, sent_bitrate);
397   }
398   clock_.AdvanceTime(hundred_ms_);
399   sent_bitrate = bitrate_ramp_up_.GetNewBitrate(sent_bitrate);
400   EXPECT_LE(channel_estimate, sent_bitrate);
401 }
402
403 }  // namespace test
404 }  // namespace net