-
Notifications
You must be signed in to change notification settings - Fork 1
/
fasthttp_test.go
102 lines (81 loc) · 2.3 KB
/
fasthttp_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
// +build ignore
package tcprpc
import (
"context"
"net"
"testing"
"time"
"github.com/hydrogen18/memlistener"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
"golang.org/x/sync/errgroup"
)
func BenchmarkFastHTTPParallel(b *testing.B) {
const M = 10
s := fasthttp.Server{
Handler: func(ctx *fasthttp.RequestCtx) {
body := ctx.Request.Body()
_, err := ctx.Write(body)
assert.NoError(b, err)
},
}
l := memlistener.NewMemoryListener()
c := fasthttp.Client{}
c.Dial = func(a string) (net.Conn, error) { return l.Dial("tcp", a) }
go func() {
err := s.Serve(l)
assert.NoError(b, err)
}()
g, _ := errgroup.WithContext(context.TODO())
for j := 0; j < M; j++ {
g.Go(func() error {
for i := 0; i < b.N/M; i++ {
args := fasthttp.AcquireArgs()
args.Set("func", "some_long_long_string_argument_121312312312312312312312312333312_qweqewqewqweqewqweqewqweqweqewqwe")
code, body, err := c.Post(nil, "http://host/sleep", args)
assert.NoError(b, err)
if i%10000 == 0 {
assert.Equal(b, int(200), code)
assert.Equal(b, []byte("func=some_long_long_string_argument_121312312312312312312312312333312_qweqewqewqweqewqweqewqweqweqewqwe"), body)
}
}
return nil
})
}
g.Wait()
}
func BenchmarkFastHTTPParallelLong(b *testing.B) {
const M = 10
s := fasthttp.Server{
Handler: func(ctx *fasthttp.RequestCtx) {
body := ctx.Request.Body()
time.Sleep(100 * time.Millisecond)
_, err := ctx.Write(body)
assert.NoError(b, err)
},
}
l := memlistener.NewMemoryListener()
c := fasthttp.Client{}
c.Dial = func(a string) (net.Conn, error) { return l.Dial("tcp", a) }
go func() {
err := s.Serve(l)
assert.NoError(b, err)
}()
g, _ := errgroup.WithContext(context.TODO())
for j := 0; j < M; j++ {
g.Go(func() error {
for i := 0; i < b.N/M; i++ {
args := fasthttp.AcquireArgs()
args.Set("func", "some_long_long_string_argument_121312312312312312312312312333312_qweqewqewqweqewqweqewqweqweqewqwe")
code, body, err := c.Post(nil, "http://host/sleep", args)
assert.NoError(b, err)
if i%10000 == 0 {
assert.Equal(b, int(200), code)
assert.Equal(b, []byte("func=some_long_long_string_argument_121312312312312312312312312333312_qweqewqewqweqewqweqewqweqweqewqwe"), body)
}
}
return nil
})
}
g.Wait()
}