-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_bench_test.go
100 lines (82 loc) · 1.64 KB
/
cache_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
package wcache
import (
"context"
"math/rand"
"strconv"
"testing"
"time"
)
func BenchmarkSameKey(b *testing.B) {
const (
key = "exists"
value = "some test value"
)
c := New(context.Background(), time.Hour, NoopExpire)
b.Run("Set", func(b *testing.B) {
for n := 0; n < b.N; n++ {
c.Set(key, value)
}
})
b.Run("Get", func(b *testing.B) {
for n := 0; n < b.N; n++ {
c.Get(key)
}
})
b.Run("Set_Delete", func(b *testing.B) {
for n := 0; n < b.N; n++ {
c.Set(key, value)
c.Delete(key)
}
})
}
func BenchmarkRandomKeys(b *testing.B) {
const (
value = "some test value"
keys = 1000
)
c := New(context.Background(), time.Hour, NoopExpire)
b.Run("Set", func(b *testing.B) {
for n := 0; n < b.N; n++ {
c.Set(strconv.Itoa(rand.Intn(keys)), value)
}
})
b.Run("Get", func(b *testing.B) {
for n := 0; n < b.N; n++ {
c.Get(strconv.Itoa(rand.Intn(keys)))
}
})
b.Run("Set_Delete", func(b *testing.B) {
for n := 0; n < b.N; n++ {
key := strconv.Itoa(rand.Intn(keys))
c.Set(key, value)
c.Delete(key)
}
})
}
func BenchmarkSetSameKeyWithTTL(b *testing.B) {
const (
key = "exists"
value = "some test value"
)
ttl := time.Microsecond
c := New(context.Background(), ttl, NoopExpire)
for n := 0; n < b.N; n++ {
c.Set(key, value)
time.Sleep(ttl)
}
}
func BenchmarkCompareFn(b *testing.B) {
const (
key = "exists"
value = "some test value"
)
compareFn := func(old, new interface{}) (result interface{}) {
time.Sleep(time.Microsecond)
return new
}
c := New(context.Background(), time.Hour, NoopExpire)
c.CompareFn = compareFn
for n := 0; n < b.N; n++ {
c.Set(key, value)
}
}