-
Notifications
You must be signed in to change notification settings - Fork 9
/
bench.go
274 lines (264 loc) · 6.2 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
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
package redbench
import (
"bufio"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"net"
"os"
"strconv"
"sync/atomic"
"time"
)
func readResp(rd *bufio.Reader, n int, opts *Options) error {
for i := 0; i < n; i++ {
line, err := rd.ReadBytes('\n')
if err != nil {
return err
}
switch line[0] {
default:
return errors.New("invalid server response")
case '+', ':':
case '-':
opts.Stderr.Write(line)
case '$':
n, err := strconv.ParseInt(string(line[1:len(line)-2]), 10, 64)
if err != nil {
return err
}
if n >= 0 {
if _, err = io.CopyN(ioutil.Discard, rd, n+2); err != nil {
return err
}
}
case '*':
n, err := strconv.ParseInt(string(line[1:len(line)-2]), 10, 64)
if err != nil {
return err
}
readResp(rd, int(n), opts)
}
}
return nil
}
// Options represents various options used by the Bench() function.
type Options struct {
Requests int
Clients int
Pipeline int
Quiet bool
CSV bool
Stdout io.Writer
Stderr io.Writer
}
// DefaultsOptions are the default options used by the Bench() function.
var DefaultOptions = &Options{
Requests: 100000,
Clients: 50,
Pipeline: 1,
Quiet: false,
CSV: false,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
// Bench performs a benchmark on the server at the specified address.
func Bench(
name string,
addr string,
opts *Options,
prep func(conn net.Conn) bool,
fill func(buf []byte) []byte,
) {
if opts == nil {
opts = DefaultOptions
}
if opts.Stderr == nil {
opts.Stderr = ioutil.Discard
}
if opts.Stdout == nil {
opts.Stdout = ioutil.Discard
}
var totalPayload uint64
var count uint64
var duration int64
rpc := opts.Requests / opts.Clients
rpcex := opts.Requests % opts.Clients
var tstop int64
remaining := int64(opts.Clients)
errs := make([]error, opts.Clients)
durs := make([][]time.Duration, opts.Clients)
conns := make([]net.Conn, opts.Clients)
// create all clients
for i := 0; i < opts.Clients; i++ {
crequests := rpc
if i == opts.Clients-1 {
crequests += rpcex
}
durs[i] = make([]time.Duration, crequests)
for j := 0; j < len(durs[i]); j++ {
durs[i][j] = -1
}
conn, err := net.Dial("tcp", addr)
if err != nil {
if i == 0 {
fmt.Fprintf(opts.Stderr, "%s\n", err.Error())
return
}
errs[i] = err
}
if conn != nil && prep != nil {
if !prep(conn) {
conn.Close()
conn = nil
}
}
conns[i] = conn
}
tstart := time.Now()
for i := 0; i < opts.Clients; i++ {
crequests := rpc
if i == opts.Clients-1 {
crequests += rpcex
}
go func(conn net.Conn, client, crequests int) {
defer func() {
atomic.AddInt64(&remaining, -1)
}()
if conn == nil {
return
}
err := func() error {
var buf []byte
rd := bufio.NewReader(conn)
for i := 0; i < crequests; i += opts.Pipeline {
n := opts.Pipeline
if i+n > crequests {
n = crequests - i
}
buf = buf[:0]
for i := 0; i < n; i++ {
buf = fill(buf)
}
atomic.AddUint64(&totalPayload, uint64(len(buf)))
start := time.Now()
_, err := conn.Write(buf)
if err != nil {
return err
}
if err := readResp(rd, n, opts); err != nil {
return err
}
stop := time.Since(start)
for j := 0; j < n; j++ {
durs[client][i+j] = stop / time.Duration(n)
}
atomic.AddInt64(&duration, int64(stop))
atomic.AddUint64(&count, uint64(n))
atomic.StoreInt64(&tstop, int64(time.Since(tstart)))
}
return nil
}()
if err != nil {
errs[client] = err
}
}(conns[i], i, crequests)
}
var die bool
for {
remaining := int(atomic.LoadInt64(&remaining)) // active clients
count := int(atomic.LoadUint64(&count)) // completed requests
real := time.Duration(atomic.LoadInt64(&tstop)) // real duration
totalPayload := int(atomic.LoadUint64(&totalPayload)) // size of all bytes sent
more := remaining > 0
var realrps float64
if real > 0 {
realrps = float64(count) / (float64(real) / float64(time.Second))
}
if !opts.CSV {
fmt.Fprintf(opts.Stdout, "\r%s: %.2f", name, realrps)
if more {
fmt.Fprintf(opts.Stdout, "\r")
} else if opts.Quiet {
fmt.Fprintf(opts.Stdout, " requests per second\n")
} else {
fmt.Fprintf(opts.Stdout, "\r====== %s ======\n", name)
fmt.Fprintf(opts.Stdout, " %d requests completed in %.2f seconds\n", opts.Requests, float64(real)/float64(time.Second))
fmt.Fprintf(opts.Stdout, " %d parallel clients\n", opts.Clients)
fmt.Fprintf(opts.Stdout, " %d bytes payload\n", totalPayload/opts.Requests)
fmt.Fprintf(opts.Stdout, " keep alive: 1\n")
fmt.Fprintf(opts.Stdout, "\n")
var limit time.Duration
var lastper float64
for {
limit += time.Millisecond
var hits, count int
for i := 0; i < len(durs); i++ {
for j := 0; j < len(durs[i]); j++ {
dur := durs[i][j]
if dur == -1 {
continue
}
if dur < limit {
hits++
}
count++
}
}
per := float64(hits) / float64(count)
if math.Floor(per*10000) == math.Floor(lastper*10000) {
continue
}
lastper = per
fmt.Fprintf(opts.Stdout, "%.2f%% <= %d milliseconds\n", per*100, (limit-time.Millisecond)/time.Millisecond)
if per == 1.0 {
break
}
}
fmt.Fprintf(opts.Stdout, "%.2f requests per second\n\n", realrps)
}
}
if !more {
if opts.CSV {
fmt.Fprintf(opts.Stdout, "\"%s\",\"%.2f\"\n", name, realrps)
}
for _, err := range errs {
if err != nil {
fmt.Fprintf(opts.Stderr, "%s\n", err)
die = true
if count == 0 {
break
}
}
}
break
}
time.Sleep(time.Second / 5)
}
// close clients
for i := 0; i < len(conns); i++ {
if conns[i] != nil {
conns[i].Close()
}
}
if die {
os.Exit(1)
}
}
// AppendCommand will append a Redis command to the byte slice and
// returns a modifed slice.
func AppendCommand(buf []byte, args ...string) []byte {
buf = append(buf, '*')
buf = strconv.AppendInt(buf, int64(len(args)), 10)
buf = append(buf, '\r', '\n')
for _, arg := range args {
buf = append(buf, '$')
buf = strconv.AppendInt(buf, int64(len(arg)), 10)
buf = append(buf, '\r', '\n')
buf = append(buf, arg...)
buf = append(buf, '\r', '\n')
}
return buf
}