-
Notifications
You must be signed in to change notification settings - Fork 0
/
golru_test.go
153 lines (131 loc) · 2.85 KB
/
golru_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
// A simple LRU cache for storing documents ([]byte). When the size maximum is reached, items are evicted
// starting with the least recently used. This data structure is goroutine-safe (it has a lock around all
// operations).
package golru_test
import (
"github.com/manucorporat/golru"
"math"
"strconv"
"testing"
)
func TestSimpleSetAndGet(t *testing.T) {
c := golru.New(2, -1)
c.Set("key1", []byte("hi"))
if string(c.Get("key1")) != "hi" || c.Len() != 1 {
t.FailNow()
}
}
func TestMultipleSetAndGet(t *testing.T) {
c := golru.New(2, -1)
c.Set("key1", []byte("hi"))
c.Set("key1", []byte("bye"))
if string(c.Get("key1")) != "bye" || c.Len() != 1 {
t.FailNow()
}
c.Set("key1", []byte("hey"))
if string(c.Get("key1")) != "hey" || c.Len() != 1 {
t.FailNow()
}
c.Set("key1", []byte("goodbye"))
if string(c.Get("key1")) != "goodbye" || c.Len() != 1 {
t.FailNow()
}
}
func TestMultipleSetDeleteAndGet(t *testing.T) {
c := golru.New(2, -1)
c.Set("key1", []byte("hi"))
c.Del("key1")
if c.Get("key1") != nil || c.Len() != 0 {
t.FailNow()
}
if c.Get("key1") != nil || c.Len() != 0 {
t.FailNow()
}
}
func TestMultipleBatch(t *testing.T) {
size := 1000
c := golru.New(size, -1)
for i := 0; i < size; i++ {
key := strconv.Itoa(i)
c.Set(key, []byte("A"+key))
if c.Len() != i+1 {
t.FailNow()
}
}
for i := 0; i < size; i++ {
key := strconv.Itoa(i)
if string(c.Get(key)) != ("A" + key) {
t.FailNow()
}
}
for i := 0; i < size; i++ {
key := strconv.Itoa(i)
c.Del(key)
if c.Len() != size-i-1 {
t.FailNow()
}
}
}
func TestMultipleCaped(t *testing.T) {
c := golru.New(2, -1)
c.Set("key", []byte("doc0"))
c.Set("key1", []byte("doc1"))
c.Set("key2", []byte("doc2"))
if c.Get("key") != nil || c.Len() != 2 {
t.FailNow()
}
c.Set("key3", []byte("doc3"))
if c.Get("key1") != nil || c.Len() != 2 {
t.FailNow()
}
c.Get("key2")
c.Set("key4", []byte("doc4"))
if c.Get("key3") != nil || c.Len() != 2 {
t.FailNow()
}
if c.Get("key2") == nil {
t.FailNow()
}
}
func TestMultipleCaped100(t *testing.T) {
size := 1000
c := golru.New(100, -1)
for i := 0; i < size; i++ {
key := strconv.Itoa(i)
c.Set(key, []byte("A"+key))
if float64(c.Len()) != math.Min(float64(i+1), 100.0) {
t.FailNow()
}
}
}
func BenchmarkSetFullSize(b *testing.B) {
c := golru.New(b.N, -1)
RunSet(c, b)
}
func BenchmarkSetHalfSize(b *testing.B) {
c := golru.New(b.N/2, -1)
RunSet(c, b)
}
func BenchmarkSet10000(b *testing.B) {
c := golru.New(10000, -1)
RunSet(c, b)
}
func BenchmarkGetFullSize(b *testing.B) {
c := golru.New(b.N, -1)
RunGet(c, b)
}
func RunSet(c *golru.Cache, b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := strconv.Itoa(i)
c.Set(key, []byte(key))
}
}
func RunGet(c *golru.Cache, b *testing.B) {
RunSet(c, b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := strconv.Itoa(i)
c.Get(key)
}
}