-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
68 lines (54 loc) · 1.08 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
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
})
}
}