-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathalloc_test.go
More file actions
104 lines (77 loc) · 2.57 KB
/
Copy pathalloc_test.go
File metadata and controls
104 lines (77 loc) · 2.57 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
// The race detector allocates its own bookkeeping on top of everything this
// measures, so the counts here only mean anything without it.
//go:build !race
package http2
import (
"crypto/tls"
"net"
"runtime"
"testing"
"github.com/valyala/fasthttp"
)
// TestAllocsPerRequest puts a ceiling on the work a request does that the GC
// has to clean up afterwards. Both ends run in this process, so the count
// covers the client, the server and the TLS layer between them.
//
// This started as a regression test for two bugs that never showed up as a
// failure anywhere else: the server dropped every stream frame it read on the
// floor instead of returning it to the pool, and the client allocated a
// context, a channel and a timer for every request.
func TestAllocsPerRequest(t *testing.T) {
certPEM, keyPEM := testKeyPair(t)
server := &fasthttp.Server{
Handler: func(ctx *fasthttp.RequestCtx) {
ctx.SetContentType("text/plain")
ctx.SetBodyString("hello")
},
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}); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = ClientFrom(hc).Close() })
req := fasthttp.AcquireRequest()
res := fasthttp.AcquireResponse()
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(res)
req.SetRequestURI("https://" + addr + "/")
do := func(n int) {
t.Helper()
for i := 0; i < n; i++ {
if err := hc.Do(req, res); err != nil {
t.Fatalf("request %d: %v", i, err)
}
}
}
// Warm every pool on both sides first, so the count is the steady state
// rather than the cost of starting up.
do(2000)
const n = 20000
var before, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&before)
do(n)
runtime.ReadMemStats(&after)
perRequest := float64(after.Mallocs-before.Mallocs) / n
// Around 2.5 with everything pooled. The ceiling leaves room for the
// runtime and the TLS layer to move without failing the build, while still
// catching anything that starts allocating per request or per frame.
const ceiling = 10
t.Logf("%.2f allocations per request", perRequest)
if perRequest > ceiling {
t.Errorf("%.2f allocations per request, want at most %d", perRequest, ceiling)
}
}