-
Notifications
You must be signed in to change notification settings - Fork 1
/
memcached_client.go
468 lines (422 loc) · 12.8 KB
/
memcached_client.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package main
import (
"github.com/dsidler/fpgamemcache/memcache"
"fmt"
"net"
"bufio"
"os"
"sync"
"time"
"math/rand"
"flag"
//"runtime/pprof"
//"log"
)
type configuration struct {
host string
numClients int
runtime int
scans int
setProb float64
valueLength int
zipfs float64
regex bool
matchProb float64
}
type statistics struct {
reqs int
sets int
gets int
miss int
setErrors int
getErrors int
}
const letters = "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz"
func GenerateKeys(numKeys int) []string {
keys := make([]string, numKeys)
// Generate keys in range
pre := 0
for i := 0; i < numKeys; i++ {
keys[i] = "foobarba" + string(letters[i % len(letters)]) + string(letters[pre % len(letters)]) +"xxxxzz"
if i+1 % len(letters) == 0 {
pre++;
}
}
// Check key length
for _,key := range(keys) {
if len(key) != 16 {
fmt.Println("Error: key has unexpected length: ", len(key))
os.Exit(1)
}
}
return keys
}
//TODO Generate Skewed Keys
func client(wg * sync.WaitGroup, s chan bool, mc *memcache.Client, config *configuration, statschan chan statistics) {
defer wg.Done()
numKeys := 1000
/*if config.zipfs != 0 {
fmt.Println("Using zipf")
}*/
oracle := rand.New(rand.NewSource(time.Now().UnixNano()))
r := rand.New(rand.NewSource(time.Now().UnixNano()))
zipf := rand.NewZipf(r, config.zipfs, 1.0, uint64(numKeys))
if config.regex {
config.valueLength -= 2
}
// Generate keys
keys := GenerateKeys(numKeys)
// Generate value
value := make([]byte, config.valueLength)
for i := 0; i < config.valueLength; i++ {
value[i] = letters[i % len(letters)]
}
matchingValue := append([]byte("0123456789abcdef"), value[32:]...)
matchingValue = append(matchingValue, []byte("systemsgroupethz")...)
stats := statistics{reqs: 0, sets: 0, gets: 0, setErrors: 0, getErrors: 0}
// Open a connection by issuing a set/get
var err error
if config.regex {
err = mc.SetJSON(&memcache.Item{Key: keys[0], Value: value})
} else {
err = mc.Set(&memcache.Item{Key: keys[0], Value: value})
}
if err != nil {
fmt.Println("Error on open/set connection: ", err.Error())
//os.Exit(1)
}
// Wait for start signal
startsig := <-s
if !startsig {
fmt.Println("Wrong start signal.")
}
//map for zipf, for debug
//distr := make(map[string]int)
keyidx := uint64(0)
for {
select {
case stopsig := <-s:
//fmt.Println("go signal")
if !stopsig {
statschan <- stats
return
}
default:
stats.reqs++
if config.zipfs != 0 {
keyidx = zipf.Uint64()
} else {
keyidx = uint64(stats.reqs % numKeys)
}
// For debug only
/*if _, ok := distr[keys[keyidx]]; !ok{
distr[keys[keyidx]] = 0
}
distr[keys[keyidx]]++*/
prob := oracle.Float64()
var err error
//if runs % 10 == 0 {
if prob < config.setProb {
if config.regex {
//matching prob
prob := oracle.Float64()
if prob < config.matchProb {
err = mc.SetJSON(&memcache.Item{Key: keys[keyidx], Value: matchingValue})
} else {
err = mc.SetJSON(&memcache.Item{Key: keys[keyidx], Value: value})
}
} else {
err = mc.Set(&memcache.Item{Key: keys[keyidx], Value: value})
}
if err != nil {
fmt.Println("Error on set: ", err.Error())
stats.setErrors++
//os.Exit(1)
} else {
stats.sets++
}
} else {
//TODO regex config
regex := []byte("0123456789abcdefsystemsgroupethz")
if config.regex {
_, err = mc.Ret(&memcache.Item{Key: keys[keyidx], Value: regex}, config.scans)
} else {
_, err = mc.Get(keys[keyidx], config.scans)
}
if err != nil {
fmt.Println("Error on get/ret: ", err.Error())
stats.getErrors++
//os.Exit(1)
} else {
stats.gets++
}
}
} //select
} //for
//print out distribution, for debug
/*for s,c := range distr {
fmt.Println(s, "\t", c)
}*/
}
func scan_client(wg * sync.WaitGroup, s chan bool, mc *memcache.Client, config *configuration, statschan chan statistics) {
defer wg.Done()
numKeys := 1000
if config.regex {
config.valueLength -= 2
}
// Generate keys
keys := GenerateKeys(numKeys)
// Generate value
value := make([]byte, config.valueLength)
for i := 0; i < config.valueLength; i++ {
value[i] = letters[i % len(letters)]
}
matchingValue := append([]byte("0123456789abcdef"), value[32:]...)
matchingValue = append(matchingValue, []byte("systemsgroupethz")...)
stats := statistics{reqs: 0, sets: 0, gets: 0, miss: 0, setErrors: 0, getErrors: 0}
// Open a connection by issuing a set/get
var err error
if config.regex {
err = mc.SetJSON(&memcache.Item{Key: keys[0], Value: value})
} else {
err = mc.Set(&memcache.Item{Key: keys[0], Value: value})
}
if err != nil {
fmt.Println("Error on open/set connection: ", err.Error())
//os.Exit(1)
}
// Wait for start signal
startsig := <-s
if !startsig {
fmt.Println("Wrong start signal.")
}
keyidx := uint64(0)
doSets := true
numKeys -= config.scans
for {
select {
case stopsig := <-s:
//fmt.Println("go signal")
if !stopsig {
statschan <- stats
return
}
default:
var err error
if doSets {
for i := 0; i < config.scans; i++ {
stats.reqs++
if i % 2 == 0 {
err = mc.SetJSON(&memcache.Item{Key: keys[keyidx+uint64(i)], Value: value})
} else {
err = mc.SetJSON(&memcache.Item{Key: keys[keyidx+uint64(i)], Value: matchingValue})
}
if err != nil {
fmt.Println("Error on set: ", err.Error())
stats.setErrors++
//os.Exit(1)
} else {
stats.sets++
}
} //for
doSets = false
} else {
stats.reqs++
//regex := []byte("0123456789abcdef0123456789abcdef")
regex := []byte("0123456789abcdefsystemsgroupethz")
it, err := mc.Ret(&memcache.Item{Key: keys[keyidx], Value: regex}, config.scans)
if err != nil {
fmt.Println("Error on get/ret: ", err.Error())
stats.getErrors++
//os.Exit(1)
} else {
stats.gets++
}
if it == nil {
stats.miss++
}
//update key
keyidx = uint64(stats.reqs % numKeys)
doSets = true
}
} //select
} //for
//print out distribution, for debug
/*for s,c := range distr {
fmt.Println(s, "\t", c)
}*/
}
func udp_client(wg * sync.WaitGroup, s chan bool, mc *memcache.Client, config *configuration, statschan chan statistics) {
defer wg.Done()
numKeys := 1000
oracle := rand.New(rand.NewSource(time.Now().UnixNano()))
r := rand.New(rand.NewSource(time.Now().UnixNano()))
zipf := rand.NewZipf(r, config.zipfs, 1.0, uint64(numKeys))
//Create connection
conn, err := net.Dial("udp", config.host)
if err != nil {
fmt.Println("Error on dial: ", err.Error())
os.Exit(1)
}
rw := bufio.NewReadWriter(bufio.NewReader(conn), bufio.NewWriter(conn))
// Generate keys
keys := GenerateKeys(numKeys)
// Generate value
value := make([]byte, config.valueLength)
for i := 0; i < config.valueLength; i++ {
value[i] = letters[i % len(letters)]
}
stats := statistics{reqs: 0, sets: 0, gets: 0, setErrors: 0, getErrors: 0}
// Wait for start signal
startsig := <-s
if !startsig {
fmt.Println("Wrong start signal.")
}
keyidx := uint64(0)
//for ; runs < config.numRuns; runs++ {
for {
select {
case stopsig := <-s:
fmt.Println("go signal")
if !stopsig {
statschan <- stats
return
}
default:
stats.reqs++ //TODO what about fails??
if config.zipfs != 0 {
keyidx = zipf.Uint64()
} else {
keyidx = uint64(stats.reqs % numKeys)
}
prob := oracle.Float64()
conn.SetReadDeadline(time.Now().Add(1000 * time.Millisecond))
if prob < config.setProb {
err := mc.SetUDP(rw, &memcache.Item{Key: keys[keyidx], Value: value})
if err != nil {
fmt.Println("Error on set: ", err.Error())
stats.setErrors++
//os.Exit(1)
} else {
stats.sets++
}
} else {
//TODO regex config
//regex := []byte("0123456789abcdef0123456789abcdef")
_, err := mc.GetUDP(rw, keys[keyidx], config.scans)
//_, err := mc.RetUDP(rw, &memcache.Item{Key: keys[keyidx], Value: regex}, config.scans)
if err != nil {
fmt.Println("Error on get: ", err.Error())
stats.getErrors++
//os.Exit(1)
} else {
stats.gets++
}
}
} // select
} //for
fmt.Println("done")
}
//var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
//runtime.GOMAXPROCS(8)
hostPtr := flag.String("host", "10.1.212.209:2888", "host addr and port")
udpPtr := flag.Bool("udp", false, "use UDP")
numPtr := flag.Int("clients", 1, "number of clients")
timePtr := flag.Int("time", 10, "runtime in seconds")
scanPtr := flag.Int("scans", 1, "number of scans")
setPtr := flag.Float64("setp", 0.1, "probability of set")
valuePtr := flag.Int("vallen", 64, "length of value")
zipfPtr := flag.Float64("zipfs", 0.0, "zipf value s")
zsoltPtr := flag.Bool("zsolt", true, "use zsolts protocol")
regexPtr := flag.Bool("regex", false, "use regex mode")
matchPtr := flag.Float64("regexmatch", 0.5, "probability of regex match")
flag.Parse()
/*if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}*/
useUDP := *udpPtr
config := configuration {
host: *hostPtr,
numClients: *numPtr,
runtime: *timePtr,
scans: *scanPtr,
setProb: *setPtr,
valueLength: *valuePtr,
zipfs: *zipfPtr,
regex: *regexPtr,
matchProb: *matchPtr}
if config.valueLength % 64 != 0 {
fmt.Println("value length Must be multiple of 64")
os.Exit(1)
}
// Adapt value length
config.valueLength -= 8
if config.zipfs != 0 && config.zipfs <= 1 {
fmt.Println("zifps must be >1 or 0.0")
os.Exit(1)
}
//TODO make host as input
mc := memcache.New(config.host)
// Set max idle cons
mc.MaxIdleConns = config.numClients+10//(numClients/2)
// set network timeout
mc.Timeout = 5000 * time.Millisecond
mc.UseZsolt = *zsoltPtr
wg := new(sync.WaitGroup)
start := make(chan bool)
statschan := make(chan statistics)
// start clients
for i := 0; i < config.numClients; i++ {
wg.Add(1)
if useUDP {
go udp_client(wg, start, mc, &config, statschan)
} else {
if config.scans != 1 {
go scan_client(wg, start, mc, &config, statschan)
} else {
go client(wg, start, mc, &config, statschan)
}
}
}
// wait for clients to setup
time.Sleep(time.Second*3)
fmt.Println("Start...")
starttime := time.Now()
for i := 0; i < config.numClients; i++ {
start <- true
}
// Run for x seconds
time.Sleep(time.Duration(config.runtime) * time.Second)
// Stop clients
fmt.Println("Stopping...")
for i :=0; i < config.numClients; i++ {
start <- false
}
stats := make([]statistics, config.numClients)
gstats := statistics{reqs: 0, sets: 0, gets: 0, miss: 0, setErrors: 0, getErrors: 0}
for i :=0; i < config.numClients; i++ {
stats[i] = <- statschan
gstats.reqs += stats[i].reqs
gstats.sets += stats[i].sets
gstats.gets += stats[i].gets
gstats.setErrors += stats[i].setErrors
gstats.getErrors += stats[i].getErrors
}
wg.Wait()
duration := time.Since(starttime).Seconds()
fmt.Println("----------------------------")
fmt.Printf("Throughput[KReq/s]: %2f\n", float64(gstats.reqs) / duration / 1000)
fmt.Printf("Duration[s]: %2f\n", duration)
fmt.Println("Requests: ", gstats.reqs)
fmt.Println("Sets: ", gstats.sets)
fmt.Println("Gets: ", gstats.gets)
fmt.Println("Misses: ", gstats.miss)
fmt.Println("Set errors: ", gstats.setErrors)
fmt.Println("Get errors: ", gstats.getErrors)
}