forked from tidwall/redbench
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbench.go
406 lines (385 loc) · 10.6 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
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
//package redbench
package main
import (
"bufio"
"errors"
"flag"
"fmt"
//"github.com/pkg/profile"
"github.com/wangaoone/ecRedis"
"io"
"io/ioutil"
"log"
"math"
"math/rand"
"os"
"strconv"
"strings"
"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 {
AddrList string
Requests int
Clients int
Pipeline int
Keymin int
Keymax int
Objsz int
Datashard int
Parityshard int
ECmaxgoroutine int
Decoding bool
Op int
Quiet bool
CSV bool
Stdout io.Writer
Stderr io.Writer
Printlog bool
}
// DefaultsOptions are the default options used by the Bench() function.
var DefaultOptions = &Options{
AddrList: "127.0.0.1:6379",
Requests: 15,
Clients: 1,
Pipeline: 1,
Keymin: 0,
Keymax: 99,
Objsz: 10485760 * 4,
Datashard: 4,
Parityshard: 2,
ECmaxgoroutine: 32,
Decoding: true,
Op: 0, // 0: SET; 1: GET
Quiet: false,
CSV: false,
Stdout: os.Stdout,
Stderr: os.Stderr,
Printlog: true,
}
func getRandomRange(min int, max int) int {
var rn int
rand.Seed(time.Now().UnixNano())
rn = rand.Intn(max-min) + min
return rn
}
func genKey(keymin int, keymax int, op int, i int) string {
var ret string
if op == 0 { // SET
keyIdx := keymin + i%(keymax-keymin+1)
ret = strings.Join([]string{"key_", strconv.Itoa(keyIdx)}, "")
} else { // GET
rn := getRandomRange(keymin, keymax)
ret = strings.Join([]string{"key_", strconv.Itoa(rn)}, "")
}
log.Println("generated key: ", ret, "len: ", len(ret))
return ret
}
// 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,
//) {
func Bench(
opts *Options,
) {
if !opts.Printlog {
log.SetOutput(ioutil.Discard)
}
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
rpc := opts.Requests
var tstop int64
remaining := int64(opts.Clients)
errs := make([]error, opts.Clients)
durs := make([][]time.Duration, opts.Clients)
clients := make([]ecRedis.Client, opts.Clients)
// create all clients
for i := 0; i < opts.Clients; i++ {
crequests := rpc
durs[i] = make([]time.Duration, crequests)
for j := 0; j < len(durs[i]); j++ {
durs[i][j] = -1
}
//conn, err := net.Dial("tcp", addr)
addrArr := strings.Split(opts.AddrList, ",")
log.Println("number of hosts: ", len(addrArr))
client := ecRedis.NewClient(opts.Datashard, opts.Parityshard, opts.ECmaxgoroutine)
client.Dial(addrArr)
/*
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*/
clients[i] = client
}
tstart := time.Now()
for i := 0; i < opts.Clients; i++ {
crequests := rpc
//if i == opts.Clients-1 {
// crequests += rpcex
//}
//val := make([]byte, 10485760)
val := make([]byte, opts.Objsz)
rand.Read(val)
//go func(conn net.Conn, client, crequests int) {
go func(client ecRedis.Client, cid, crequests int) {
defer func() {
atomic.AddInt64(&remaining, -1)
}()
/*if conn == nil {
if client == 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
}
key := genKey(opts.Keymin, opts.Keymax, opts.Op, i)
/*
buf = buf[:0]
for i := 0; i < n; i++ {
buf = fill(buf)
}
atomic.AddUint64(&totalPayload, uint64(len(buf)))
*/
atomic.AddUint64(&totalPayload, uint64(len(val)))
start := time.Now()
//_, err := conn.Write(buf)
//client.EcSet("key", val)
var host string // FIXME: dirty hack... : (
if opts.Op == 0 {
host, _ = client.EcSet(key, val)
} else {
host, _ = client.EcGet(key)
}
client.Receive(host)
if opts.Op == 1 && opts.Decoding {
ecRedis.Decoding(client.EC, client.ChunkArr)
}
/*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[cid][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[cid] = err
}
//}(conns[i], i, crequests)
}(clients[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)
fmt.Fprintf(opts.Stdout, "\r%.2f", 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)
fmt.Fprintf(opts.Stdout, "\"%.2f\"\n", 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
}
func helpInfo() {
fmt.Println("Usage: ./bench [options]")
fmt.Println("Option list: ")
fmt.Println(" -addrlist [ADDR:PORT,...]: server address:port")
fmt.Println(" -n [NUMBER]: number of requests")
fmt.Println(" -c [NUMBER]: number of concurrent clients")
fmt.Println(" -pipeline [NUMBER]: number of pipelined requests")
fmt.Println(" -keymin [NUMBER]: minimum key range")
fmt.Println(" -keymax [NUMBER]: maximum key range")
fmt.Println(" -sz [NUMBER]: object data size")
fmt.Println(" -d [NUMBER]: number of data shards for RS erasure coding")
fmt.Println(" -p [NUMBER]: number of parity shards for RS erasure coding")
fmt.Println(" -g [NUMBER]: max number of goroutines for RS erasure coding")
fmt.Println(" -dec: do decoding after Receive()?")
fmt.Println(" -op [0 or 1]: operation type (0: SET (load the data store); 1: GET)")
fmt.Println(" -log: print out debugging info?")
fmt.Println(" -h: print out help info?")
}
func main() {
//profile.Start(profile.CPUProfile)
//defer profile.Start(profile.CPUProfile).Stop()
//defer profile.Start().Stop()
var printInfo bool
flag.BoolVar(&printInfo, "h", false, "help info?")
option := DefaultOptions
flag.StringVar(&option.AddrList, "addrlist", "127.0.0.1:6379", "server address:port")
flag.IntVar(&option.Requests, "n", 10, "number of requests")
flag.IntVar(&option.Clients, "c", 1, "number of clients")
flag.IntVar(&option.Pipeline, "pipeline", 1, "number of pipelined requests")
flag.IntVar(&option.Keymin, "keymin", 0, "minimum key range")
flag.IntVar(&option.Keymax, "keymax", 10, "maximum key range")
flag.IntVar(&option.Objsz, "sz", 128, "object data size")
flag.IntVar(&option.Datashard, "d", 4, "number of data shards for RS erasure coding")
flag.IntVar(&option.Parityshard, "p", 2, "number of parity shards for RS erasure coding")
flag.IntVar(&option.ECmaxgoroutine, "g", 32, "max number of goroutines for RS erasure coding")
flag.BoolVar(&option.Decoding, "dec", false, "do decoding after Receive()?")
flag.IntVar(&option.Op, "op", 0, "operation type")
flag.BoolVar(&option.Printlog, "log", true, "print debugging log?")
flag.Parse()
if printInfo {
helpInfo()
os.Exit(0)
}
fmt.Println("Test starting...")
Bench(option)
}