-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.go
191 lines (150 loc) · 3.78 KB
/
bench.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
package httptestbench
import (
"fmt"
"log"
"net"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/valyala/fasthttp"
"github.com/vearutop/dynhist-go"
)
type countingDialer struct {
dial fasthttp.DialFunc
sent uint64
rcvd uint64
mu sync.Mutex
conns []net.Conn
}
type countingConn struct {
net.Conn
dialer *countingDialer
}
func (c countingConn) Write(data []byte) (int, error) {
n, err := c.Conn.Write(data)
atomic.AddUint64(&c.dialer.sent, uint64(n))
return n, err
}
func (c countingConn) Read(data []byte) (int, error) {
n, err := c.Conn.Read(data)
atomic.AddUint64(&c.dialer.rcvd, uint64(n))
return n, err
}
func (c *countingDialer) Dial(addr string) (net.Conn, error) {
conn, err := c.dial(addr)
if err == nil {
c.mu.Lock()
c.conns = append(c.conns, conn)
c.mu.Unlock()
}
return countingConn{Conn: conn, dialer: c}, err
}
func (c *countingDialer) Close() {
c.mu.Lock()
defer c.mu.Unlock()
for _, conn := range c.conns {
err := conn.Close()
if err != nil {
log.Printf("failed to close connection: %v", err)
}
}
c.conns = nil
}
// RoundTrip sends b.N requests concurrently and asserts valid response.
func RoundTrip(
b *testing.B,
concurrency int,
setupRequest func(i int, req *fasthttp.Request),
responseIsValid func(i int, resp *fasthttp.Response) bool,
) {
b.Helper()
dialer := countingDialer{
dial: (&fasthttp.TCPDialer{Concurrency: 1000}).Dial,
}
client := fasthttp.Client{
Dial: dialer.Dial,
DisableHeaderNamesNormalizing: true,
DisablePathNormalizing: true,
MaxIdleConnDuration: time.Second / 10,
}
atomic.StoreUint64(&dialer.rcvd, 0)
atomic.StoreUint64(&dialer.sent, 0)
hist := dynhist.Collector{
BucketsLimit: 50,
WeightFunc: dynhist.ExpWidth(1.2, 0.9),
}
concurrentBench(b, concurrency, func(i int) {
req := fasthttp.AcquireRequest()
resp := fasthttp.AcquireResponse()
defer func() {
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
}()
setupRequest(i, req)
start := time.Now()
err := client.Do(req, resp)
if err != nil {
b.Fatal(err.Error())
}
hist.Add(time.Since(start).Seconds() * 1000)
if !responseIsValid(i, resp) {
failIteration(i, resp.StatusCode(), resp.Body())
}
})
b.ReportMetric(float64(atomic.LoadUint64(&dialer.sent))/float64(b.N), "B:sent/op")
b.ReportMetric(float64(atomic.LoadUint64(&dialer.rcvd))/float64(b.N), "B:rcvd/op")
b.ReportMetric(hist.Percentile(99.9), "99.9%:ms")
b.ReportMetric(hist.Percentile(99), "99%:ms")
b.ReportMetric(hist.Percentile(90), "90%:ms")
b.ReportMetric(hist.Percentile(50), "50%:ms")
client.CloseIdleConnections()
}
func concurrentBench(b *testing.B, concurrency int, iterate func(i int)) {
b.Helper()
semaphore := make(chan bool, concurrency)
start := time.Now()
b.Helper()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
i := i
semaphore <- true
go func() {
defer func() {
<-semaphore
}()
iterate(i)
}()
}
for i := 0; i < cap(semaphore); i++ {
semaphore <- true
}
b.StopTimer()
elapsed := time.Since(start)
b.ReportMetric(float64(b.N)/elapsed.Seconds(), "rps")
}
func failIteration(i int, code int, body []byte) {
panic(fmt.Sprintf("iteration: %d, unexpected result status: %d, body: %q",
i, code, string(body)))
}
// ServeHTTP serves b.N requests with http.Handler concurrently and asserts valid response.
func ServeHTTP(
b *testing.B,
concurrency int,
h http.Handler,
makeRequest func(i int) *http.Request,
responseIsValid func(i int, resp *httptest.ResponseRecorder) bool,
) {
b.Helper()
concurrentBench(b, concurrency, func(i int) {
req := makeRequest(i)
rw := httptest.NewRecorder()
h.ServeHTTP(rw, req)
if !responseIsValid(i, rw) {
failIteration(i, rw.Code, rw.Body.Bytes())
}
})
}