Debian Codegraph @

tests/test_recovery_cubic.py

raw symbols SCIP
1 import math
2 from unittest import TestCase
3
4 from aioquic import tls
5 from aioquic.quic.congestion.base import K_INITIAL_WINDOW, K_MINIMUM_WINDOW
6 from aioquic.quic.congestion.cubic import (
7 K_CUBIC_C,
8 K_CUBIC_LOSS_REDUCTION_FACTOR,
9 CubicCongestionControl,
10 better_cube_root,
11 )
12 from aioquic.quic.packet import QuicPacketType
13 from aioquic.quic.packet_builder import QuicSentPacket
14 from aioquic.quic.rangeset import RangeSet
15 from aioquic.quic.recovery import QuicPacketRecovery, QuicPacketSpace
16
17
18 def send_probe():
19 pass
20
21
22 def W_cubic(t, K, W_max):
23 return K_CUBIC_C * (t - K) ** 3 + (W_max)
24
25
26 class QuicPacketRecoveryCubicTest(TestCase):
27 def setUp(self):
28 self.INITIAL_SPACE = QuicPacketSpace()
29 self.HANDSHAKE_SPACE = QuicPacketSpace()
30 self.ONE_RTT_SPACE = QuicPacketSpace()
31
32 self.recovery = QuicPacketRecovery(
33 congestion_control_algorithm="cubic",
34 initial_rtt=0.1,
35 max_datagram_size=1280,
36 peer_completed_address_validation=True,
37 send_probe=send_probe,
38 )
39 self.recovery.spaces = [
40 self.INITIAL_SPACE,
41 self.HANDSHAKE_SPACE,
42 self.ONE_RTT_SPACE,
43 ]
44
45 def test_better_cube_root(self):
46 self.assertAlmostEqual(better_cube_root(8), 2)
47 self.assertAlmostEqual(better_cube_root(-8), -2)
48 self.assertAlmostEqual(better_cube_root(0), 0)
49 self.assertAlmostEqual(better_cube_root(27), 3)
50
51 def test_discard_space(self):
52 self.recovery.discard_space(self.INITIAL_SPACE)
53
54 def test_on_ack_received_ack_eliciting(self):
55 packet = QuicSentPacket(
56 epoch=tls.Epoch.ONE_RTT,
57 in_flight=True,
58 is_ack_eliciting=True,
59 is_crypto_packet=False,
60 packet_number=0,
61 packet_type=QuicPacketType.ONE_RTT,
62 sent_bytes=1280,
63 sent_time=0.0,
64 )
65 space = self.ONE_RTT_SPACE
66
67 #  packet sent
68 self.recovery.on_packet_sent(packet=packet, space=space)
69 self.assertEqual(self.recovery.bytes_in_flight, 1280)
70 self.assertEqual(space.ack_eliciting_in_flight, 1)
71 self.assertEqual(len(space.sent_packets), 1)
72
73 # packet ack'd
74 self.recovery.on_ack_received(
75 ack_rangeset=RangeSet([range(0, 1)]),
76 ack_delay=0.0,
77 now=10.0,
78 space=space,
79 )
80 self.assertEqual(self.recovery.bytes_in_flight, 0)
81 self.assertEqual(space.ack_eliciting_in_flight, 0)
82 self.assertEqual(len(space.sent_packets), 0)
83
84 # check RTT
85 self.assertTrue(self.recovery._rtt_initialized)
86 self.assertEqual(self.recovery._rtt_latest, 10.0)
87 self.assertEqual(self.recovery._rtt_min, 10.0)
88 self.assertEqual(self.recovery._rtt_smoothed, 10.0)
89
90 def test_on_ack_received_non_ack_eliciting(self):
91 packet = QuicSentPacket(
92 epoch=tls.Epoch.ONE_RTT,
93 in_flight=True,
94 is_ack_eliciting=False,
95 is_crypto_packet=False,
96 packet_number=0,
97 packet_type=QuicPacketType.ONE_RTT,
98 sent_bytes=1280,
99 sent_time=123.45,
100 )
101 space = self.ONE_RTT_SPACE
102
103 #  packet sent
104 self.recovery.on_packet_sent(packet=packet, space=space)
105 self.assertEqual(self.recovery.bytes_in_flight, 1280)
106 self.assertEqual(space.ack_eliciting_in_flight, 0)
107 self.assertEqual(len(space.sent_packets), 1)
108
109 # packet ack'd
110 self.recovery.on_ack_received(
111 ack_rangeset=RangeSet([range(0, 1)]),
112 ack_delay=0.0,
113 now=10.0,
114 space=space,
115 )
116 self.assertEqual(self.recovery.bytes_in_flight, 0)
117 self.assertEqual(space.ack_eliciting_in_flight, 0)
118 self.assertEqual(len(space.sent_packets), 0)
119
120 # check RTT
121 self.assertFalse(self.recovery._rtt_initialized)
122 self.assertEqual(self.recovery._rtt_latest, 0.0)
123 self.assertEqual(self.recovery._rtt_min, math.inf)
124 self.assertEqual(self.recovery._rtt_smoothed, 0.0)
125
126 def test_on_packet_lost_crypto(self):
127 packet = QuicSentPacket(
128 epoch=tls.Epoch.INITIAL,
129 in_flight=True,
130 is_ack_eliciting=True,
131 is_crypto_packet=True,
132 packet_number=0,
133 packet_type=QuicPacketType.INITIAL,
134 sent_bytes=1280,
135 sent_time=0.0,
136 )
137 space = self.INITIAL_SPACE
138
139 self.recovery.on_packet_sent(packet=packet, space=space)
140 self.assertEqual(self.recovery.bytes_in_flight, 1280)
141 self.assertEqual(space.ack_eliciting_in_flight, 1)
142 self.assertEqual(len(space.sent_packets), 1)
143
144 self.recovery._detect_loss(space=space, now=1.0)
145 self.assertEqual(self.recovery.bytes_in_flight, 0)
146 self.assertEqual(space.ack_eliciting_in_flight, 0)
147 self.assertEqual(len(space.sent_packets), 0)
148
149 def test_packet_expired(self):
150 packet = QuicSentPacket(
151 epoch=tls.Epoch.ONE_RTT,
152 in_flight=True,
153 is_ack_eliciting=True,
154 is_crypto_packet=False,
155 packet_number=0,
156 packet_type=QuicPacketType.ONE_RTT,
157 sent_bytes=1280,
158 sent_time=0.0,
159 )
160
161 cubic = CubicCongestionControl(1440)
162 cubic.on_packet_sent(packet=packet)
163
164 cubic.on_packets_expired(packets=[packet])
165
166 self.assertEqual(cubic.bytes_in_flight, 0)
167
168 def test_log_data(self):
169 cubic = CubicCongestionControl(1440)
170
171 self.assertEqual(
172 cubic.get_log_data(),
173 {
174 "cwnd": cubic.congestion_window,
175 "bytes_in_flight": cubic.bytes_in_flight,
176 "cubic-wmax": cubic._W_max,
177 },
178 )
179
180 cubic._W_max = 5000
181 cubic.ssthresh = 5000
182
183 self.assertEqual(
184 cubic.get_log_data(),
185 {
186 "cwnd": cubic.congestion_window,
187 "ssthresh": cubic.ssthresh,
188 "bytes_in_flight": cubic.bytes_in_flight,
189 "cubic-wmax": cubic._W_max,
190 },
191 )
192
193 def test_congestion_avoidance(self):
194 """
195 Check if the cubic implementation respects the mathematical
196 formula defined in the rfc 9438
197 """
198
199 max_datagram_size = 1440
200
201 n = 400 # number of ms to check
202
203 W_max = 5 # starting W_max
204 K = better_cube_root(W_max * (1 - K_CUBIC_LOSS_REDUCTION_FACTOR) / K_CUBIC_C)
205 cwnd = W_max * K_CUBIC_LOSS_REDUCTION_FACTOR
206
207 correct = []
208
209 test_range = range(n)
210
211 for i in test_range:
212 correct.append(W_cubic(i / 1000, K, W_max) * max_datagram_size)
213
214 cubic = CubicCongestionControl(max_datagram_size)
215 cubic.rtt = 0
216 cubic._W_max = W_max * max_datagram_size
217 cubic._starting_congestion_avoidance = True
218 cubic.congestion_window = cwnd * max_datagram_size
219 cubic.ssthresh = cubic.congestion_window
220 cubic._W_est = 0
221
222 results = []
223 for i in test_range:
224 cwnd = cubic.congestion_window // max_datagram_size # number of segments
225
226 # simulate the reception of cwnd packets (a full window of acks)
227 for _ in range(int(cwnd)):
228 packet = QuicSentPacket(None, True, True, True, 0, 0)
229 packet.sent_bytes = 0 # won't affect results
230
231 cubic.on_packet_acked(packet=packet, now=(i / 1000))
232
233 results.append(cubic.congestion_window)
234
235 for i in test_range:
236 # check if it is almost equal to the value of W_cubic
237 self.assertTrue(
238 correct[i] * 0.99 <= results[i] <= 1.01 * correct[i],
239 f"Error at {i}ms, Result={results[i]}, Expected={correct[i]}",
240 )
241
242 def test_reset_idle(self):
243 packet = QuicSentPacket(
244 epoch=tls.Epoch.ONE_RTT,
245 in_flight=True,
246 is_ack_eliciting=True,
247 is_crypto_packet=False,
248 packet_number=0,
249 packet_type=QuicPacketType.ONE_RTT,
250 sent_bytes=1280,
251 sent_time=10.0,
252 )
253
254 max_datagram_size = 1440
255
256 cubic = CubicCongestionControl(1440)
257 # set last received at time 1
258 cubic.last_ack = 1
259
260 # receive a packet after 9s of idle time
261 cubic.on_packet_sent(packet=packet)
262
263 cubic.on_packets_expired(packets=[packet])
264
265 self.assertEqual(cubic.congestion_window, K_INITIAL_WINDOW * max_datagram_size)
266
267 self.assertIsNone(cubic.ssthresh)
268
269 self.assertTrue(cubic._first_slow_start)
270 self.assertFalse(cubic._starting_congestion_avoidance)
271 self.assertEqual(cubic.K, 0.0)
272 self.assertEqual(cubic._W_est, 0)
273 self.assertEqual(cubic._cwnd_epoch, 0)
274 self.assertEqual(cubic._t_epoch, 0.0)
275
276 self.assertEqual(cubic._W_max, K_INITIAL_WINDOW * max_datagram_size)
277
278 def test_reno_friendly_region(self):
279 cubic = CubicCongestionControl(1440)
280 cubic._W_max = 5000 # set the target number of bytes to 5000
281 cubic._cwnd_epoch = 2880 # a cwnd of 1440 bytes when we had congestion
282 cubic._starting_congestion_avoidance = False
283 cubic._first_slow_start = False
284 cubic.ssthresh = 2880
285 cubic._t_epoch = 5
286
287 # set an arbitrarily high W_est,
288 # meaning that cubic would underperform compared to reno
289 cubic._W_est = 100000
290
291 # calculate K
292 W_max_segments = cubic._W_max / cubic._max_datagram_size
293 cwnd_epoch_segments = cubic._cwnd_epoch / cubic._max_datagram_size
294 cubic.K = better_cube_root((W_max_segments - cwnd_epoch_segments) / K_CUBIC_C)
295
296 packet = QuicSentPacket(
297 epoch=tls.Epoch.ONE_RTT,
298 in_flight=True,
299 is_ack_eliciting=True,
300 is_crypto_packet=False,
301 packet_number=0,
302 packet_type=QuicPacketType.ONE_RTT,
303 sent_bytes=1280,
304 sent_time=0.0,
305 )
306
307 previous_cwnd = cubic.congestion_window
308
309 cubic.on_packet_acked(now=10, packet=packet)
310
311 # congestion window should be equal to W_est (Reno estimated window)
312 self.assertAlmostEqual(
313 cubic.congestion_window,
314 100000
315 + cubic.additive_increase_factor * (packet.sent_bytes / previous_cwnd),
316 )
317
318 def test_convex_region(self):
319 cubic = CubicCongestionControl(1440)
320 cubic._W_max = 5000 # set the target number of bytes to 5000
321 cubic._cwnd_epoch = 2880 # a cwnd of 1440 bytes when we had congestion
322 cubic._starting_congestion_avoidance = False
323 cubic._first_slow_start = False
324 cubic.ssthresh = 2880
325 cubic._t_epoch = 5
326
327 cubic._W_est = 0
328
329 # calculate K
330 W_max_segments = cubic._W_max / cubic._max_datagram_size
331 cwnd_epoch_segments = cubic._cwnd_epoch / cubic._max_datagram_size
332 cubic.K = better_cube_root((W_max_segments - cwnd_epoch_segments) / K_CUBIC_C)
333
334 packet = QuicSentPacket(
335 epoch=tls.Epoch.ONE_RTT,
336 in_flight=True,
337 is_ack_eliciting=True,
338 is_crypto_packet=False,
339 packet_number=0,
340 packet_type=QuicPacketType.ONE_RTT,
341 sent_bytes=1280,
342 sent_time=0.0,
343 )
344
345 previous_cwnd = cubic.congestion_window
346
347 cubic.on_packet_acked(now=10, packet=packet)
348
349 # elapsed time + basic rtt
350 target = int(previous_cwnd * 1.5)
351
352 expected = int(
353 previous_cwnd
354 + ((target - previous_cwnd) * (cubic._max_datagram_size / previous_cwnd))
355 )
356
357 # congestion window should be equal to W_est (Reno estimated window)
358 self.assertAlmostEqual(cubic.congestion_window, expected)
359
360 def test_concave_region(self):
361 cubic = CubicCongestionControl(1440)
362 cubic._W_max = 25000 # set the target number of bytes to 25000
363 cubic._cwnd_epoch = 2880 # a cwnd of 1440 bytes when we had congestion
364 cubic._starting_conges2ion_avoidance = False
365 cubic._first_slow_start = False
366 cubic.ssthresh = 2880
367 cubic._t_epoch = 5
368
369 cubic._W_est = 0
370
371 # calculate K
372 W_max_segments = cubic._W_max / cubic._max_datagram_size
373 cwnd_epoch_segments = cubic._cwnd_epoch / cubic._max_datagram_size
374 cubic.K = better_cube_root((W_max_segments - cwnd_epoch_segments) / K_CUBIC_C)
375
376 packet = QuicSentPacket(
377 epoch=tls.Epoch.ONE_RTT,
378 in_flight=True,
379 is_ack_eliciting=True,
380 is_crypto_packet=False,
381 packet_number=0,
382 packet_type=QuicPacketType.ONE_RTT,
383 sent_bytes=1280,
384 sent_time=0.0,
385 )
386
387 previous_cwnd = cubic.congestion_window
388
389 cubic.on_packet_acked(now=6, packet=packet)
390
391 # elapsed time + basic rtt
392 target = cubic.W_cubic(1 + 0.02)
393
394 expected = int(
395 previous_cwnd
396 + ((target - previous_cwnd) * (cubic._max_datagram_size / previous_cwnd))
397 )
398
399 self.assertAlmostEqual(cubic.congestion_window, expected)
400
401 def test_increasing_rtt(self):
402 cubic = CubicCongestionControl(1440)
403
404 # get some low rtt
405 for i in range(10):
406 cubic.on_rtt_measurement(now=i + 1, rtt=1)
407
408 # rtt increase (because of congestion for example)
409 for i in range(10):
410 cubic.on_rtt_measurement(now=100 + i, rtt=1000)
411
412 self.assertEqual(cubic.ssthresh, cubic.congestion_window)
413
414 def test_increasing_rtt_exiting_slow_start(self):
415 packet = QuicSentPacket(
416 epoch=tls.Epoch.ONE_RTT,
417 in_flight=True,
418 is_ack_eliciting=True,
419 is_crypto_packet=False,
420 packet_number=0,
421 packet_type=QuicPacketType.ONE_RTT,
422 sent_bytes=1280,
423 sent_time=200.0,
424 )
425
426 cubic = CubicCongestionControl(1440)
427
428 # get some low rtt
429 for i in range(10):
430 cubic.on_rtt_measurement(now=i + 1, rtt=1)
431
432 # rtt increase (because of congestion for example)
433 for i in range(10):
434 cubic.on_rtt_measurement(now=100 + i, rtt=1000)
435
436 previous_cwnd = cubic.congestion_window
437
438 self.assertFalse(cubic._starting_congestion_avoidance)
439
440 cubic.on_packet_acked(packet=packet, now=220)
441
442 self.assertFalse(cubic._first_slow_start)
443 self.assertEqual(cubic._W_max, previous_cwnd)
444 self.assertEqual(cubic._t_epoch, 220)
445 self.assertEqual(cubic._cwnd_epoch, previous_cwnd)
446 self.assertEqual(
447 cubic._W_est,
448 previous_cwnd
449 + cubic.additive_increase_factor * (packet.sent_bytes / previous_cwnd),
450 )
451
452 # calculate K
453 W_max_segments = previous_cwnd / cubic._max_datagram_size
454 cwnd_epoch_segments = previous_cwnd / cubic._max_datagram_size
455 K = better_cube_root((W_max_segments - cwnd_epoch_segments) / K_CUBIC_C)
456
457 self.assertEqual(cubic.K, K)
458
459 def test_packet_lost(self):
460 packet = QuicSentPacket(
461 epoch=tls.Epoch.ONE_RTT,
462 in_flight=True,
463 is_ack_eliciting=True,
464 is_crypto_packet=False,
465 packet_number=0,
466 packet_type=QuicPacketType.ONE_RTT,
467 sent_bytes=1280,
468 sent_time=200.0,
469 )
470
471 packet2 = QuicSentPacket(
472 epoch=tls.Epoch.ONE_RTT,
473 in_flight=True,
474 is_ack_eliciting=True,
475 is_crypto_packet=False,
476 packet_number=0,
477 packet_type=QuicPacketType.ONE_RTT,
478 sent_bytes=1280,
479 sent_time=240.0,
480 )
481
482 cubic = CubicCongestionControl(1440)
483
484 previous_cwnd = cubic.congestion_window
485
486 cubic.on_packets_lost(now=210, packets=[packet])
487
488 self.assertEqual(cubic._congestion_recovery_start_time, 210)
489
490 self.assertEqual(cubic._W_max, previous_cwnd)
491 self.assertEqual(cubic.ssthresh, K_MINIMUM_WINDOW * cubic._max_datagram_size)
492 self.assertEqual(
493 cubic.congestion_window, K_MINIMUM_WINDOW * cubic._max_datagram_size
494 )
495 self.assertTrue(cubic._starting_congestion_avoidance)
496
497 previous_cwnd = cubic.congestion_window
498 W_max = cubic._W_max
499
500 cubic.on_packet_acked(now=250, packet=packet)
501
502 self.assertFalse(cubic._starting_congestion_avoidance)
503 self.assertFalse(cubic._first_slow_start)
504 self.assertEqual(cubic._t_epoch, 250)
505 self.assertEqual(cubic._cwnd_epoch, previous_cwnd)
506 self.assertEqual(
507 cubic._W_est,
508 previous_cwnd
509 + cubic.additive_increase_factor * (packet2.sent_bytes / previous_cwnd),
510 )
511 # calculate K
512 W_max_segments = W_max / cubic._max_datagram_size
513 cwnd_epoch_segments = previous_cwnd / cubic._max_datagram_size
514 K = better_cube_root((W_max_segments - cwnd_epoch_segments) / K_CUBIC_C)
515
516 self.assertEqual(cubic.K, K)
517
518 def test_lost_with_W_max(self):
519 packet = QuicSentPacket(
520 epoch=tls.Epoch.ONE_RTT,
521 in_flight=True,
522 is_ack_eliciting=True,
523 is_crypto_packet=False,
524 packet_number=0,
525 packet_type=QuicPacketType.ONE_RTT,
526 sent_bytes=1280,
527 sent_time=200.0,
528 )
529
530 cubic = CubicCongestionControl(1440)
531
532 cubic._W_max = 100000
533
534 previous_cwnd = cubic.congestion_window
535
536 cubic.on_packets_lost(now=210, packets=[packet])
537
538 # test when W_max was much more than cwnd
539 # and a loss occur
540 self.assertEqual(
541 cubic._W_max, previous_cwnd * (1 + K_CUBIC_LOSS_REDUCTION_FACTOR) / 2
542 )
543
544 def test_cwnd_target(self):
545 cubic = CubicCongestionControl(1440)
546 cubic._W_max = 25000 # set the target number of bytes to 25000
547 cubic._cwnd_epoch = 2880 # a cwnd of 1440 bytes when we had congestion
548 cubic._starting_conges2ion_avoidance = False
549 cubic._first_slow_start = False
550 cubic.ssthresh = 2880
551 cubic._t_epoch = 5
552 cubic.congestion_window = 100000
553
554 cubic._W_est = 0
555
556 # calculate K
557 W_max_segments = cubic._W_max / cubic._max_datagram_size
558 cwnd_epoch_segments = cubic._cwnd_epoch / cubic._max_datagram_size
559 cubic.K = better_cube_root((W_max_segments - cwnd_epoch_segments) / K_CUBIC_C)
560
561 packet = QuicSentPacket(
562 epoch=tls.Epoch.ONE_RTT,
563 in_flight=True,
564 is_ack_eliciting=True,
565 is_crypto_packet=False,
566 packet_number=0,
567 packet_type=QuicPacketType.ONE_RTT,
568 sent_bytes=1280,
569 sent_time=0.0,
570 )
571
572 previous_cwnd = cubic.congestion_window
573
574 cubic.on_packet_acked(now=6, packet=packet)
575
576 # elapsed time + basic rtt
577 target = previous_cwnd
578
579 expected = int(
580 previous_cwnd
581 + ((target - previous_cwnd) * (cubic._max_datagram_size / previous_cwnd))
582 )
583
584 self.assertAlmostEqual(cubic.congestion_window, expected)