Skip to content

Commit a0f4739

Browse files
committed
Add cache benchmark
Signed-off-by: David Kröll <[email protected]>
1 parent 67b6b48 commit a0f4739

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

cache/cache_bench_test.go

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package cache
2+
3+
import (
4+
"math"
5+
"testing"
6+
)
7+
8+
var (
9+
testByteArray64 = [64]byte{0xde, 0xad, 0xbe, 0xef}
10+
testByteArray8 = [...]byte{1, 2, 3, 4, 5, 6, 7, 8}
11+
testByteSlice8 = []byte{1, 2, 3, 4, 5, 6, 7, 8}
12+
)
13+
14+
const (
15+
testString = "abcdefghijklmnopqrstuvwxyz"
16+
testInt = 0xdeadbeef
17+
testFloat = float64(math.Pi)
18+
)
19+
20+
type sampleData struct {
21+
bytes [64]byte
22+
boolean bool
23+
integer int
24+
decimal float64
25+
}
26+
27+
func BenchmarkCache_SetInt(b *testing.B) {
28+
c := New(DefaultConfig)
29+
30+
for i := 0; i < b.N; i++ {
31+
c.Set(string(b.N), testInt, true)
32+
}
33+
}
34+
35+
func BenchmarkCache_SetInt64(b *testing.B) {
36+
c := New(DefaultConfig)
37+
38+
for i := 0; i < b.N; i++ {
39+
c.Set(string(b.N), int64(testInt), true)
40+
}
41+
}
42+
43+
func BenchmarkCache_SetFloat64(b *testing.B) {
44+
c := New(DefaultConfig)
45+
46+
for i := 0; i < b.N; i++ {
47+
c.Set(string(b.N), testFloat, true)
48+
}
49+
}
50+
51+
func BenchmarkCache_SetString(b *testing.B) {
52+
c := New(DefaultConfig)
53+
54+
for i := 0; i < b.N; i++ {
55+
c.Set(string(b.N), testString, true)
56+
}
57+
}
58+
59+
func BenchmarkCache_SetByteSlice(b *testing.B) {
60+
c := New(DefaultConfig)
61+
62+
for i := 0; i < b.N; i++ {
63+
c.Set(string(b.N), testByteSlice8, true)
64+
}
65+
}
66+
67+
func BenchmarkCache_SetByteArray(b *testing.B) {
68+
c := New(DefaultConfig)
69+
70+
for i := 0; i < b.N; i++ {
71+
c.Set(string(b.N), testByteArray8, true)
72+
}
73+
}
74+
75+
func BenchmarkCache_SetStruct(b *testing.B) {
76+
c := New(DefaultConfig)
77+
78+
for i := 0; i < b.N; i++ {
79+
s := sampleData{
80+
bytes: testByteArray64,
81+
boolean: true,
82+
integer: testInt,
83+
decimal: math.Pi,
84+
}
85+
c.Set(string(b.N), s, true)
86+
}
87+
}
88+
89+
func BenchmarkCache_SetStructPointer(b *testing.B) {
90+
c := New(DefaultConfig)
91+
92+
for i := 0; i < b.N; i++ {
93+
s := sampleData{
94+
bytes: testByteArray64,
95+
boolean: true,
96+
integer: testInt,
97+
decimal: math.Pi,
98+
}
99+
c.Set(string(b.N), &s, true)
100+
}
101+
}
102+
103+
func BenchmarkCache_Get(b *testing.B) {
104+
c := New(DefaultConfig)
105+
106+
s := sampleData{
107+
bytes: [64]byte{0xde, 0xad, 0xbe, 0xef},
108+
boolean: true,
109+
integer: 0xdeadbeef,
110+
decimal: math.Pi,
111+
}
112+
c.Set("id", s, true)
113+
114+
for i := 0; i < b.N; i++ {
115+
_, ok := c.Get("id")
116+
if !ok {
117+
b.Fatal("Cache miss")
118+
}
119+
}
120+
}

0 commit comments

Comments
 (0)