-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbenchmark_test.go
57 lines (44 loc) · 1.09 KB
/
benchmark_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
package ttlcache
import (
"testing"
"time"
)
// The Set time is highly depends on the map capacity.
// The main problem is map expansion.
func BenchmarkCache_Set_100(b *testing.B) {
benchmarkCacheSet(b, 100)
}
func BenchmarkCache_Set_1000(b *testing.B) {
benchmarkCacheSet(b, 1000)
}
func BenchmarkCache_Set_10000(b *testing.B) {
benchmarkCacheSet(b, 10000)
}
func BenchmarkCache_Get_100(b *testing.B) {
benchmarkCacheGet(b, 100)
}
func BenchmarkCache_Get_1000(b *testing.B) {
benchmarkCacheGet(b, 1000)
}
func BenchmarkCache_Get_10000(b *testing.B) {
benchmarkCacheGet(b, 10000)
}
func benchmarkCacheSet(b *testing.B, numberOfKeys int) {
resolution := 9999 * time.Second // Avoid stopping the world.
c := New(resolution)
for i := 0; i < b.N; i++ {
c.Set(IntKey(i%numberOfKeys), i, 0)
}
}
func benchmarkCacheGet(b *testing.B, numberOfKeys int) {
resolution := 9999 * time.Second // Avoid stopping the world.
c := New(resolution)
for i := 0; i < 100; i++ {
c.Set(IntKey(i), i, 0)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
val, _ := c.Get(IntKey(i % numberOfKeys))
_ = val
}
}