-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_common_test.go
183 lines (155 loc) · 4.25 KB
/
bench_common_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xcache/blob/main/LICENSE.
package xcache_test
import (
"context"
"encoding/binary"
"sync/atomic"
"testing"
"time"
"unsafe"
"github.com/actforgood/xcache"
)
func benchLoadSequential(cache xcache.Cache) func(b *testing.B) {
return func(b *testing.B) {
ctx, expire, key, value := getBenchInput()
if err := cache.Save(ctx, key, value, expire); err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, err := cache.Load(ctx, key); err != nil {
b.Error(err)
}
}
}
}
func benchLoadParallel(cache xcache.Cache) func(b *testing.B) {
return func(b *testing.B) {
ctx, expire, key, value := getBenchInput()
if err := cache.Save(ctx, key, value, expire); err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := cache.Load(ctx, key); err != nil {
b.Error(err)
}
}
})
}
}
func benchSaveSequential(cache xcache.Cache) func(b *testing.B) {
return func(b *testing.B) {
ctx, expire, keyPrefix, value := getBenchInput()
// Used byte strategy to generate distinct key
// because in this way, no extra allocation is reported.
// Something more simple like key := keyPrefix + strconv.FormatInt(int64(n), 10) would end up
// reporting 2 extra allocations which have nothing to do with the tested cache.
keyPrefixLen := len(keyPrefix)
keyBytes := make([]byte, len(keyPrefix)+8)
for i := 0; i < keyPrefixLen; i++ {
keyBytes[i] = keyPrefix[i]
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
binary.LittleEndian.PutUint64(keyBytes[keyPrefixLen:], uint64(n))
key := *(*string)(unsafe.Pointer(&keyBytes))
if err := cache.Save(ctx, key, value, expire); err != nil {
b.Error(err)
}
}
}
}
func benchSaveParallel(cache xcache.Cache) func(b *testing.B) {
return func(b *testing.B) {
ctx, expire, keyPrefix, value := getBenchInput()
var counter uint64
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
// 1 extra allocation will be reported from keyBytes,
// so in _Save_parallel benchmarks real value should be considered the reported one - 1.
keyPrefixLen := len(keyPrefix)
keyBytes := make([]byte, len(keyPrefix)+8)
for i := 0; i < keyPrefixLen; i++ {
keyBytes[i] = keyPrefix[i]
}
binary.LittleEndian.PutUint64(keyBytes[keyPrefixLen:], atomic.LoadUint64(&counter))
key := *(*string)(unsafe.Pointer(&keyBytes))
if err := cache.Save(ctx, key, value, expire); err != nil {
b.Error(err)
}
atomic.AddUint64(&counter, 1)
}
})
}
}
func benchTTLSequential(cache xcache.Cache) func(b *testing.B) {
return func(b *testing.B) {
ctx, expire, key, value := getBenchInput()
if err := cache.Save(ctx, key, value, expire); err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, err := cache.TTL(ctx, key); err != nil {
b.Error(err)
}
}
}
}
func benchTTLParallel(cache xcache.Cache) func(b *testing.B) {
return func(b *testing.B) {
ctx, expire, key, value := getBenchInput()
if err := cache.Save(ctx, key, value, expire); err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := cache.TTL(ctx, key); err != nil {
b.Error(err)
}
}
})
}
}
func benchStatsSequential(cache xcache.Cache) func(b *testing.B) {
return func(b *testing.B) {
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
if _, err := cache.Stats(ctx); err != nil {
b.Error(err)
}
}
}
}
func benchStatsParallel(cache xcache.Cache) func(b *testing.B) {
return func(b *testing.B) {
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := cache.Stats(ctx); err != nil {
b.Error(err)
}
}
})
}
}
func getBenchInput() (context.Context, time.Duration, string, []byte) {
return context.Background(), 3 * time.Minute, "xcache_bench_key", []byte("benchmark")
}