-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyedpool_test.go
160 lines (150 loc) · 3.25 KB
/
keyedpool_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
package base
import (
"math/rand"
"strconv"
"testing"
)
func BenchmarkKeyedPool_Random(b *testing.B) {
values := make([]string, b.N*2)
for i := range values {
values[i] = strconv.FormatInt(rand.Int63()&0x7FFF, 10)
}
p := NewKeyedPool[string](KeyedPoolOptions[string]{
MaxEntries: 8196,
New: func(key string) (string, error) {
return "", ErrKeyNotFound
},
})
b.ResetTimer()
var hit, miss int
for i, value := range values {
if i&1 == 0 {
p.Put(value, value)
} else {
v, err := p.Get(value)
if err == nil {
hit++
p.Put(value, v)
} else {
miss++
}
}
}
b.Logf("n: %d hit: %d miss: %d ratio: %f", b.N, hit, miss, float64(hit)/float64(hit+miss))
}
func BenchmarkKeyedPool_Frequent(b *testing.B) {
values := make([]string, b.N*2)
for i := range values {
if i&1 == 0 {
values[i] = strconv.FormatInt(rand.Int63()&0x3FFF, 10)
} else {
values[i] = strconv.FormatInt(rand.Int63()&0x7FFF, 10)
}
}
p := NewKeyedPool[string](KeyedPoolOptions[string]{
MaxEntries: 8196,
New: func(key string) (string, error) {
return "", ErrKeyNotFound
},
})
b.ResetTimer()
for i := 0; i < b.N; i++ {
p.Put(values[i], values[i])
}
var hit, miss int
for i := 0; i < b.N; i++ {
value := values[i]
v, err := p.Get(value)
if err == nil {
hit++
p.Put(value, v)
} else {
miss++
}
}
b.Logf("n: %d hit: %d miss: %d ratio: %f", b.N, hit, miss, float64(hit)/float64(hit+miss))
}
func TestKeyedPoolEviction(t *testing.T) {
evictCounter := 0
p := NewKeyedPool[int](KeyedPoolOptions[int]{
MaxEntries: 128,
Shards: 1,
OnEvicted: func(key string, value int) {
if value > 128 {
t.Fatalf("key %s should have not been evicted", key)
}
evictCounter++
},
New: func(key string) (int, error) {
return 0, ErrKeyNotFound
},
})
for i := 0; i < 256; i++ {
value := strconv.Itoa(i)
p.Put(value, i)
}
if p.Len() != 128 {
t.Fatalf("bad len: %v, want 128", p.Len())
}
if evictCounter != 128 {
t.Fatalf("bad evict count: %v, want 128", evictCounter)
}
for i := 0; i < 128; i++ {
value := strconv.Itoa(i)
_, err := p.Get(value)
if err == nil {
t.Fatalf("key %d should have been evicted", i)
}
}
for i := 128; i < 256; i++ {
value := strconv.Itoa(i)
_, err := p.Get(value)
if err != nil {
t.Fatalf("key %d should have not be evicted", i)
}
}
}
func TestKeyedPoolRemove(t *testing.T) {
p := NewKeyedPool[string](KeyedPoolOptions[string]{
MaxEntries: 128,
Shards: 1,
New: func(key string) (string, error) {
return "", ErrKeyNotFound
},
})
for i := 0; i < 128; i++ {
value := strconv.Itoa(i)
p.Put(value, value)
}
if p.Len() != 128 {
t.Fatalf("bad len: %v, want 128", p.Len())
}
for i := 0; i < 64; i++ {
value := strconv.Itoa(i)
p.Remove(value)
_, err := p.Get(value)
if err == nil {
t.Fatalf("key %d should have been deleted", i)
}
}
if p.Len() != 64 {
t.Fatalf("bad len: %v, want 64", p.Len())
}
}
func TestKeyedPoolClear(t *testing.T) {
p := NewKeyedPool[string](KeyedPoolOptions[string]{
MaxEntries: 128,
Shards: 1,
})
for i := 0; i < 128; i++ {
value := strconv.Itoa(i)
p.Put(value, value)
}
if p.Len() != 128 {
t.Fatalf("bad len: %v, want 128", p.Len())
}
p.Clear()
if p.Len() != 0 {
t.Fatalf("bad len: %v, want 0", p.Len())
}
}