-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttlcache_bench_test.go
190 lines (178 loc) · 3.67 KB
/
ttlcache_bench_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
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
package ttlcache
import (
"fmt"
"math"
"math/rand"
"runtime"
"strconv"
"sync"
"testing"
"time"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func RandomString(n int) string {
if n < 1 {
return ""
}
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
return string(b)
}
type live struct {
i int64
n string
}
func (l *live) loop() {
ticker := time.NewTicker(time.Millisecond * 20)
for {
select {
case now := <-ticker.C:
l.i = now.Unix()
}
}
}
func newLive() *live {
l := &live{}
go l.loop()
return l
}
func livex(l *live, wg *sync.WaitGroup) {
defer wg.Done()
//runtime.LockOSThread()
for i := int64(0); i < 10000; i++ {
j := rand.Int63() % 100
if i == j {
}
}
}
func get(cache *TTLCache, wg *sync.WaitGroup) {
defer wg.Done()
//runtime.LockOSThread()
for i := 0; i < 100000; i++ {
val, found := cache.Get("small-" + strconv.FormatInt(int64(i), 10))
if found {
if val.(int) != i {
panic("set val invalid")
}
}
}
}
func set(cache *TTLCache, wg *sync.WaitGroup) {
defer wg.Done()
//runtime.LockOSThread()
for i := 0; i < 100000; i++ {
cache.Set("small-"+strconv.FormatInt(int64(i), 10), i, 3 /*seconds*/)
}
}
func BenchmarkLive(b *testing.B) {
fmt.Println("hello boy")
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
l := &live{}
var wg sync.WaitGroup
for i := 0; i < 20; i++ {
wg.Add(1)
go livex(l, &wg)
}
wg.Wait()
}
func BenchmarkSetGet(b *testing.B) {
fmt.Println("hello boy")
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
cache := New(BucketsCount(512),
BucketsMapPreAllocSize(256),
CleanInterval(10),
)
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
set(cache, &wg)
}
for i := 0; i < 10; i++ {
wg.Add(2)
go set(cache, &wg)
go get(cache, &wg)
}
wg.Wait()
}
func TestAll(t *testing.T) {
cache := New(BucketsCount(512),
BucketsMapPreAllocSize(256),
CleanInterval(10),
)
cache.Set("ttlcache", "nb", 1)
val, found := cache.Get("ttlcache")
if val.(string) != "nb" {
t.Error("get set val error")
return
}
if exist := cache.Exist("ttlcache"); !exist {
t.Error("exist error")
return
}
time.Sleep(time.Millisecond * 1500)
_, found = cache.Get("ttlcache")
if found {
t.Error("set expiration error")
return
}
if ok := cache.Add("ttlcache", "nb", 2); !ok {
t.Error("add error")
return
}
if ok := cache.Add("ttlcache", "nb", 2); ok {
t.Error("add an exist item error")
return
}
val, found = cache.Get("ttlcache")
if !found || val.(string) != "nb" {
t.Error("add item error")
return
}
if ok := cache.Replace("ttlcache", "no1", 1); !ok {
t.Error("replace an exist item error")
return
}
val, found = cache.Get("ttlcache")
if !found || val.(string) != "no1" {
t.Error("replace item error")
return
}
time.Sleep(time.Millisecond * 1300)
_, found = cache.Get("ttlcache")
if found {
t.Error("replace expiration error")
return
}
n, ok := cache.IncrementInt("ttlcacheno", 1, 1)
if !ok || n != 1 {
t.Error("incrementInt error")
return
}
time.Sleep(time.Millisecond * 1200)
_, found = cache.Get("ttlcacheno")
if found {
t.Error("incrementInt expiration error")
return
}
n1, ok := cache.IncrementInt64("ttlcacheno", 10, 1)
if !ok || n1 != 10 {
t.Error("incrementInt64 error")
return
}
f, ok := cache.IncrementFloat64("ttlcacheno", 10, 1)
if ok {
t.Error("incrementFloat64 by int64 error")
return
}
cache.Delete("ttlcacheno")
f, ok = cache.IncrementFloat64("ttlcacheno", 10, 1)
if !ok || !(math.Abs(f-10) < 0.000001) {
t.Error("incrementFloat64 error")
return
}
fmt.Printf("items: %d\n", cache.Items())
time.Sleep(time.Second * 10)
fmt.Printf("items: %d\n", cache.Items())
}