-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathredis.go
270 lines (238 loc) · 5.48 KB
/
redis.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
/*
This is an example about how to build a redis benchmark to using fperf
A fperf testcase in fact is an implementation of fperf.UnaryClient.
The client has two method:
Dial(addr string) error
Request() error
Dial connect to the server address witch set by fperf option "-server". fperf will exit and print
the error message if error occurs.
Request is the method to fperf uses to issue an request. The returned error would be printed and
fperf would continue.
*/
package redis
import (
"bufio"
"fmt"
"math/rand"
"os"
"strconv"
"strings"
"sync"
"github.com/fperf/fperf"
"github.com/garyburd/redigo/redis"
)
const seqPlaceHolder = "__seq_int__"
const randPlaceHolder = "__rand_int__"
var seq func() string = seqCreater(0)
var random func() string = randCreater(10000000000000000)
//A test case can have itself options witch would be passed by fperf
type options struct {
verbose bool
auth string
load string
}
type command struct {
name string
args []interface{}
}
//A client is a struct that should implement fperf.UnaryClient
type redisClient struct {
args []string //the args of client, we use redis command as args
rds redis.Conn //the redis connection, should be created when call Dial
options options //the options user set
commands []command //commands read from file
}
//newRedisClient create the client object. The function should be
//registered to fperf, fperf -h will list all the registered clients(testcases)
func newRedisClient(flag *fperf.FlagSet) fperf.Client {
c := new(redisClient)
flag.BoolVar(&c.options.verbose, "v", false, "verbose")
flag.StringVar(&c.options.auth, "a", "", "auth of redis")
flag.StringVar(&c.options.load, "load", "", "load commands from file")
//Customize the usage output
flag.Usage = func() {
fmt.Printf("Usage: redis [options] [cmd] [args...], use __rand_int__ or __seq_int__ to generate random or sequence keys\noptions:\n")
flag.PrintDefaults()
}
flag.Parse()
args := flag.Args()
//Set the default command if not be set
if len(args) == 0 {
args = []string{"SET", "fperf", "hello world"}
}
c.args = args
if c.options.verbose {
fmt.Println(c.args)
}
if c.options.load != "" {
c.readFile()
}
return c
}
//Dial to redis server. The addr is set by the fperf option "-server"
func (c *redisClient) Dial(addr string) error {
rds, err := redis.DialURL(addr)
if err != nil {
return err
}
if c.options.auth != "" {
rds.Do("auth", c.options.auth)
}
c.rds = rds
return nil
}
func seqCreater(begin int64) func() string {
// filled map, filled generated to 16 bytes
l := []string{
"",
"0",
"00",
"000",
"0000",
"00000",
"000000",
"0000000",
"00000000",
"000000000",
"0000000000",
"00000000000",
"000000000000",
"0000000000000",
"00000000000000",
"000000000000000",
}
v := begin
m := &sync.Mutex{}
return func() string {
m.Lock()
s := strconv.FormatInt(v, 10)
v += 1
m.Unlock()
filled := len(l) - len(s)
if filled <= 0 {
return s
}
return l[filled] + s
}
}
func randCreater(max int64) func() string {
// filled map, filled generated to 16 bytes
l := []string{
"",
"0",
"00",
"000",
"0000",
"00000",
"000000",
"0000000",
"00000000",
"000000000",
"0000000000",
"00000000000",
"000000000000",
"0000000000000",
"00000000000000",
"000000000000000",
}
var v int64
m := &sync.Mutex{}
return func() string {
m.Lock()
v = rand.Int63n(max)
s := strconv.FormatInt(v, 10)
m.Unlock()
filled := len(l) - len(s)
if filled <= 0 {
return s
}
return l[filled] + s
}
}
func replaceSeq(s string) string {
return strings.Replace(s, seqPlaceHolder, seq(), -1)
}
func replaceRand(s string) string {
return strings.Replace(s, randPlaceHolder, random(), -1)
}
func (c *redisClient) readFile() error {
file, err := os.Open(c.options.load)
if err != nil {
return err
}
defer file.Close()
var commands []command
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) == 0 {
continue
}
cmd := command{name: fields[0]}
for _, arg := range fields[1:] {
cmd.args = append(cmd.args, arg)
}
commands = append(commands, cmd)
}
if err := scanner.Err(); err != nil {
return err
}
c.commands = commands
return nil
}
func replace(s string) string {
if strings.Index(s, seqPlaceHolder) >= 0 {
s = replaceSeq(s)
}
if strings.Index(s, randPlaceHolder) >= 0 {
s = replaceRand(s)
}
return s
}
func (c *redisClient) RequestBatch() error {
for _, cmd := range c.commands {
var args []interface{}
name := replace(cmd.name)
for _, arg := range cmd.args {
args = append(args, replace(arg.(string)))
}
if err := c.rds.Send(name, args...); err != nil {
return err
}
}
if err := c.rds.Flush(); err != nil {
return err
}
for _ = range c.commands {
_, err := c.rds.Receive()
if err != nil {
return err
}
}
return nil
}
//Request send a redis request and return the error if there is
func (c *redisClient) Request() error {
if c.options.load != "" {
return c.RequestBatch()
}
var args []interface{}
//Build the redis cmd and args
cmd := c.args[0]
for _, arg := range c.args[1:] {
if strings.Index(arg, seqPlaceHolder) >= 0 {
arg = replaceSeq(arg)
}
if strings.Index(arg, randPlaceHolder) >= 0 {
arg = replaceRand(arg)
}
args = append(args, arg)
}
_, err := c.rds.Do(cmd, args...)
return err
}
//Register to fperf
func init() {
//rand.Seed(time.Now().UnixNano())
fperf.Register("redis", newRedisClient, "redis performance benchmark")
}