-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbench_test.go
94 lines (76 loc) · 1.48 KB
/
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
package evcache_test
import (
"errors"
"sync/atomic"
"testing"
"github.com/mgnsk/evcache/v4"
)
func BenchmarkFetchAndEvictParallel(b *testing.B) {
b.StopTimer()
c := evcache.New[uint64, int]()
index := uint64(0)
errFetch := errors.New("error fetching")
b.ReportAllocs()
b.StartTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if idx := atomic.AddUint64(&index, 1); idx%2 == 0 {
_, _ = c.Fetch(0, func() (int, error) {
if idx%4 == 0 {
return 0, errFetch
}
return 0, nil
})
} else {
c.Evict(0)
}
}
})
}
func BenchmarkFetchExists(b *testing.B) {
b.StopTimer()
c := evcache.New[uint64, int]()
c.Fetch(0, func() (int, error) {
return 0, nil
})
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
_, _ = c.Fetch(0, func() (int, error) {
panic("unexpected fetch callback")
})
}
}
func BenchmarkFetchNotExists(b *testing.B) {
b.StopTimer()
c := evcache.New[int, int]()
b.ReportAllocs()
b.StartTimer()
for i := 0; i < b.N; i++ {
_, _ = c.Fetch(i, func() (int, error) {
return 0, nil
})
}
}
func BenchmarkLoad(b *testing.B) {
for _, policy := range []string{
evcache.FIFO,
evcache.LRU,
evcache.LFU,
} {
b.Run(policy, func(b *testing.B) {
c := evcache.New[int, int](
evcache.WithPolicy(policy),
)
c.Store(0, 1)
b.ReportAllocs()
b.ResetTimer()
for range b.N {
value, _ := c.Load(0)
if value != 1 {
b.Fatal("expected value to be loaded")
}
}
})
}
}