forked from valyala/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_timing_test.go
461 lines (405 loc) · 11.2 KB
/
server_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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package fasthttp
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"runtime"
"sync"
"sync/atomic"
"testing"
"time"
)
var defaultClientsCount = runtime.NumCPU()
func BenchmarkRequestCtxRedirect(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var ctx RequestCtx
for pb.Next() {
ctx.Request.SetRequestURI("http://aaa.com/fff/ss.html?sdf")
ctx.Redirect("/foo/bar?baz=111", StatusFound)
}
})
}
func BenchmarkServerGet1ReqPerConn(b *testing.B) {
benchmarkServerGet(b, defaultClientsCount, 1)
}
func BenchmarkServerGet2ReqPerConn(b *testing.B) {
benchmarkServerGet(b, defaultClientsCount, 2)
}
func BenchmarkServerGet10ReqPerConn(b *testing.B) {
benchmarkServerGet(b, defaultClientsCount, 10)
}
func BenchmarkServerGet10KReqPerConn(b *testing.B) {
benchmarkServerGet(b, defaultClientsCount, 10000)
}
func BenchmarkNetHTTPServerGet1ReqPerConn(b *testing.B) {
benchmarkNetHTTPServerGet(b, defaultClientsCount, 1)
}
func BenchmarkNetHTTPServerGet2ReqPerConn(b *testing.B) {
benchmarkNetHTTPServerGet(b, defaultClientsCount, 2)
}
func BenchmarkNetHTTPServerGet10ReqPerConn(b *testing.B) {
benchmarkNetHTTPServerGet(b, defaultClientsCount, 10)
}
func BenchmarkNetHTTPServerGet10KReqPerConn(b *testing.B) {
benchmarkNetHTTPServerGet(b, defaultClientsCount, 10000)
}
func BenchmarkServerPost1ReqPerConn(b *testing.B) {
benchmarkServerPost(b, defaultClientsCount, 1)
}
func BenchmarkServerPost2ReqPerConn(b *testing.B) {
benchmarkServerPost(b, defaultClientsCount, 2)
}
func BenchmarkServerPost10ReqPerConn(b *testing.B) {
benchmarkServerPost(b, defaultClientsCount, 10)
}
func BenchmarkServerPost10KReqPerConn(b *testing.B) {
benchmarkServerPost(b, defaultClientsCount, 10000)
}
func BenchmarkNetHTTPServerPost1ReqPerConn(b *testing.B) {
benchmarkNetHTTPServerPost(b, defaultClientsCount, 1)
}
func BenchmarkNetHTTPServerPost2ReqPerConn(b *testing.B) {
benchmarkNetHTTPServerPost(b, defaultClientsCount, 2)
}
func BenchmarkNetHTTPServerPost10ReqPerConn(b *testing.B) {
benchmarkNetHTTPServerPost(b, defaultClientsCount, 10)
}
func BenchmarkNetHTTPServerPost10KReqPerConn(b *testing.B) {
benchmarkNetHTTPServerPost(b, defaultClientsCount, 10000)
}
func BenchmarkServerGet1ReqPerConn10KClients(b *testing.B) {
benchmarkServerGet(b, 10000, 1)
}
func BenchmarkServerGet2ReqPerConn10KClients(b *testing.B) {
benchmarkServerGet(b, 10000, 2)
}
func BenchmarkServerGet10ReqPerConn10KClients(b *testing.B) {
benchmarkServerGet(b, 10000, 10)
}
func BenchmarkServerGet100ReqPerConn10KClients(b *testing.B) {
benchmarkServerGet(b, 10000, 100)
}
func BenchmarkNetHTTPServerGet1ReqPerConn10KClients(b *testing.B) {
benchmarkNetHTTPServerGet(b, 10000, 1)
}
func BenchmarkNetHTTPServerGet2ReqPerConn10KClients(b *testing.B) {
benchmarkNetHTTPServerGet(b, 10000, 2)
}
func BenchmarkNetHTTPServerGet10ReqPerConn10KClients(b *testing.B) {
benchmarkNetHTTPServerGet(b, 10000, 10)
}
func BenchmarkNetHTTPServerGet100ReqPerConn10KClients(b *testing.B) {
benchmarkNetHTTPServerGet(b, 10000, 100)
}
func BenchmarkServerHijack(b *testing.B) {
clientsCount := 1000
requestsPerConn := 10000
ch := make(chan struct{}, b.N)
responseBody := []byte("123")
s := &Server{
Handler: func(ctx *RequestCtx) {
ctx.Hijack(func(c net.Conn) {
// emulate server loop :)
err := ServeConn(c, func(ctx *RequestCtx) {
ctx.Success("foobar", responseBody)
registerServedRequest(b, ch)
})
if err != nil {
b.Fatalf("error when serving connection")
}
})
ctx.Success("foobar", responseBody)
registerServedRequest(b, ch)
},
Concurrency: 16 * clientsCount,
}
req := "GET /foo HTTP/1.1\r\nHost: google.com\r\n\r\n"
benchmarkServer(b, s, clientsCount, requestsPerConn, req)
verifyRequestsServed(b, ch)
}
func BenchmarkServerMaxConnsPerIP(b *testing.B) {
clientsCount := 1000
requestsPerConn := 10
ch := make(chan struct{}, b.N)
responseBody := []byte("123")
s := &Server{
Handler: func(ctx *RequestCtx) {
ctx.Success("foobar", responseBody)
registerServedRequest(b, ch)
},
MaxConnsPerIP: clientsCount * 2,
Concurrency: 16 * clientsCount,
}
req := "GET /foo HTTP/1.1\r\nHost: google.com\r\n\r\n"
benchmarkServer(b, s, clientsCount, requestsPerConn, req)
verifyRequestsServed(b, ch)
}
func BenchmarkServerTimeoutError(b *testing.B) {
clientsCount := 10
requestsPerConn := 1
ch := make(chan struct{}, b.N)
n := uint32(0)
responseBody := []byte("123")
s := &Server{
Handler: func(ctx *RequestCtx) {
if atomic.AddUint32(&n, 1)&7 == 0 {
ctx.TimeoutError("xxx")
go func() {
ctx.Success("foobar", responseBody)
}()
} else {
ctx.Success("foobar", responseBody)
}
registerServedRequest(b, ch)
},
Concurrency: 16 * clientsCount,
}
req := "GET /foo HTTP/1.1\r\nHost: google.com\r\n\r\n"
benchmarkServer(b, s, clientsCount, requestsPerConn, req)
verifyRequestsServed(b, ch)
}
type fakeServerConn struct {
net.TCPConn
ln *fakeListener
requestsCount int
pos int
closed uint32
}
func (c *fakeServerConn) Read(b []byte) (int, error) {
nn := 0
reqLen := len(c.ln.request)
for len(b) > 0 {
if c.requestsCount == 0 {
if nn == 0 {
return 0, io.EOF
}
return nn, nil
}
pos := c.pos % reqLen
n := copy(b, c.ln.request[pos:])
b = b[n:]
nn += n
c.pos += n
if n+pos == reqLen {
c.requestsCount--
}
}
return nn, nil
}
func (c *fakeServerConn) Write(b []byte) (int, error) {
return len(b), nil
}
var fakeAddr = net.TCPAddr{
IP: []byte{1, 2, 3, 4},
Port: 12345,
}
func (c *fakeServerConn) RemoteAddr() net.Addr {
return &fakeAddr
}
func (c *fakeServerConn) Close() error {
if atomic.AddUint32(&c.closed, 1) == 1 {
c.ln.ch <- c
}
return nil
}
func (c *fakeServerConn) SetReadDeadline(t time.Time) error {
return nil
}
func (c *fakeServerConn) SetWriteDeadline(t time.Time) error {
return nil
}
type fakeListener struct {
lock sync.Mutex
requestsCount int
requestsPerConn int
request []byte
ch chan *fakeServerConn
done chan struct{}
closed bool
}
func (ln *fakeListener) Accept() (net.Conn, error) {
ln.lock.Lock()
if ln.requestsCount == 0 {
ln.lock.Unlock()
for len(ln.ch) < cap(ln.ch) {
time.Sleep(10 * time.Millisecond)
}
ln.lock.Lock()
if !ln.closed {
close(ln.done)
ln.closed = true
}
ln.lock.Unlock()
return nil, io.EOF
}
requestsCount := ln.requestsPerConn
if requestsCount > ln.requestsCount {
requestsCount = ln.requestsCount
}
ln.requestsCount -= requestsCount
ln.lock.Unlock()
c := <-ln.ch
c.requestsCount = requestsCount
c.closed = 0
c.pos = 0
return c, nil
}
func (ln *fakeListener) Close() error {
return nil
}
func (ln *fakeListener) Addr() net.Addr {
return &fakeAddr
}
func newFakeListener(requestsCount, clientsCount, requestsPerConn int, request string) *fakeListener {
ln := &fakeListener{
requestsCount: requestsCount,
requestsPerConn: requestsPerConn,
request: []byte(request),
ch: make(chan *fakeServerConn, clientsCount),
done: make(chan struct{}),
}
for i := 0; i < clientsCount; i++ {
ln.ch <- &fakeServerConn{
ln: ln,
}
}
return ln
}
var (
fakeResponse = []byte("Hello, world!")
getRequest = "GET /foobar?baz HTTP/1.1\r\nHost: google.com\r\nUser-Agent: aaa/bbb/ccc/ddd/eee Firefox Chrome MSIE Opera\r\n" +
"Referer: http://xxx.com/aaa?bbb=ccc\r\nCookie: foo=bar; baz=baraz; aa=aakslsdweriwereowriewroire\r\n\r\n"
postRequest = fmt.Sprintf("POST /foobar?baz HTTP/1.1\r\nHost: google.com\r\nContent-Type: foo/bar\r\nContent-Length: %d\r\n"+
"User-Agent: Opera Chrome MSIE Firefox and other/1.2.34\r\nReferer: http://google.com/aaaa/bbb/ccc\r\n"+
"Cookie: foo=bar; baz=baraz; aa=aakslsdweriwereowriewroire\r\n\r\n%s",
len(fakeResponse), fakeResponse)
)
func benchmarkServerGet(b *testing.B, clientsCount, requestsPerConn int) {
ch := make(chan struct{}, b.N)
s := &Server{
Handler: func(ctx *RequestCtx) {
if !ctx.IsGet() {
b.Fatalf("Unexpected request method: %s", ctx.Method())
}
ctx.Success("text/plain", fakeResponse)
if requestsPerConn == 1 {
ctx.SetConnectionClose()
}
registerServedRequest(b, ch)
},
Concurrency: 16 * clientsCount,
}
benchmarkServer(b, s, clientsCount, requestsPerConn, getRequest)
verifyRequestsServed(b, ch)
}
func benchmarkNetHTTPServerGet(b *testing.B, clientsCount, requestsPerConn int) {
ch := make(chan struct{}, b.N)
s := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Method != MethodGet {
b.Fatalf("Unexpected request method: %s", req.Method)
}
h := w.Header()
h.Set("Content-Type", "text/plain")
if requestsPerConn == 1 {
h.Set(HeaderConnection, "close")
}
w.Write(fakeResponse)
registerServedRequest(b, ch)
}),
}
benchmarkServer(b, s, clientsCount, requestsPerConn, getRequest)
verifyRequestsServed(b, ch)
}
func benchmarkServerPost(b *testing.B, clientsCount, requestsPerConn int) {
ch := make(chan struct{}, b.N)
s := &Server{
Handler: func(ctx *RequestCtx) {
if !ctx.IsPost() {
b.Fatalf("Unexpected request method: %s", ctx.Method())
}
body := ctx.Request.Body()
if !bytes.Equal(body, fakeResponse) {
b.Fatalf("Unexpected body %q. Expected %q", body, fakeResponse)
}
ctx.Success("text/plain", body)
if requestsPerConn == 1 {
ctx.SetConnectionClose()
}
registerServedRequest(b, ch)
},
Concurrency: 16 * clientsCount,
}
benchmarkServer(b, s, clientsCount, requestsPerConn, postRequest)
verifyRequestsServed(b, ch)
}
func benchmarkNetHTTPServerPost(b *testing.B, clientsCount, requestsPerConn int) {
ch := make(chan struct{}, b.N)
s := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Method != MethodPost {
b.Fatalf("Unexpected request method: %s", req.Method)
}
body, err := ioutil.ReadAll(req.Body)
if err != nil {
b.Fatalf("Unexpected error: %s", err)
}
req.Body.Close()
if !bytes.Equal(body, fakeResponse) {
b.Fatalf("Unexpected body %q. Expected %q", body, fakeResponse)
}
h := w.Header()
h.Set("Content-Type", "text/plain")
if requestsPerConn == 1 {
h.Set(HeaderConnection, "close")
}
w.Write(body)
registerServedRequest(b, ch)
}),
}
benchmarkServer(b, s, clientsCount, requestsPerConn, postRequest)
verifyRequestsServed(b, ch)
}
func registerServedRequest(b *testing.B, ch chan<- struct{}) {
select {
case ch <- struct{}{}:
default:
b.Fatalf("More than %d requests served", cap(ch))
}
}
func verifyRequestsServed(b *testing.B, ch <-chan struct{}) {
requestsServed := 0
for len(ch) > 0 {
<-ch
requestsServed++
}
requestsSent := b.N
for requestsServed < requestsSent {
select {
case <-ch:
requestsServed++
case <-time.After(100 * time.Millisecond):
b.Fatalf("Unexpected number of requests served %d. Expected %d", requestsServed, requestsSent)
}
}
}
type realServer interface {
Serve(ln net.Listener) error
}
func benchmarkServer(b *testing.B, s realServer, clientsCount, requestsPerConn int, request string) {
ln := newFakeListener(b.N, clientsCount, requestsPerConn, request)
ch := make(chan struct{})
go func() {
s.Serve(ln)
ch <- struct{}{}
}()
<-ln.done
select {
case <-ch:
case <-time.After(10 * time.Second):
b.Fatalf("Server.Serve() didn't stop")
}
}