| 1 |
package sievecache |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math/rand" |
| 6 |
"strconv" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
const ( |
| 12 |
benchCacheSize = 10000 |
| 13 |
benchKeySize = 100000 |
| 14 |
benchWorkingSet = 20000 |
| 15 |
benchRandSeed = 42 |
| 16 |
benchShardCount = 16 |
| 17 |
) |
| 18 |
|
| 19 |
|
| 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 |
|
| 29 |
|
| 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 |
|
| 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 |
|
| 54 |
func benchmarkGet(b *testing.B, c Cache[string, int]) { |
| 55 |
|
| 56 |
keys := generateKeys(benchKeySize) |
| 57 |
for i := 0; i < benchCacheSize; i++ { |
| 58 |
c.Insert(keys[i%benchKeySize], i) |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
rng := rand.New(rand.NewSource(benchRandSeed)) |
| 63 |
|
| 64 |
|
| 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 |
|
| 74 |
func benchmarkMixed(b *testing.B, c Cache[string, int]) { |
| 75 |
|
| 76 |
keys := generateKeys(benchKeySize) |
| 77 |
|
| 78 |
|
| 79 |
rng := rand.New(rand.NewSource(benchRandSeed)) |
| 80 |
|
| 81 |
|
| 82 |
for i := 0; i < benchCacheSize/2; i++ { |
| 83 |
c.Insert(keys[i%benchKeySize], i) |
| 84 |
} |
| 85 |
|
| 86 |
b.ResetTimer() |
| 87 |
|
| 88 |
|
| 89 |
for i := 0; i < b.N; i++ { |
| 90 |
if rng.Intn(100) < 80 { |
| 91 |
|
| 92 |
keyIndex := rng.Intn(benchWorkingSet) |
| 93 |
c.Get(keys[keyIndex]) |
| 94 |
} else { |
| 95 |
|
| 96 |
keyIndex := rng.Intn(benchKeySize) |
| 97 |
c.Insert(keys[keyIndex], i) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
type Cache[K comparable, V any] interface { |
| 104 |
Insert(key K, value V) bool |
| 105 |
Get(key K) (V, bool) |
| 106 |
} |
| 107 |
|
| 108 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 169 |
func BenchmarkParallelAccess(b *testing.B) { |
| 170 |
cache, _ := NewShardedWithShards[string, int](benchCacheSize, benchShardCount) |
| 171 |
keys := generateKeys(benchKeySize) |
| 172 |
|
| 173 |
|
| 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 |
|
| 181 |
rng := rand.New(rand.NewSource(benchRandSeed)) |
| 182 |
|
| 183 |
counter := 0 |
| 184 |
for pb.Next() { |
| 185 |
counter++ |
| 186 |
if rng.Intn(100) < 80 { |
| 187 |
|
| 188 |
keyIndex := rng.Intn(benchWorkingSet) |
| 189 |
cache.Get(keys[keyIndex]) |
| 190 |
} else { |
| 191 |
|
| 192 |
keyIndex := rng.Intn(benchKeySize) |
| 193 |
cache.Insert(keys[keyIndex], counter) |
| 194 |
} |
| 195 |
} |
| 196 |
}) |
| 197 |
} |