File: cubic_test.go

package info (click to toggle)
golang-github-lucas-clemente-quic-go 0.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,312 kB
  • sloc: sh: 54; makefile: 7
file content (205 lines) | stat: -rw-r--r-- 8,066 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package congestion

import (
	"math"
	"testing"
	"time"

	"github.com/quic-go/quic-go/internal/protocol"
	"github.com/stretchr/testify/require"
)

const (
	numConnections         uint32  = 2
	nConnectionBeta        float32 = (float32(numConnections) - 1 + beta) / float32(numConnections)
	nConnectionBetaLastMax float32 = (float32(numConnections) - 1 + betaLastMax) / float32(numConnections)
	nConnectionAlpha       float32 = 3 * float32(numConnections) * float32(numConnections) * (1 - nConnectionBeta) / (1 + nConnectionBeta)
	maxCubicTimeInterval           = 30 * time.Millisecond
)

func renoCwnd(currentCwnd protocol.ByteCount) protocol.ByteCount {
	return currentCwnd + protocol.ByteCount(float32(maxDatagramSize)*nConnectionAlpha*float32(maxDatagramSize)/float32(currentCwnd))
}

func cubicConvexCwnd(initialCwnd protocol.ByteCount, rtt, elapsedTime time.Duration) protocol.ByteCount {
	offset := protocol.ByteCount((elapsedTime+rtt)/time.Microsecond) << 10 / 1000000
	deltaCongestionWindow := 410 * offset * offset * offset * maxDatagramSize >> 40
	return initialCwnd + deltaCongestionWindow
}

func TestCubicAboveOriginWithTighterBounds(t *testing.T) {
	clock := mockClock{}
	cubic := NewCubic(&clock)
	cubic.SetNumConnections(int(numConnections))

	// Convex growth.
	const rttMin = 100 * time.Millisecond
	const rttMinS = float32(rttMin/time.Millisecond) / 1000.0
	currentCwnd := 10 * maxDatagramSize
	initialCwnd := currentCwnd

	clock.Advance(time.Millisecond)
	initialTime := clock.Now()
	expectedFirstCwnd := renoCwnd(currentCwnd)
	currentCwnd = cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, initialTime)
	require.Equal(t, expectedFirstCwnd, currentCwnd)

	// Normal TCP phase.
	// The maximum number of expected reno RTTs can be calculated by
	// finding the point where the cubic curve and the reno curve meet.
	maxRenoRtts := int(math.Sqrt(float64(nConnectionAlpha/(0.4*rttMinS*rttMinS*rttMinS))) - 2)
	for range maxRenoRtts {
		numAcksThisEpoch := int(float32(currentCwnd/maxDatagramSize) / nConnectionAlpha)

		initialCwndThisEpoch := currentCwnd
		for range numAcksThisEpoch {
			// Call once per ACK.
			expectedNextCwnd := renoCwnd(currentCwnd)
			currentCwnd = cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now())
			require.Equal(t, expectedNextCwnd, currentCwnd)
		}
		cwndChangeThisEpoch := currentCwnd - initialCwndThisEpoch
		require.InDelta(t, float64(maxDatagramSize), float64(cwndChangeThisEpoch), float64(maxDatagramSize)/2)
		clock.Advance(100 * time.Millisecond)
	}

	for range 54 {
		maxAcksThisEpoch := currentCwnd / maxDatagramSize
		interval := time.Duration(100*1000/maxAcksThisEpoch) * time.Microsecond
		for range int(maxAcksThisEpoch) {
			clock.Advance(interval)
			currentCwnd = cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now())
			expectedCwnd := cubicConvexCwnd(initialCwnd, rttMin, clock.Now().Sub(initialTime))
			require.Equal(t, expectedCwnd, currentCwnd)
		}
	}
	expectedCwnd := cubicConvexCwnd(initialCwnd, rttMin, clock.Now().Sub(initialTime))
	currentCwnd = cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now())
	require.Equal(t, expectedCwnd, currentCwnd)
}

func TestCubicAboveOriginWithFineGrainedCubing(t *testing.T) {
	clock := mockClock{}
	cubic := NewCubic(&clock)
	cubic.SetNumConnections(int(numConnections))

	currentCwnd := 1000 * maxDatagramSize
	initialCwnd := currentCwnd
	rttMin := 100 * time.Millisecond
	clock.Advance(time.Millisecond)
	initialTime := clock.Now()

	currentCwnd = cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now())
	clock.Advance(600 * time.Millisecond)
	currentCwnd = cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now())

	for i := 0; i < 100; i++ {
		clock.Advance(10 * time.Millisecond)
		expectedCwnd := cubicConvexCwnd(initialCwnd, rttMin, clock.Now().Sub(initialTime))
		nextCwnd := cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now())
		require.Equal(t, expectedCwnd, nextCwnd)
		require.Greater(t, nextCwnd, currentCwnd)
		cwndDelta := nextCwnd - currentCwnd
		require.Less(t, cwndDelta, maxDatagramSize/10)
		currentCwnd = nextCwnd
	}
}

