-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark_test.go
64 lines (55 loc) · 1.03 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
58
59
60
61
62
63
64
package icache
import (
"math/rand"
"os"
"runtime"
"testing"
"time"
)
func randomString() string {
n := rand.Intn(10) + 8
var letter = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
b := make([]rune, n)
for i := range b {
b[i] = letter[rand.Intn(len(letter))]
}
return string(b)
}
func TestMain(m *testing.M) {
runtime.GOMAXPROCS(runtime.NumCPU())
icache = NewPot[item](WithTTL(time.Hour))
for i := 0; i < 10000; i++ {
id := randomString()
ids = append(ids, id)
icache.Set(id, newItem(id))
}
idsLen = len(ids) - 1
os.Exit(m.Run())
}
func randomID() string {
return ids[rand.Intn(idsLen)]
}
var idsLen int
var ids []string
var icache Pot[item]
func BenchmarkICache(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
get()
}
}
var it item
func get() {
it, _ = icache.Get(randomID())
}
func BenchmarkICacheConcurrent(b *testing.B) {
b.SetParallelism(100)
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
get()
}
})
}