Debian Codegraph @

pkg/sievecache/benchmark_test.go

raw symbols SCIP
1 package sievecache
2
3 import (
4 "fmt"
5 "math/rand"
6 "strconv"
7 "testing"
8 )
9
10 // Fixed parameters for benchmarks
11 const (
12 benchCacheSize = 10000
13 benchKeySize = 100000 // Number of possible keys
14 benchWorkingSet = 20000 // Number of keys in working set
15 benchRandSeed = 42 // Fixed seed for reproducible benchmarks
16 benchShardCount = 16 // Number of shards for ShardedSieveCache
17 )
18
19 // generateKeys generates a set of keys for benchmarking
20 func generateKeys(count int) []string {
21 keys := make([]string, count)
22 for i := 0; i < count; i++ {
23 keys[i] = fmt.Sprintf("key-%d", i)
24 }
25 return keys
26 }
27
28 // zipfDistribution generates a set of keys following a Zipf distribution
29 // This simulates a realistic cache access pattern with frequently accessed hot keys
30 func zipfDistribution(keyCount, sampleCount int, rng *rand.Rand) []string {
31 zipf := rand.NewZipf(rng, 1.1, 1.0, uint64(keyCount-1))
32 samples := make([]string, sampleCount)
33
34 for i := 0; i < sampleCount; i++ {
35 keyIndex := zipf.Uint64()
36 samples[i] = fmt.Sprintf("key-%d", keyIndex)
37 }
38
39 return samples
40 }
41
42 // benchmarkInsert benchmarks the insertion of keys into a cache
43 func benchmarkInsert(b *testing.B, c Cache[string, int]) {
44 keys := generateKeys(benchKeySize)
45 b.ResetTimer()
46
47 for i := 0; i < b.N; i++ {
48 keyIndex := i % benchKeySize
49 c.Insert(keys[keyIndex], keyIndex)
50 }
51 }
52
53 // benchmarkGet benchmarks the retrieval of keys from a cache
54 func benchmarkGet(b *testing.B, c Cache[string, int]) {
55 // First, populate the cache
56 keys := generateKeys(benchKeySize)
57 for i := 0; i < benchCacheSize; i++ {
58 c.Insert(keys[i%benchKeySize], i)
59 }
60
61 // Create a deterministic RNG for reproducible results
62 rng := rand.New(rand.NewSource(benchRandSeed))
63
64 // Generate access patterns following a Zipf distribution
65 accessPatterns := zipfDistribution(benchKeySize, b.N, rng)
66
67 b.ResetTimer()
68 for i := 0; i < b.N; i++ {
69 c.Get(accessPatterns[i])
70 }
71 }
72
73 // benchmarkMixed benchmarks a mixed workload of inserts and gets
74 func benchmarkMixed(b *testing.B, c Cache[string, int]) {
75 // Generate a large set of keys
76 keys := generateKeys(benchKeySize)
77
78 // Create a deterministic RNG for reproducible results
79 rng := rand.New(rand.NewSource(benchRandSeed))
80
81 // Pre-populate cache
82 for i := 0; i < benchCacheSize/2; i++ {
83 c.Insert(keys[i%benchKeySize], i)
84 }
85
86 b.ResetTimer()
87
88 // Mixed workload with 80% reads and 20% writes
89 for i := 0; i < b.N; i++ {
90 if rng.Intn(100) < 80 {
91 // Get operation
92 keyIndex := rng.Intn(benchWorkingSet)
93 c.Get(keys[keyIndex])
94 } else {
95 // Insert operation
96 keyIndex := rng.Intn(benchKeySize)
97 c.Insert(keys[keyIndex], i)
98 }
99 }
100 }
101
102 // Define Cache interface for benchmarking
103 type Cache[K comparable, V any] interface {
104 Insert(key K, value V) bool
105 Get(key K) (V, bool)
106 }
107
108 // Benchmark the base SieveCache implementation
109 func BenchmarkSieveCache_Insert(b *testing.B) {
110 cache, _ := New[string, int](benchCacheSize)
111 benchmarkInsert(b, cache)
112 }
113
114 func BenchmarkSieveCache_Get(b *testing.B) {
115 cache, _ := New[string, int](benchCacheSize)
116 benchmarkGet(b, cache)
117 }
118
119 func BenchmarkSieveCache_Mixed(b *testing.B) {
120 cache, _ := New[string, int](benchCacheSize)
121 benchmarkMixed(b, cache)
122 }
123
124 // Benchmark the thread-safe SyncSieveCache implementation
125 func BenchmarkSyncSieveCache_Insert(b *testing.B) {
126 cache, _ := NewSync[string, int](benchCacheSize)
127 benchmarkInsert(b, cache)
128 }
129
130 func BenchmarkSyncSieveCache_Get(b *testing.B) {
131 cache, _ := NewSync[string, int](benchCacheSize)
132 benchmarkGet(b, cache)
133 }
134
135 func BenchmarkSyncSieveCache_Mixed(b *testing.B) {
136 cache, _ := NewSync[string, int](benchCacheSize)
137 benchmarkMixed(b, cache)
138 }
139
140 // Benchmark the sharded implementation
141 func BenchmarkShardedSieveCache_Insert(b *testing.B) {
142 cache, _ := NewShardedWithShards[string, int](benchCacheSize, benchShardCount)
143 benchmarkInsert(b, cache)
144 }
145
146 func BenchmarkShardedSieveCache_Get(b *testing.B) {
147 cache, _ := NewShardedWithShards[string, int](benchCacheSize, benchShardCount)
148 benchmarkGet(b, cache)
149 }
150
151 func BenchmarkShardedSieveCache_Mixed(b *testing.B) {
152 cache, _ := NewShardedWithShards[string, int](benchCacheSize, benchShardCount)
153 benchmarkMixed(b, cache)
154 }
155
156 // Benchmark the ShardedSieveCache with different numbers of shards
157 func BenchmarkShardCount(b *testing.B) {
158 shardCounts := []int{1, 2, 4, 8, 16, 32, 64}
159
160 for _, shards := range shardCounts {
161 b.Run(strconv.Itoa(shards), func(b *testing.B) {
162 cache, _ := NewShardedWithShards[string, int](benchCacheSize, shards)
163 benchmarkMixed(b, cache)
164 })
165 }
166 }
167
168 // Benchmark parallel access to ShardedSieveCache
169 func BenchmarkParallelAccess(b *testing.B) {
170 cache, _ := NewShardedWithShards[string, int](benchCacheSize, benchShardCount)
171 keys := generateKeys(benchKeySize)
172
173 // Pre-populate cache
174 for i := 0; i < benchCacheSize/2; i++ {
175 cache.Insert(keys[i%benchKeySize], i)
176 }
177
178 b.ResetTimer()
179 b.RunParallel(func(pb *testing.PB) {
180 // Each goroutine has its own RNG
181 rng := rand.New(rand.NewSource(benchRandSeed))
182
183 counter := 0
184 for pb.Next() {
185 counter++
186 if rng.Intn(100) < 80 {
187 // Get operation
188 keyIndex := rng.Intn(benchWorkingSet)
189 cache.Get(keys[keyIndex])
190 } else {
191 // Insert operation
192 keyIndex := rng.Intn(benchKeySize)
193 cache.Insert(keys[keyIndex], counter)
194 }
195 }
196 })
197 }