-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
199 lines (181 loc) · 4.85 KB
/
cache_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
191
192
193
194
195
196
197
198
199
package plrucache
import (
"fmt"
"sync"
"testing"
"time"
)
func TestStoreAndGet(t *testing.T) {
t.Run("Store value and get it", func(t *testing.T) {
cache := New[string, string](1, 1*time.Minute)
key := "k1"
value := "val1"
cache.Set(key, value)
res, ok := cache.Get(key)
if !ok {
t.Errorf("expect: %v, got: %v", true, ok)
}
if res != value {
t.Errorf("expected: %s, got: %v", value, res)
}
})
t.Run("Get value that does not exist", func(t *testing.T) {
cache := New[string, string](1, 1*time.Minute)
key := "k1"
res, ok := cache.Get(key)
if ok {
t.Errorf("expect: %v, got: %v", false, ok)
}
if res != nil {
t.Errorf("expected: %v, got: %v", nil, res)
}
})
t.Run("Overfil the cache size and try to get displaced value", func(t *testing.T) {
cache := New[string, string](1, 1*time.Minute)
data := [][2]string{
{"k1", "val1"},
{"k2", "val2"},
}
for _, pair := range data {
cache.Set(pair[0], pair[1])
}
// first pair should be displaced
res, ok := cache.Get(data[0][0])
if ok {
t.Errorf("expect: %v, got: %v", false, ok)
}
if res != nil {
t.Errorf("expected: %v, got: %v", nil, res)
}
// second pair should be in cache
res, ok = cache.Get(data[1][0])
if !ok {
t.Errorf("expect: %v, got: %v", true, ok)
}
if res != data[1][1] {
t.Errorf("expected: %s, got: %v", data[1][1], res)
}
})
t.Run("Remove item from cache", func(t *testing.T) {
cache := New[string, string](3, 1*time.Minute)
data := [][2]string{
{"k1", "val1"},
{"k2", "val2"},
{"k3", "val3"},
}
for _, pair := range data {
cache.Set(pair[0], pair[1])
}
if cache.Len() != len(data) {
t.Errorf("expected %d, got %d", len(data), cache.Len())
}
cache.Delete(data[0][0])
if cache.Len() != len(data)-1 {
t.Errorf("expected %d, got %d", len(data)-1, cache.Len())
}
})
t.Run("Statistics calculation", func(t *testing.T) {
cache := New[string, string](10, 1*time.Minute)
data := [][2]string{
{"k1", "val1"},
{"k2", "val2"},
}
for _, pair := range data {
cache.Set(pair[0], pair[1])
}
stat := cache.Stat()
if stat.Misses != 0 {
t.Errorf("expected: %d, got: %d", 0, stat.Misses)
}
if stat.Hits != 0 {
t.Errorf("expected: %d, got: %d", 0, stat.Hits)
}
for _, pair := range data {
for i := 0; i < 3; i++ {
cache.Get(pair[0])
}
}
stat = cache.Stat()
if stat.Misses != 0 {
t.Errorf("expected: %d, got: %d", 0, stat.Misses)
}
if stat.Hits != uint64(3*len(data)) {
t.Errorf("expected: %d, got: %d", 3*len(data), stat.Hits)
}
for i := 0; i < 3; i++ {
cache.Get("not exists")
}
stat = cache.Stat()
if stat.Misses != 3 {
t.Errorf("expected: %d, got: %d", 3, stat.Misses)
}
if stat.Hits != uint64(3*len(data)) {
t.Errorf("expected: %d, got: %d", 3*len(data), stat.Hits)
}
cache.Reset()
if cache.Len() != 0 {
t.Errorf("expected: %v, got: %v", 0, cache.Len())
}
stat = cache.Stat()
if stat.Misses != 0 {
t.Errorf("expected: %d, got: %d", 0, stat.Misses)
}
if stat.Hits != 0 {
t.Errorf("expected: %d, got: %d", 0, stat.Hits)
}
})
}
func benchmarkCache(size, i int, b *testing.B) {
cache := New[string, string](size, 5*time.Second)
data := make([][2]string, i)
for j := 0; j < i; j++ {
data[j][0] = fmt.Sprintf("k%d", j)
data[j][1] = fmt.Sprintf("v%d", j)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
wg := sync.WaitGroup{}
wg.Add(i * 2)
for j := 0; j < i; j++ {
idx := j
go func() {
idx := idx
cache.Set(data[idx][0], data[idx][1])
wg.Done()
}()
go func() {
idx := idx
_, _ = cache.Get(data[idx][0])
wg.Done()
}()
}
wg.Wait()
}
}
func BenchmarkCache100(b *testing.B) { benchmarkCache(100, 100, b) }
func BenchmarkCache200(b *testing.B) { benchmarkCache(100, 200, b) }
func BenchmarkCache300(b *testing.B) { benchmarkCache(100, 300, b) }
func BenchmarkCache1000(b *testing.B) { benchmarkCache(100, 1000, b) }
func BenchmarkCache2000(b *testing.B) { benchmarkCache(100, 2000, b) }
func BenchmarkCache4000(b *testing.B) { benchmarkCache(100, 4000, b) }
func benchmarkCacheGet(size, i int, b *testing.B) {
cache := New[string, string](size, 5*time.Second)
data := make([][2]string, i)
for j := 0; j < i; j++ {
data[j][0] = fmt.Sprintf("k%d", j)
data[j][1] = fmt.Sprintf("v%d", j)
cache.Set(data[j][0], data[j][1])
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
for j := 0; j < i; j++ {
_, _ = cache.Get(data[j][0])
}
}
}
func BenchmarkCacheGet100(b *testing.B) { benchmarkCacheGet(100, 100, b) }
func BenchmarkCacheGet200(b *testing.B) { benchmarkCacheGet(100, 200, b) }
func BenchmarkCacheGet300(b *testing.B) { benchmarkCacheGet(100, 300, b) }
func BenchmarkCacheGet1000(b *testing.B) { benchmarkCacheGet(100, 1000, b) }
func BenchmarkCacheGet2000(b *testing.B) { benchmarkCacheGet(100, 2000, b) }
func BenchmarkCacheGet4000(b *testing.B) { benchmarkCacheGet(100, 4000, b) }