-
Notifications
You must be signed in to change notification settings - Fork 27
/
memorybackend_benchmark_test.go
88 lines (78 loc) · 1.89 KB
/
memorybackend_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 krakendrate
import (
"context"
"crypto/sha256"
"fmt"
"math/rand"
"testing"
"time"
)
func generateTestKeys(num, length int) []string {
res := make([]string, 0, num)
// we do not need crypto strength random number to test
r := rand.New(rand.NewSource(int64(num))) // skipcq: GSC-G404
h := sha256.New()
for i := 0; i < num; i++ {
fmt.Fprintf(h, "%x", r.Int())
res = append(res, fmt.Sprintf("%x", h.Sum(nil))[:length])
}
return res
}
type buildCounter struct {
counter int64
}
func (bc *buildCounter) builder() func() interface{} {
return func() interface{} {
bc.counter += 1
return bc.counter
}
}
func BenchmarkMemoryBackend(b *testing.B) {
ctx, cancel := context.WithCancel(context.Background())
ttl := time.Millisecond
maxNumToEvict := 1000000 // 1 million * (8 length + 4 ) ~ 12 megs ?
maxNumToKeep := 1000000 //
toEvict := generateTestKeys(maxNumToEvict, 8)
toKeep := generateTestKeys(maxNumToKeep, 8)
bc := buildCounter{}
bcf := bc.builder()
t := []struct {
keep int
evict int
}{
{keep: 100, evict: 100},
{keep: 100, evict: 10000},
{keep: 100, evict: 1000000},
{keep: 10000, evict: 100},
{keep: 10000, evict: 10000},
{keep: 10000, evict: 1000000},
}
for _, tc := range t {
b.Run(fmt.Sprintf("loads_%d_keep_%d_evict_%d", tc.keep*10+tc.evict, tc.keep, tc.evict), func(b *testing.B) {
mb := NewMemoryBackend(ctx, ttl)
for i := 0; i < b.N; i++ {
numMax := tc.keep
if tc.evict > tc.keep {
numMax = tc.evict
}
// load all one time
for k := 0; k < numMax; k++ {
if k < tc.keep {
mb.Load(toKeep[k], bcf)
}
if k < tc.evict {
mb.Load(toEvict[k], bcf)
}
}
// now keep loading the to keep 9 times
// it might or might not evict depending on the time it takes
for j := 0; j < 9; j++ {
for k := 0; k < tc.keep; k++ {
mb.Load(toKeep[k], bcf)
}
}
}
})
}
cancel()
}