Repository navigation
Expand file tree
/
Copy pathsoak_test.go
More file actions
348 lines (266 loc) · 8.54 KB
/
Copy pathsoak_test.go
File metadata and controls
348 lines (266 loc) · 8.54 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
336
337
338
339
340
341
342
343
344
345
346
347
348
package http2
import (
"crypto/tls"
"net"
"runtime"
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/valyala/fasthttp"
)
// Everything else in this package finishes in seconds and then exits, which
// hides anything that accumulates. These tests hold connections open across
// several ping intervals and cycle connections in waves, then check that
// goroutines and heap come back to where they started.
// settleTo waits for the goroutine count to come down to target and returns
// what it reached. Teardown is staggered across both ends of every connection
// plus fasthttp's worker pool, so a count that is merely stable for a moment is
// not the same as a count that has finished falling.
func settleTo(t *testing.T, target int, timeout time.Duration) int {
t.Helper()
deadline := time.Now().Add(timeout)
for {
runtime.GC()
n := runtime.NumGoroutine()
if n <= target || time.Now().After(deadline) {
return n
}
time.Sleep(100 * time.Millisecond)
}
}
// TestSoakConnectionChurn opens and closes connections in waves. Every
// connection starts goroutines on both ends, so a connection that does not
// fully tear down shows up as goroutines that never come back.
func TestSoakConnectionChurn(t *testing.T) {
if testing.Short() {
t.Skip("soak test skipped in short mode")
}
const (
waves = 8
connsPerWave = 50
requestsPerConn = 5
)
addr, _ := newAttackServer(t, ServerConfig{PingInterval: 100 * time.Millisecond})
// Earlier tests in this binary may still be winding down, so wait for the
// count to come down before taking it as the baseline.
baseline := settleTo(t, 20, 30*time.Second)
for w := 0; w < waves; w++ {
clients := make([]*fasthttp.HostClient, 0, connsPerWave)
for i := 0; i < connsPerWave; i++ {
hc := &fasthttp.HostClient{
Addr: addr,
IsTLS: true,
TLSConfig: &tls.Config{InsecureSkipVerify: true},
}
if err := ConfigureClient(hc, ClientOpts{
PingInterval: 100 * time.Millisecond,
MaxResponseTime: 10 * time.Second,
}); err != nil {
t.Fatalf("wave %d conn %d: %v", w, i, err)
}
clients = append(clients, hc)
}
for i, hc := range clients {
for r := 0; r < requestsPerConn; r++ {
req := fasthttp.AcquireRequest()
res := fasthttp.AcquireResponse()
req.SetRequestURI("https://" + addr + "/")
req.Header.SetMethod(fasthttp.MethodGet)
if err := hc.Do(req, res); err != nil {
t.Fatalf("wave %d conn %d request %d: %v", w, i, r, err)
}
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(res)
}
}
for _, hc := range clients {
// HostClient.CloseIdleConnections does not reach HTTP/2
// connections, so go through the library's own client.
if err := ClientFrom(hc).Close(); err != nil {
t.Errorf("wave %d: closing: %v", w, err)
}
}
}
// Some slack for fasthttp's worker pool, which shrinks on its own schedule.
// What matters is that nothing scales with the number of connections.
const tolerance = 20
conns := waves * connsPerWave
after := settleTo(t, baseline+tolerance, 60*time.Second)
t.Logf("goroutines: %d before, %d after %d connections", baseline, after, conns)
if after > baseline+tolerance {
t.Errorf("goroutines went from %d to %d over %d connections, want them released",
baseline, after, conns)
}
}
// TestSoakLongLivedConnections holds connections open across many ping
// intervals with steady traffic. Anything retained per request or per ping
// shows up as heap that keeps climbing.
func TestSoakLongLivedConnections(t *testing.T) {
if testing.Short() {
t.Skip("soak test skipped in short mode")
}
const (
conns = 25
rounds = 40
perRound = 20
)
addr, _ := newAttackServer(t, ServerConfig{PingInterval: 50 * time.Millisecond})
clients := make([]*fasthttp.HostClient, 0, conns)
for i := 0; i < conns; i++ {
hc := &fasthttp.HostClient{
Addr: addr,
IsTLS: true,
TLSConfig: &tls.Config{InsecureSkipVerify: true},
}
if err := ConfigureClient(hc, ClientOpts{
PingInterval: 50 * time.Millisecond,
MaxResponseTime: 10 * time.Second,
}); err != nil {
t.Fatalf("conn %d: %v", i, err)
}
clients = append(clients, hc)
}
t.Cleanup(func() {
for _, hc := range clients {
_ = ClientFrom(hc).Close()
}
})
traffic := func() {
for _, hc := range clients {
for r := 0; r < perRound; r++ {
req := fasthttp.AcquireRequest()
res := fasthttp.AcquireResponse()
req.SetRequestURI("https://" + addr + "/")
req.Header.SetMethod(fasthttp.MethodGet)
if err := hc.Do(req, res); err != nil {
t.Errorf("request: %v", err)
}
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(res)
}
}
}
// Warm up first: pools and buffers fill on the first pass, and that growth
// is not a leak.
traffic()
var early, late uint64
grew := heapDelta(func() {
for r := 0; r < rounds; r++ {
traffic()
// Give the ping timers room to fire between rounds.
time.Sleep(20 * time.Millisecond)
if r == rounds/4 {
early = heapInUse()
}
}
late = heapInUse()
})
requests := rounds * conns * perRound
t.Logf("%d requests over %d long-lived connections: heap %.1f -> %.1f MiB, delta %.1f MiB (%.1f bytes per request)",
requests, conns, float64(early)/(1<<20), float64(late)/(1<<20),
float64(grew)/(1<<20), float64(grew)/float64(requests))
if perRequest := float64(grew) / float64(requests); perRequest > 16 {
t.Errorf("heap grows %.1f bytes per request on a held-open connection", perRequest)
}
}
// TestSoakUploads pushes bodies through the flow-controlled send path for long
// enough that the connection has to be granted its window back many times over.
// A request whose body is held back leaves state on both ends, so anything that
// is not cleaned up when the body finishes shows up here as heap that climbs
// and as streams the connection never lets go of.
func TestSoakUploads(t *testing.T) {
if testing.Short() {
t.Skip("soak test skipped in short mode")
}
const (
rounds = 200
body = 64 << 10
)
certPEM, keyPEM := testKeyPair(t)
server := &fasthttp.Server{
Handler: func(ctx *fasthttp.RequestCtx) {
ctx.SetBodyString(strconv.Itoa(len(ctx.Request.Body())))
},
MaxRequestBodySize: 1 << 20,
Logger: discardLogger{},
}
ConfigureServer(server, ServerConfig{PingInterval: -1})
ln, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = ln.Close() })
go func() { _ = server.ServeTLSEmbed(ln, certPEM, keyPEM) }()
addr := ln.Addr().String()
hc := &fasthttp.HostClient{
Addr: addr,
IsTLS: true,
TLSConfig: &tls.Config{InsecureSkipVerify: true},
}
if err := ConfigureClient(hc, ClientOpts{
PingInterval: -1,
MaxResponseTime: 20 * time.Second,
}); err != nil {
t.Fatal(err)
}
cl := ClientFrom(hc)
t.Cleanup(func() { _ = cl.Close() })
payload := make([]byte, body)
upload := func(n int) {
t.Helper()
for i := 0; i < n; i++ {
req := fasthttp.AcquireRequest()
res := fasthttp.AcquireResponse()
req.SetRequestURI("https://" + addr + "/")
req.Header.SetMethod(fasthttp.MethodPost)
req.SetBody(payload)
if err := hc.Do(req, res); err != nil {
t.Fatalf("upload %d: %v", i, err)
}
if got := string(res.Body()); got != strconv.Itoa(body) {
t.Fatalf("upload %d: server saw %s bytes, want %d", i, got, body)
}
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(res)
}
}
upload(20)
grew := heapDelta(func() { upload(rounds) })
sent := rounds * body
t.Logf("%d uploads of %d bytes (%.1f MiB in total): heap delta %.1f MiB",
rounds, body, float64(sent)/(1<<20), float64(grew)/(1<<20))
if perUpload := float64(grew) / float64(rounds); perUpload > float64(body)/4 {
t.Errorf("heap grows %.0f bytes per upload of %d", perUpload, body)
}
// Everything the connection was tracking for those bodies should be gone.
cl.lck.Lock()
conns := make([]*Conn, 0, cl.conns.Len())
for e := cl.conns.Front(); e != nil; e = e.Next() {
conns = append(conns, e.Value.(*Conn))
}
cl.lck.Unlock()
for _, c := range conns {
c.sendLck.Lock()
pending := len(c.pending)
c.sendLck.Unlock()
if pending != 0 {
t.Errorf("connection still holds %d unsent request bodies", pending)
}
c.reqLck.Lock()
queued := len(c.reqQueued)
c.reqLck.Unlock()
if queued != 0 {
t.Errorf("connection still holds %d finished requests", queued)
}
if n := atomic.LoadInt32(&c.openStreams); n != 0 {
t.Errorf("connection still counts %d open streams", n)
}
}
}
func heapInUse() uint64 {
var m runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&m)
return m.HeapAlloc
}