-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_benchmark_test.go
More file actions
84 lines (65 loc) · 1.66 KB
/
cache_benchmark_test.go
File metadata and controls
84 lines (65 loc) · 1.66 KB
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
package cache_test
import (
"context"
"strconv"
"testing"
"time"
cache "github.com/microup/vcache"
)
func BenchmarkCacheAdd(b *testing.B) {
c := cache.New(1*time.Second, 2*time.Second)
for i := 0; i < b.N; i++ {
key := "key" + strconv.Itoa(i)
value := "value" + strconv.Itoa(i)
_ = c.Add(key, value)
}
}
func BenchmarkCacheGet(b *testing.B) {
cacheTest := cache.New(1*time.Second, 2*time.Second)
for i := 0; i < b.N; i++ {
key := "key" + strconv.Itoa(i)
value := "value" + strconv.Itoa(i)
_ = cacheTest.Add(key, value)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := "key" + strconv.Itoa(i)
cacheTest.Get(key)
}
}
func BenchmarkCacheDelete(b *testing.B) {
cacheTest := cache.New(1*time.Second, 2*time.Second)
for i := 0; i < b.N; i++ {
key := "key" + strconv.Itoa(i)
value := "value" + strconv.Itoa(i)
_ = cacheTest.Add(key, value)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := "key" + strconv.Itoa(i)
cacheTest.Delete(key)
}
}
func BenchmarkCacheEvict(b *testing.B) {
cacheTest := cache.New(60*time.Second, 2*time.Second)
for i := 0; i < b.N; i++ {
key := "key" + strconv.Itoa(i)
value := "value" + strconv.Itoa(i)
_ = cacheTest.Add(key, value)
}
// this needs to be done so that the condition for deletion applies to all records.
time.Sleep(2 * time.Second)
b.ResetTimer()
for i := 0; i < b.N; i++ {
cacheTest.Evict()
}
}
func BenchmarkCacheMixed(b *testing.B) {
cacheTest := cache.New(2*time.Second, 1*time.Nanosecond)
_ = cacheTest.StartEvict(context.Background())
for i := 0; i < b.N; i++ {
key := "key" + strconv.Itoa(i)
value := "value" + strconv.Itoa(i)
_ = cacheTest.Add(key, value)
}
}