forked from CodisLabs/codis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_pttl.go
146 lines (136 loc) · 3.12 KB
/
test_pttl.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
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package main
import (
"flag"
"fmt"
"time"
)
type TestPttlTestCase struct {
proxy string
group int
round int
nkeys int
ntags int
expire int
}
func init() {
testcase = &TestPttlTestCase{}
}
func (tc *TestPttlTestCase) init() {
flag.StringVar(&tc.proxy, "proxy", "", "redis host:port")
flag.IntVar(&tc.group, "group", 8, "# of test players")
flag.IntVar(&tc.round, "round", 100, "# push/pop all per key")
flag.IntVar(&tc.nkeys, "nkeys", 1000, "# of keys per test")
flag.IntVar(&tc.ntags, "ntags", 1000, "# tags")
flag.IntVar(&tc.expire, "expire", 1, "expire seconds")
}
func (tc *TestPttlTestCase) 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 *TestPttlTestCase) player(gid int, tg *TestGroup, tags *ZeroTags) {
tg.PlayerWait()
defer tg.PlayerDone()
c := NewConn(tc.proxy)
defer c.Close()
us := UnitSlice(make([]*Unit, tc.nkeys))
for i := 0; i < len(us); i++ {
key := fmt.Sprintf("test_pttl_%d_tag{%d}", gid, i, tags.Get(i))
us[i] = NewUnit(key)
}
ttls := make(map[string]*TTL)
for _, u := range us {
u.Del(c, false)
ttls[u.key] = &TTL{}
ops.Incr()
}
for i := 0; i < tc.round; i++ {
for _, u := range us {
u.Set(c, u.key)
Expire(c, u, ttls[u.key], tc.expire)
ops.Incr()
}
for {
nothing := true
for _, u := range us {
if Pttl(c, u, ttls[u.key]) {
nothing = false
}
ops.Incr()
}
if nothing {
break
}
}
}
}
type TTL struct {
beg int64
end int64
done bool
}
func Expire(c *Conn, u *Unit, ttl *TTL, expire int) {
var rsp interface{}
defer func() {
if x := recover(); x != nil {
Panic("expire: c = %s, key = '%s', error = '%s', rsp = %v", c.Addr(), u.key, x, rsp)
}
}()
if expire <= 0 {
panic(fmt.Sprintf("invalid expire = %d", expire))
}
var err error
if rsp, err = c.Do("expire", u.key, expire); err != nil {
panic(err)
}
if v := c.Int(rsp); v != 1 {
panic(fmt.Sprintf("return = %d, expect = 1", v))
}
ttl.beg = time.Now().UnixNano()
ttl.end = int64(time.Second)*int64(expire) + ttl.beg
ttl.done = false
}
func Pttl(c *Conn, u *Unit, ttl *TTL) bool {
var rsp interface{}
defer func() {
if x := recover(); x != nil {
Panic("pttl: c = %s, key = '%s', error = '%s', rsp = %v", c.Addr(), u.key, x, rsp)
}
}()
var err error
if rsp, err = c.Do("pttl", u.key); err != nil {
panic(err)
}
if v := c.Int(rsp); v == -1 {
panic("return = -1")
} else if v == -2 {
if !ttl.done {
now := time.Now().UnixNano()
dlt := ttl.end - now
if dlt > int64(time.Second) {
panic(fmt.Sprintf("beg = %d, end = %d, dlt = %d", ttl.beg, ttl.end, dlt))
}
ttl.done = true
}
return false
} else {
now := time.Now().UnixNano()
dlt := now + int64(v)*int64(time.Millisecond) - ttl.end
if dlt < 0 {
dlt = -dlt
}
if dlt > int64(time.Second) {
panic(fmt.Sprintf("beg = %d, end = %d, dlt = %d, now = %d, return = %d", ttl.beg, ttl.end, dlt, now, v))
}
return true
}
}