-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_test.go
More file actions
335 lines (328 loc) · 10.6 KB
/
Copy pathperformance_test.go
File metadata and controls
335 lines (328 loc) · 10.6 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package mipstack
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net"
"net/netip"
"testing"
"time"
)
func benchmarkTCPControllerConnection(b *testing.B, algorithm CongestionControl) (net.Conn, *Stack, *stackBridge) {
b.Helper()
clientAddress := netip.MustParseAddr("192.0.2.201")
serverAddress := netip.MustParseAddr("192.0.2.202")
client, err := New(Config{
LocalAddresses: []netip.Prefix{netip.PrefixFrom(clientAddress, 32)},
TCP: TCPSocketDefaults{CongestionControl: algorithm},
})
if err != nil {
b.Fatal(err)
}
server, err := New(Config{
LocalAddresses: []netip.Prefix{netip.PrefixFrom(serverAddress, 32)},
TCP: TCPSocketDefaults{CongestionControl: algorithm},
})
if err != nil {
b.Fatal(err)
}
if err = client.Start(); err != nil {
b.Fatal(err)
}
if err = server.Start(); err != nil {
b.Fatal(err)
}
// Benchmarks use the same packet pump as tests and own its lifetime here.
bridge := &stackBridge{client: client, peer: server, done: make(chan struct{}, 2)}
go bridge.run(client, server, true)
go bridge.run(server, client, false)
listener, err := server.ListenTCP(context.Background(), "tcp4", netip.AddrPortFrom(serverAddress, 0))
if err != nil {
b.Fatal(err)
}
accepted := make(chan net.Conn, 1)
go func() {
connection, acceptErr := listener.Accept()
if acceptErr != nil {
accepted <- nil
return
}
accepted <- connection
_, _ = io.Copy(connection, connection)
_ = connection.Close()
}()
connection, err := client.DialTCP(context.Background(), "tcp4", netip.AddrPort{}, netip.AddrPortFrom(serverAddress, listener.Addr().(*net.TCPAddr).AddrPort().Port()))
if err != nil {
b.Fatal(err)
}
serverConnection := <-accepted
if serverConnection == nil {
b.Fatal("benchmark accept failed")
}
b.Cleanup(func() {
_ = connection.Close()
_ = serverConnection.Close()
_ = listener.Close()
_ = client.Close()
_ = server.Close()
<-bridge.done
<-bridge.done
})
return connection, server, bridge
}
func BenchmarkTCPControllerThroughput(b *testing.B) {
const size = 4 * 1024 * 1024
for _, algorithm := range []CongestionControl{CongestionControlReno, CongestionControlCUBIC, CongestionControlBBR, CongestionControlBBR3} {
b.Run(string(algorithm), func(b *testing.B) {
connection, peer, bridge := benchmarkTCPControllerConnection(b, algorithm)
payload := bytes.Repeat([]byte{0x5a}, size)
received := make([]byte, size)
b.SetBytes(2 * size)
b.ResetTimer()
for iteration := 0; iteration < b.N; iteration++ {
writeDone := make(chan error, 1)
go func() {
_, writeErr := connection.Write(payload)
writeDone <- writeErr
}()
if _, err := io.ReadFull(connection, received); err != nil {
b.Fatal(err)
}
if err := <-writeDone; err != nil {
b.Fatal(err)
}
if !bytes.Equal(received, payload) {
b.Fatal("echo payload mismatch")
}
}
if tcp, ok := connection.(*TCPConn); ok {
info := tcp.Info()
stats := tcp.stack.Stats()
peerStats := peer.Stats()
b.ReportMetric(float64(info.CongestionWindow), "cwnd-B")
b.ReportMetric(float64(info.DeliveryRate), "delivery-B/s")
b.ReportMetric(float64(info.BytesInFlight), "flight-B")
b.ReportMetric(float64(info.PacingRate), "pacing-B/s")
b.ReportMetric(float64(info.SchedulerLimitedEvents), "scheduler-limited-events")
b.ReportMetric(float64(info.SlowStartThreshold), "ssthresh-B")
b.ReportMetric(float64(info.RTT)/float64(time.Microsecond), "rtt-us")
b.ReportMetric(float64(info.Retransmissions), "retransmissions")
b.ReportMetric(float64(info.InboundQueueDrops), "connection-queue-drops")
b.ReportMetric(float64(stats.InboundDroppedPackets), "rx-drops")
b.ReportMetric(float64(stats.TCPInboundQueueDrops), "queue-drops")
b.ReportMetric(float64(peerStats.InboundDroppedPackets), "peer-rx-drops")
b.ReportMetric(float64(peerStats.TCPInboundQueueDrops), "peer-queue-drops")
b.ReportMetric(float64(stats.TCPSACKRetransmissions), "sack-retransmissions")
b.ReportMetric(float64(stats.TCPRACKRetransmissions), "rack-retransmissions")
b.ReportMetric(float64(stats.TCPTailLossProbes), "tail-loss-probes")
b.ReportMetric(float64(info.SendBufferCapacity), "send-buffer-B")
b.ReportMetric(float64(info.InboundQueuePeak), "inbound-queue-peak-B")
bridge.mu.Lock()
b.ReportMetric(float64(bridge.clientGaps), "wire-gaps")
b.ReportMetric(float64(bridge.clientRepeats), "wire-repeats")
b.ReportMetric(float64(bridge.peerSACKs), "peer-sack-acks")
b.ReportMetric(float64(bridge.peerDSACKs), "peer-dsack-acks")
bridge.mu.Unlock()
}
})
}
}
func BenchmarkTCPControllerLatency(b *testing.B) {
for _, algorithm := range []CongestionControl{CongestionControlReno, CongestionControlCUBIC, CongestionControlBBR, CongestionControlBBR3} {
b.Run(string(algorithm), func(b *testing.B) {
connection, _, _ := benchmarkTCPControllerConnection(b, algorithm)
_ = connection.SetDeadline(time.Now().Add(time.Minute))
request := []byte{0x5a}
response := make([]byte, 1)
b.ResetTimer()
for iteration := 0; iteration < b.N; iteration++ {
if _, err := connection.Write(request); err != nil {
b.Fatal(err)
}
if _, err := io.ReadFull(connection, response); err != nil {
b.Fatal(err)
}
}
})
}
}
func benchmarkTCPControllerConnections(b *testing.B, algorithm CongestionControl, count int) []net.Conn {
b.Helper()
clientAddress := netip.MustParseAddr("192.0.2.211")
serverAddress := netip.MustParseAddr("192.0.2.212")
client, err := New(Config{LocalAddresses: []netip.Prefix{netip.PrefixFrom(clientAddress, 32)}, TCP: TCPSocketDefaults{CongestionControl: algorithm}})
if err != nil {
b.Fatal(err)
}
server, err := New(Config{
LocalAddresses: []netip.Prefix{netip.PrefixFrom(serverAddress, 32)},
TCP: TCPSocketDefaults{
CongestionControl: algorithm,
// This benchmark measures established-flow concurrency rather than
// the independently tested accept-queue overload policy.
AcceptQueue: count,
},
})
if err != nil {
b.Fatal(err)
}
if err = client.Start(); err != nil {
b.Fatal(err)
}
if err = server.Start(); err != nil {
b.Fatal(err)
}
bridge := &stackBridge{client: client, peer: server, done: make(chan struct{}, 2)}
go bridge.run(client, server, true)
go bridge.run(server, client, false)
listener, err := server.ListenTCP(context.Background(), "tcp4", netip.AddrPortFrom(serverAddress, 0))
if err != nil {
b.Fatal(err)
}
accepted := make(chan net.Conn, count)
acceptErrors := make(chan error, 1)
go func() {
for index := 0; index < count; index++ {
connection, acceptErr := listener.Accept()
if acceptErr != nil {
acceptErrors <- acceptErr
return
}
accepted <- connection
go func(connection net.Conn) { _, _ = io.Copy(connection, connection) }(connection)
}
acceptErrors <- nil
}()
endpoint := netip.AddrPortFrom(serverAddress, listener.Addr().(*net.TCPAddr).AddrPort().Port())
connections := make([]net.Conn, count)
dialErrors := make(chan error, count)
dialLimit := make(chan struct{}, 32)
for index := range connections {
go func(index int) {
dialLimit <- struct{}{}
connection, dialErr := client.DialTCP(context.Background(), "tcp4", netip.AddrPort{}, endpoint)
<-dialLimit
connections[index] = connection
dialErrors <- dialErr
}(index)
}
for range connections {
if err = <-dialErrors; err != nil {
b.Fatal(err)
}
}
serverConnections := make([]net.Conn, 0, count)
for len(serverConnections) < count {
select {
case connection := <-accepted:
serverConnections = append(serverConnections, connection)
case err = <-acceptErrors:
if err != nil {
b.Fatal(err)
}
}
}
b.Cleanup(func() {
for _, connection := range connections {
_ = connection.Close()
}
for _, connection := range serverConnections {
_ = connection.Close()
}
_ = listener.Close()
_ = client.Close()
_ = server.Close()
<-bridge.done
<-bridge.done
})
return connections
}
func BenchmarkTCPControllerConcurrency(b *testing.B) {
const size = 128 * 1024
for _, algorithm := range []CongestionControl{CongestionControlReno, CongestionControlCUBIC, CongestionControlBBR, CongestionControlBBR3} {
for _, count := range []int{16, 64, 256, 512, 1024, 2048, 4096} {
b.Run(fmt.Sprintf("%s-%d", algorithm, count), func(b *testing.B) {
connections := benchmarkTCPControllerConnections(b, algorithm, count)
payload := bytes.Repeat([]byte{0x6b}, size)
b.SetBytes(int64(2 * size * count))
b.ResetTimer()
for iteration := 0; iteration < b.N; iteration++ {
results := make(chan error, count)
for _, connection := range connections {
go func(connection net.Conn) {
_ = connection.SetDeadline(time.Now().Add(2 * time.Minute))
writeDone := make(chan error, 1)
go func() {
_, writeErr := connection.Write(payload)
writeDone <- writeErr
}()
received := make([]byte, size)
_, readErr := io.ReadFull(connection, received)
if writeErr := <-writeDone; readErr == nil {
readErr = writeErr
}
if readErr == nil && !bytes.Equal(received, payload) {
readErr = errors.New("echo payload mismatch")
}
results <- readErr
}(connection)
}
for range connections {
if err := <-results; err != nil {
b.Fatal(err)
}
}
}
})
}
}
}
func BenchmarkPacketDeviceBatchRead(b *testing.B) {
const payloadSize = 1200
for _, batch := range []int{1, deviceBatchSize} {
b.Run(fmt.Sprintf("batch-%d", batch), func(b *testing.B) {
local := netip.MustParseAddr("192.0.2.221")
remote := netip.MustParseAddrPort("192.0.2.222:9000")
stack, err := New(Config{LocalAddresses: []netip.Prefix{netip.PrefixFrom(local, 32)}})
if err != nil {
b.Fatal(err)
}
if err = stack.Start(); err != nil {
b.Fatal(err)
}
connection, err := stack.DialUDP(context.Background(), "udp4", netip.AddrPort{}, remote)
if err != nil {
b.Fatal(err)
}
b.Cleanup(func() {
_ = connection.Close()
_ = stack.Close()
})
payload := make([]byte, payloadSize)
buffers := make([][]byte, batch)
for index := range buffers {
buffers[index] = make([]byte, 1500)
}
sizes := make([]int, batch)
b.SetBytes(payloadSize * deviceBatchSize)
b.ResetTimer()
for iteration := 0; iteration < b.N; iteration++ {
for packet := 0; packet < deviceBatchSize; packet++ {
if _, err = connection.Write(payload); err != nil {
b.Fatal(err)
}
}
read := 0
for read < deviceBatchSize {
count, readErr := stack.Read(buffers, sizes, 0)
if readErr != nil {
b.Fatal(readErr)
}
read += count
}
}
})
}
}