-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathallocation_test.go
More file actions
138 lines (110 loc) · 2.91 KB
/
allocation_test.go
File metadata and controls
138 lines (110 loc) · 2.91 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
//go:build !race
package fasthttp
import (
"net"
"testing"
)
func TestAllocationServeConn(t *testing.T) {
s := &Server{
Handler: func(ctx *RequestCtx) {
ctx.SetStatusCode(StatusOK)
ctx.SetBodyString("Hello, World!")
ctx.Response.Header.Set("Content-Type", "text/plain; charset=utf-8")
},
}
rw := &readWriter{}
// Make space for the request and response here so it
// doesn't allocate within the test.
rw.r.Grow(1024)
rw.w.Grow(1024)
n := testing.AllocsPerRun(100, func() {
rw.r.WriteString("GET / HTTP/1.1\r\nHost: google.com\r\nCookie: foo=bar\r\n\r\n")
if err := s.ServeConn(rw); err != nil {
t.Fatal(err)
}
// Reset the write buffer to make space for the next response.
rw.w.Reset()
})
if n != 0 {
t.Fatalf("expected 0 allocations, got %f", n)
}
}
func TestAllocationClient(t *testing.T) {
ln, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
t.Fatalf("cannot listen: %v", err)
}
defer ln.Close()
s := &Server{
Handler: func(ctx *RequestCtx) {
ctx.SetStatusCode(StatusOK)
ctx.SetBodyString("Hello, World!")
ctx.Response.Header.Set("Content-Type", "text/plain; charset=utf-8")
},
}
go s.Serve(ln) //nolint:errcheck
c := &Client{}
url := "http://test:test@" + ln.Addr().String() + "/foo?bar=baz"
n := testing.AllocsPerRun(100, func() {
req := AcquireRequest()
res := AcquireResponse()
req.SetRequestURI(url)
req.Header.Add("Foo", "bar")
if err := c.Do(req, res); err != nil {
t.Fatal(err)
}
ReleaseRequest(req)
ReleaseResponse(res)
})
if n != 0 {
t.Fatalf("expected 0 allocations, got %f", n)
}
}
func TestAllocationURI(t *testing.T) {
uri := []byte("http://username:password@hello.%e4%b8%96%e7%95%8c.com/some/path?foo=bar#test")
n := testing.AllocsPerRun(100, func() {
u := AcquireURI()
u.Parse(nil, uri) //nolint:errcheck
ReleaseURI(u)
})
if n != 0 {
t.Fatalf("expected 0 allocations, got %f", n)
}
}
func TestAllocationFS(t *testing.T) {
// Create a simple test filesystem handler
fs := &FS{
Root: ".",
GenerateIndexPages: false,
Compress: false,
AcceptByteRange: false,
}
h := fs.NewRequestHandler()
ctx := &RequestCtx{}
n := testing.AllocsPerRun(100, func() {
ctx.Request.Reset()
ctx.Response.Reset()
ctx.Request.SetRequestURI("/allocation_test.go")
ctx.Request.Header.Set("Host", "localhost")
h(ctx)
})
t.Logf("FS operations allocate %f times per request", n)
if n != 0 {
t.Fatalf("expected 0 allocations, got %f", n)
}
}
func TestAllocationsHeaderScanner(t *testing.T) {
body := []byte("Host: a.com\r\nCookie: foo=bar\r\nWithTabs: \t v1 \t\r\nWithTabs-Start: \t \t v1 \r\nWithTabs-End: v1 \t \t\t\t\r\nWithTabs-Multi-Line: \t v1 \t;\r\n \t v2 \t;\r\n\t v3\r\n\r\n")
n := testing.AllocsPerRun(100, func() {
var s headerScanner
s.b = body
for s.next() {
}
if s.err != nil {
t.Fatal(s.err)
}
})
if n != 0 {
t.Fatalf("expected 0 allocations, got %f", n)
}
}