forked from valyala/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
header_timing_test.go
146 lines (131 loc) · 3.35 KB
/
header_timing_test.go
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
package fasthttp
import (
"bufio"
"bytes"
"io"
"testing"
)
var strFoobar = []byte("foobar.com")
type benchReadBuf struct {
s []byte
n int
}
func (r *benchReadBuf) Read(p []byte) (int, error) {
if r.n == len(r.s) {
return 0, io.EOF
}
n := copy(p, r.s[r.n:])
r.n += n
return n, nil
}
func BenchmarkRequestHeaderRead(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var h RequestHeader
buf := &benchReadBuf{
s: []byte("GET /foo/bar HTTP/1.1\r\nHost: foobar.com\r\nUser-Agent: aaa.bbb\r\nReferer: http://google.com/aaa/bbb\r\n\r\n"),
}
br := bufio.NewReader(buf)
for pb.Next() {
buf.n = 0
br.Reset(buf)
if err := h.Read(br); err != nil {
b.Fatalf("unexpected error when reading header: %s", err)
}
}
})
}
func BenchmarkResponseHeaderRead(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var h ResponseHeader
buf := &benchReadBuf{
s: []byte("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: 1256\r\nServer: aaa 1/2.3\r\nTest: 1.2.3\r\n\r\n"),
}
br := bufio.NewReader(buf)
for pb.Next() {
buf.n = 0
br.Reset(buf)
if err := h.Read(br); err != nil {
b.Fatalf("unexpected error when reading header: %s", err)
}
}
})
}
func BenchmarkRequestHeaderWrite(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var h RequestHeader
h.SetRequestURI("/foo/bar")
h.SetHost("foobar.com")
h.SetUserAgent("aaa.bbb")
h.SetReferer("http://google.com/aaa/bbb")
var w ByteBuffer
for pb.Next() {
if _, err := h.WriteTo(&w); err != nil {
b.Fatalf("unexpected error when writing header: %s", err)
}
w.Reset()
}
})
}
func BenchmarkResponseHeaderWrite(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var h ResponseHeader
h.SetStatusCode(200)
h.SetContentType("text/html")
h.SetContentLength(1256)
h.SetServer("aaa 1/2.3")
h.Set("Test", "1.2.3")
var w ByteBuffer
for pb.Next() {
if _, err := h.WriteTo(&w); err != nil {
b.Fatalf("unexpected error when writing header: %s", err)
}
w.Reset()
}
})
}
func BenchmarkRequestHeaderPeekBytesCanonical(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var h RequestHeader
h.SetBytesV("Host", strFoobar)
for pb.Next() {
v := h.PeekBytes(strHost)
if !bytes.Equal(v, strFoobar) {
b.Fatalf("unexpected result: %q. Expected %q", v, strFoobar)
}
}
})
}
func BenchmarkRequestHeaderPeekBytesNonCanonical(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var h RequestHeader
h.SetBytesV("Host", strFoobar)
hostBytes := []byte("HOST")
for pb.Next() {
v := h.PeekBytes(hostBytes)
if !bytes.Equal(v, strFoobar) {
b.Fatalf("unexpected result: %q. Expected %q", v, strFoobar)
}
}
})
}
func BenchmarkNormalizeHeaderKeyCommonCase(b *testing.B) {
src := []byte("User-Agent-Host-Content-Type-Content-Length-Server")
benchmarkNormalizeHeaderKey(b, src)
}
func BenchmarkNormalizeHeaderKeyLowercase(b *testing.B) {
src := []byte("user-agent-host-content-type-content-length-server")
benchmarkNormalizeHeaderKey(b, src)
}
func BenchmarkNormalizeHeaderKeyUppercase(b *testing.B) {
src := []byte("USER-AGENT-HOST-CONTENT-TYPE-CONTENT-LENGTH-SERVER")
benchmarkNormalizeHeaderKey(b, src)
}
func benchmarkNormalizeHeaderKey(b *testing.B, src []byte) {
b.RunParallel(func(pb *testing.PB) {
buf := make([]byte, len(src))
for pb.Next() {
copy(buf, src)
normalizeHeaderKey(buf, false)
}
})
}