-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem_test.go
158 lines (129 loc) · 3.41 KB
/
mem_test.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
package timerstore
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestMemProvider(t *testing.T) {
equal := func(expected, got interface{}) {
if got != expected {
t.Fatalf("expeted: %v, got: %v", expected, got)
}
}
mem := NewMemProvider()
mem.SetPrefix("Test")
equal("Test", mem.prefix)
mem.Set("first", ("this is the first"), 5)
data, ok, err := mem.Get("first")
equal(nil, err)
equal(true, ok)
equal("this is the first", string(data))
err = mem.Set("first", ("this is the first replacement"), 4)
equal(nil, err)
data, _, _ = mem.Get("first")
equal("this is the first replacement", string(data))
mem.Set("second", ("this is the second"), 10)
due, ok, _ := mem.Before(time.Now().Unix() + 15)
equal(true, ok)
equal(2, len(due))
t.Logf("due: %v\n", due)
mem.Del("first")
mem.Del("second")
data, ok, err = mem.Get("first")
equal(nil, err)
equal(false, ok)
_, ok, err = mem.Get("second")
equal(nil, err)
equal(false, ok)
}
func TestMemTimerStore(t *testing.T) {
equal := func(expected, got interface{}) {
if got != expected {
t.Fatalf("expeted: %v, got: %v", expected, got)
}
}
m := NewMemProvider()
RegisterProvider("mem", m)
p, ok := providerMgr["mem"]
equal(true, ok)
equal(m, p)
store, err := NewTimerStore("Test", "mem", 1*time.Second, func(key string, val string) {
fmt.Printf("key: %s, value: %v expired\n", key, string(val))
})
equal(nil, err)
store.Set("first", ("this is the first"), 5)
store.Set("second", ("this is the second"), 6)
store.Set("second.5", ("this is the second.5"), 6)
store.Set("third", ("this is the third"), 8)
var data string
data, ok, err = store.Get("first")
equal(nil, err)
equal(true, ok)
equal("this is the first", string(data))
store.Set("second", ("this is the second replacement"), 1)
time.Sleep(7 * time.Second)
_, ok, err = store.Get("seond")
equal(false, ok)
data, ok, err = store.Get("third")
equal(true, ok)
equal("this is the third", string(data))
}
func BenchmarkMemIterate10K(b *testing.B) {
benchmarkMemIterate(10000, b)
}
func BenchmarkMemIterate100K(b *testing.B) {
benchmarkMemIterate(100000, b)
}
func BenchmarkMemIterate1M(b *testing.B) {
benchmarkMemIterate(1000000, b)
}
func benchmarkMemIterate(i int, b *testing.B) {
for j := 0; j < b.N; j++ {
mem := TimerStore{
prefix: "Test",
store: NewMemProvider(),
h: func(key string, val string) {},
}
for n := 0; n < i; n++ {
key := fmt.Sprintf("key_%d", n)
val := string(fmt.Sprintf("val_%d", n))
mem.Set(key, val, int64(n))
}
st := time.Now()
due, ok, _ := mem.store.Before(time.Now().Unix())
if ok {
for key, val := range due {
mem.h(key, val)
mem.store.Del(key)
}
}
cost := time.Since(st) / time.Microsecond
fmt.Printf("i: %d, cost: %d\n", i, cost)
}
}
// func BenchmarkMemTimerStoreParallel(b *testing.B) {
// initMem()
// b.RunParallel(func(pb *testing.PB) {
// for pb.Next() {
// setAndGet()
// }
// })
// }
var memStore *TimerStore
func initMem() {
m := NewMemProvider()
RegisterProvider("mem", m)
memStore, _ = NewTimerStore("Test", "mem", 1*time.Second, func(key string, val string) {
// fmt.Printf("key: %s, value: %v expired\n", key, val)
})
}
func setAndGet() {
s := rand.NewSource(time.Now().UnixNano())
r := rand.New(s).Intn(1000)
r1 := rand.New(s).Intn(1000000)
key := fmt.Sprintf("key_%d", r1)
val := string(fmt.Sprintf("val_%d", r1))
memStore.Set(key, val, int64(r))
memStore.Get(key)
}