forked from CodisLabs/codis
/
test_incr2.go
77 lines (70 loc) · 1.58 KB
/
test_incr2.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
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package main
import (
"flag"
"fmt"
"time"
)
type TestIncr2TestCase struct {
proxy1 string
proxy2 string
group int
round int
nkeys int
ntags int
}
func init() {
testcase = &TestIncr2TestCase{}
}
func (tc *TestIncr2TestCase) init() {
flag.StringVar(&tc.proxy1, "proxy1", "", "redis#1 host:port")
flag.StringVar(&tc.proxy2, "proxy2", "", "redis#2 host:port")
flag.IntVar(&tc.group, "group", 8, "# of test players")
flag.IntVar(&tc.round, "round", 10, "# of incr opts per key")
flag.IntVar(&tc.nkeys, "nkeys", 10000, "# of keys per test")
flag.IntVar(&tc.ntags, "ntags", 1000, "# of tags")
}
func (tc *TestIncr2TestCase) main() {
tg := &TestGroup{}
tg.Reset()
var tags = NewZeroTags(tc.ntags)
for g := 0; g < tc.group; g++ {
tg.AddPlayer()
go tc.player(g, tg, tags)
}
tg.Start()
tg.Wait()
fmt.Println("done")
}
func (tc *TestIncr2TestCase) player(gid int, tg *TestGroup, tags *ZeroTags) {
tg.PlayerWait()
defer tg.PlayerDone()
c1 := NewConn(tc.proxy1)
defer c1.Close()
c2 := NewConn(tc.proxy2)
defer c2.Close()
us := UnitSlice(make([]*Unit, tc.nkeys))
for i := 0; i < len(us); i++ {
key := fmt.Sprintf("test_incr2_%d_%d_tag{%s}", gid, i, tags.Get(i))
us[i] = NewUnit(key)
}
for _, u := range us {
u.Del(c1, false)
ops.Incr()
}
for i := 0; i < tc.round; i++ {
r := &Rand{time.Now().UnixNano()}
for _, u := range us {
u.Incr(c1)
if r.Next()%2 != 0 {
c1, c2 = c2, c1
}
ops.Incr()
}
}
for _, u := range us {
u.Del(c2, true)
ops.Incr()
}
}