func TestCubicHandlesPerAckUpdates(t *testing.T) {
	clock := mockClock{}
	cubic := NewCubic(&clock)
	cubic.SetNumConnections(int(numConnections))

	initialCwndPackets := 150
	currentCwnd := protocol.ByteCount(initialCwndPackets) * maxDatagramSize
	rttMin := 350 * time.Millisecond

	clock.Advance(time.Millisecond)
	rCwnd := renoCwnd(currentCwnd)
	currentCwnd = cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now())
	initialCwnd := currentCwnd

	maxAcks := int(float32(initialCwndPackets) / nConnectionAlpha)
	interval := maxCubicTimeInterval / time.Duration(maxAcks+1)

	clock.Advance(interval)
	rCwnd = renoCwnd(rCwnd)
	require.Equal(t, currentCwnd, cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now()))

	for range maxAcks - 1 {
		clock.Advance(interval)
		nextCwnd := cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now())
		rCwnd = renoCwnd(rCwnd)
		require.Greater(t, nextCwnd, currentCwnd)
		require.Equal(t, rCwnd, nextCwnd)
		currentCwnd = nextCwnd
	}

	minimumExpectedIncrease := maxDatagramSize * 9 / 10
	require.Greater(t, currentCwnd, initialCwnd+minimumExpectedIncrease)
}

func TestCubicHandlesLossEvents(t *testing.T) {
	clock := mockClock{}
	cubic := NewCubic(&clock)
	cubic.SetNumConnections(int(numConnections))

	rttMin := 100 * time.Millisecond
	currentCwnd := 422 * maxDatagramSize
	expectedCwnd := renoCwnd(currentCwnd)

	clock.Advance(time.Millisecond)
	require.Equal(t, expectedCwnd, cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now()))

	preLossCwnd := currentCwnd
	require.Zero(t, cubic.lastMaxCongestionWindow)
	expectedCwnd = protocol.ByteCount(float32(currentCwnd) * nConnectionBeta)
	require.Equal(t, expectedCwnd, cubic.CongestionWindowAfterPacketLoss(currentCwnd))
	require.Equal(t, preLossCwnd, cubic.lastMaxCongestionWindow)
	currentCwnd = expectedCwnd

	preLossCwnd = currentCwnd
	expectedCwnd = protocol.ByteCount(float32(currentCwnd) * nConnectionBeta)
	require.Equal(t, expectedCwnd, cubic.CongestionWindowAfterPacketLoss(currentCwnd))
	currentCwnd = expectedCwnd
	require.Greater(t, preLossCwnd, cubic.lastMaxCongestionWindow)
	expectedLastMax := protocol.ByteCount(float32(preLossCwnd) * nConnectionBetaLastMax)
	require.Equal(t, expectedLastMax, cubic.lastMaxCongestionWindow)
	require.Less(t, expectedCwnd, cubic.lastMaxCongestionWindow)

	currentCwnd = cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now())
	require.Greater(t, cubic.lastMaxCongestionWindow, currentCwnd)

	currentCwnd = cubic.lastMaxCongestionWindow - 1
	preLossCwnd = currentCwnd
	expectedCwnd = protocol.ByteCount(float32(currentCwnd) * nConnectionBeta)
	require.Equal(t, expectedCwnd, cubic.CongestionWindowAfterPacketLoss(currentCwnd))
	expectedLastMax = preLossCwnd
	require.Equal(t, expectedLastMax, cubic.lastMaxCongestionWindow)
}

func TestCubicBelowOrigin(t *testing.T) {
	clock := mockClock{}
	cubic := NewCubic(&clock)
	cubic.SetNumConnections(int(numConnections))

	rttMin := 100 * time.Millisecond
	currentCwnd := 422 * maxDatagramSize
	expectedCwnd := renoCwnd(currentCwnd)

	clock.Advance(time.Millisecond)
	require.Equal(t, expectedCwnd, cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now()))

	expectedCwnd = protocol.ByteCount(float32(currentCwnd) * nConnectionBeta)
	require.Equal(t, expectedCwnd, cubic.CongestionWindowAfterPacketLoss(currentCwnd))
	currentCwnd = expectedCwnd

	currentCwnd = cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now())

	for range 40 {
		clock.Advance(100 * time.Millisecond)
		currentCwnd = cubic.CongestionWindowAfterAck(maxDatagramSize, currentCwnd, rttMin, clock.Now())
	}
	expectedCwnd = 553632 * maxDatagramSize / 1460
	require.Equal(t, expectedCwnd, currentCwnd)
}