-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_test.go
88 lines (79 loc) · 1.74 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package gocachebenchmarkplus_test
import (
"fmt"
"math/rand"
"testing"
"github.com/Yiling-J/go-cache-benchmark-plus/clients"
)
var benchClients = []clients.Client[string, string]{
&clients.Theine[string, string]{},
&clients.Ristretto[string, string]{},
&clients.Otter[string, string]{},
}
func BenchmarkGetParallel(b *testing.B) {
keys := []string{}
for i := 0; i < 100000; i++ {
keys = append(keys, fmt.Sprintf("%d", i))
}
total := len(keys) - 1
for _, client := range benchClients {
client.Init(total)
for _, key := range keys {
client.Set(key, key)
}
b.ResetTimer()
b.Run(client.Name(), func(b *testing.B) {
b.RunParallel(func(p *testing.PB) {
counter := rand.Int() % total
for p.Next() {
client.Get(keys[counter%total])
counter++
}
})
})
client.Close()
}
}
func BenchmarkGetSingleParallel(b *testing.B) {
keys := []string{}
for i := 0; i < 100000; i++ {
keys = append(keys, fmt.Sprintf("%d", i))
}
total := len(keys) - 1
for _, client := range benchClients {
client.Init(total)
for _, key := range keys {
client.Set(key, key)
}
b.ResetTimer()
b.Run(client.Name(), func(b *testing.B) {
b.RunParallel(func(p *testing.PB) {
for p.Next() {
client.Get(keys[0])
}
})
})
client.Close()
}
}
func BenchmarkSetParallel(b *testing.B) {
keys := []string{}
for i := 0; i < 1000000; i++ {
keys = append(keys, fmt.Sprintf("%d", i))
}
total := len(keys) - 1
for _, client := range benchClients {
client.Init(100000)
b.ResetTimer()
b.Run(client.Name(), func(b *testing.B) {
b.RunParallel(func(p *testing.PB) {
counter := int(rand.Int() % total)
for p.Next() {
client.Set(keys[counter%total], "bar")
counter++
}
})
})
client.Close()
}
}