-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache_test.go
135 lines (115 loc) · 2.86 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
package directcache_test
import (
"encoding/binary"
"fmt"
"sort"
"testing"
"github.com/cespare/xxhash/v2"
"github.com/qianbin/directcache"
"github.com/stretchr/testify/require"
)
func ExampleCache() {
// capacity will be set to directcache.MinCapacity
c := directcache.New(0)
key := []byte("DirectCache")
val := []byte("is awesome,")
c.Set(key, val)
got, ok := c.Get(key)
fmt.Println(string(key), string(got), ok)
// Output: DirectCache is awesome, true
}
func TestCache(t *testing.T) {
c := directcache.New(0)
require.Equal(t, directcache.MinCapacity, c.Capacity(), "cap should be at least MinCapacity")
c.Reset(directcache.MinCapacity * 2)
require.Equal(t, directcache.MinCapacity*2, c.Capacity())
k := "key"
v := "val"
// set
require.True(t, c.Set([]byte(k), []byte(v)))
// has get
require.True(t, c.Has([]byte(k)))
got, ok := c.Get([]byte(k))
require.True(t, ok)
require.Equal(t, v, string(got))
// del has
require.True(t, c.Del([]byte(k)))
require.False(t, c.Del([]byte(k)))
require.False(t, c.Has([]byte(k)))
// advGet
c.Set([]byte(k), []byte(v))
got = got[:0]
require.True(t, c.AdvGet([]byte(k), func(val []byte) {
got = append(got, val...)
}, false))
require.Equal(t, v, string(got))
// advSet
advv := "advval"
require.True(t, c.AdvSet([]byte(k), len(advv), func(val []byte) {
copy(val, advv)
}))
got, ok = c.Get([]byte(k))
require.True(t, ok)
require.Equal(t, advv, string(got))
}
func TestCacheDump(t *testing.T) {
c := directcache.New(0)
var set []string
for i := 0; i < 100; i++ {
k := []byte{'k', byte(i)}
v := []byte{'v', byte(i)}
c.Set(k, v)
set = append(set, string(k)+string(v))
}
sort.Strings(set)
var dumps []string
c.Dump(func(e directcache.Entry) bool {
dumps = append(dumps, string(e.Key())+string(e.Value()))
return true
})
sort.Strings(dumps)
require.Equal(t, set, dumps)
}
func BenchmarkCacheSetGet(b *testing.B) {
const nEntries = 1000000
b.Run("directcache", func(b *testing.B) {
k := make([]byte, 8)
v := make([]byte, 8)
entrySize := len(k) + len(v) + 4
nBytes := entrySize * nEntries
c := directcache.New(nBytes * 2)
b.Run("set", func(b *testing.B) {
for i := 0; i < b.N; i++ {
binary.BigEndian.PutUint64(k, uint64(i%nEntries))
c.Set(k, v)
}
})
b.Run("get", func(b *testing.B) {
for i := 0; i < b.N; i++ {
binary.BigEndian.PutUint64(k, uint64(i%nEntries))
c.AdvGet(k, func(val []byte) {}, false)
}
})
})
b.Run("map", func(b *testing.B) {
m := map[uint64]int{}
b.Run("map.set", func(b *testing.B) {
for i := 0; i < b.N; i++ {
m[uint64(i%nEntries)] = i
}
})
b.Run("map.get", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = m[uint64(i%nEntries)]
}
})
})
}
func BenchmarkHash(b *testing.B) {
b.Run("xxhash.Sum64", func(b *testing.B) {
data := make([]byte, 32)
for i := 0; i < b.N; i++ {
xxhash.Sum64(data)
}
})
}