| 1 |
// zipf_bench_test.go — Zipfian synthetic benchmarks for slotState and Sieve |
| 2 |
// |
| 3 |
// (c) 2024 Sudhi Herle <[email protected]> |
| 4 |
// |
| 5 |
// Copyright 2024- Sudhi Herle <sw-at-herle-dot-net> |
| 6 |
// License: BSD-2-Clause |
| 7 |
// |
| 8 |
// If you need a commercial license for this work, please contact |
| 9 |
// the author. |
| 10 |
// |
| 11 |
// This software does not come with any express or implied |
| 12 |
// warranty; it is provided "as is". No claim is made to its |
| 13 |
// suitability for any purpose. |
| 14 |
|
| 15 |
package sieve |
| 16 |
|
| 17 |
import ( |
| 18 |
"math/rand" |
| 19 |
"testing" |
| 20 |
) |
| 21 |
|
| 22 |
// ========================================================================= |
| 23 |
// Helpers |
| 24 |
// ========================================================================= |
| 25 |
|
| 26 |
const ( |
| 27 |
_ZipfSeqLen = 256 << 10 // 256K samples per sequence |
| 28 |
_ZipfCacheSize = 8192 |
| 29 |
_ZipfKeyRange = _ZipfCacheSize * 2 // half the keys won't fit in cache |
| 30 |
) |
| 31 |
|
| 32 |
var zipfSkews = []struct { |
| 33 |
name string |
| 34 |
s float64 |
| 35 |
}{ |
| 36 |
{"s=1.01", 1.01}, |
| 37 |
{"s=1.20", 1.2}, |
| 38 |
{"s=1.50", 1.5}, |
| 39 |
} |
| 40 |
|
| 41 |
// zipfIndices generates n indices from Zipf(s, v=1) over [0, keySpace). |
| 42 |
// Requires s > 1 (Go's rand.NewZipf constraint). |
| 43 |
func zipfIndices(n, keySpace int, s float64, seed int64) []int32 { |
| 44 |
r := rand.New(rand.NewSource(seed)) |
| 45 |
z := rand.NewZipf(r, s, 1.0, uint64(keySpace-1)) |
| 46 |
out := make([]int32, n) |
| 47 |
for i := range out { |
| 48 |
out[i] = int32(z.Uint64()) |
| 49 |
} |
| 50 |
return out |
| 51 |
} |
| 52 |
|
| 53 |
// shuffledCopy returns a new slice with the same elements as src, shuffled. |
| 54 |
func shuffledCopy(src []int32, seed int64) []int32 { |
| 55 |
dst := make([]int32, len(src)) |
| 56 |
copy(dst, src) |
| 57 |
r := rand.New(rand.NewSource(seed)) |
| 58 |
r.Shuffle(len(dst), func(i, j int) { |
| 59 |
dst[i], dst[j] = dst[j], dst[i] |
| 60 |
}) |
| 61 |
return dst |
| 62 |
} |
| 63 |
|
| 64 |
// ========================================================================= |
| 65 |
// slotState — Zipfian contention benchmarks |
| 66 |
// ========================================================================= |
| 67 |
|
| 68 |
// The Get() hot path: LockAndMark + Unlock under Zipfian contention, k=1. |
| 69 |
func BenchmarkSlotState_Zipf_K1(b *testing.B) { |
| 70 |
for _, sk := range zipfSkews { |
| 71 |
b.Run(sk.name, func(b *testing.B) { |
| 72 |
ss := newSlotState(_ZipfCacheSize, 1) |
| 73 |
seq := zipfIndices(_ZipfSeqLen, _ZipfCacheSize, sk.s, 42) |
| 74 |
|
| 75 |
b.ResetTimer() |
| 76 |
b.RunParallel(func(pb *testing.PB) { |
| 77 |
local := shuffledCopy(seq, rand.Int63()) |
| 78 |
n := len(local) |
| 79 |
i := 0 |
| 80 |
for pb.Next() { |
| 81 |
idx := local[i%n] |
| 82 |
ss.LockAndMark(idx) |
| 83 |
ss.Unlock(idx) |
| 84 |
i++ |
| 85 |
} |
| 86 |
}) |
| 87 |
}) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// Same as above but k=3 (Or + CAS path). |
| 92 |
func BenchmarkSlotState_Zipf_K3(b *testing.B) { |
| 93 |
for _, sk := range zipfSkews { |
| 94 |
b.Run(sk.name, func(b *testing.B) { |
| 95 |
ss := newSlotState(_ZipfCacheSize, 3) |
| 96 |
seq := zipfIndices(_ZipfSeqLen, _ZipfCacheSize, sk.s, 42) |
| 97 |
|
| 98 |
b.ResetTimer() |
| 99 |
b.RunParallel(func(pb *testing.PB) { |
| 100 |
local := shuffledCopy(seq, rand.Int63()) |
| 101 |
n := len(local) |
| 102 |
i := 0 |
| 103 |
for pb.Next() { |
| 104 |
idx := local[i%n] |
| 105 |
ss.LockAndMark(idx) |
| 106 |
ss.Unlock(idx) |
| 107 |
i++ |
| 108 |
} |
| 109 |
}) |
| 110 |
}) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
// 90% LockAndMark+Unlock (Get path), 10% Clear (eviction scan). |
| 115 |
func BenchmarkSlotState_Zipf_Mixed(b *testing.B) { |
| 116 |
for _, sk := range zipfSkews { |
| 117 |
b.Run(sk.name, func(b *testing.B) { |
| 118 |
ss := newSlotState(_ZipfCacheSize, 1) |
| 119 |
seq := zipfIndices(_ZipfSeqLen, _ZipfCacheSize, sk.s, 42) |
| 120 |
|
| 121 |
b.ResetTimer() |
| 122 |
b.RunParallel(func(pb *testing.PB) { |
| 123 |
local := shuffledCopy(seq, rand.Int63()) |
| 124 |
r := rand.New(rand.NewSource(rand.Int63())) |
| 125 |
n := len(local) |
| 126 |
i := 0 |
| 127 |
for pb.Next() { |
| 128 |
idx := local[i%n] |
| 129 |
if r.Intn(10) < 9 { |
| 130 |
ss.LockAndMark(idx) |
| 131 |
ss.Unlock(idx) |
| 132 |
} else { |
| 133 |
ss.Clear(idx) |
| 134 |
} |
| 135 |
i++ |
| 136 |
} |
| 137 |
}) |
| 138 |
}) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// ========================================================================= |
| 143 |
// Sieve — Zipfian benchmarks |
| 144 |
// ========================================================================= |
| 145 |
|
| 146 |
// Parallel Get on a warm cache. The cache is pre-warmed by replaying |
| 147 |
// the Zipfian sequence (Get-or-Add), so the working set is established |
| 148 |
// before timing starts. |
| 149 |
func BenchmarkSieve_Zipf_Get(b *testing.B) { |
| 150 |
for _, sk := range zipfSkews { |
| 151 |
b.Run(sk.name, func(b *testing.B) { |
| 152 |
c := Must(New[int, int](_ZipfCacheSize)) |
| 153 |
seq := zipfIndices(_ZipfSeqLen, _ZipfKeyRange, sk.s, 42) |
| 154 |
|
| 155 |
// Warm up: establish working set |
| 156 |
for _, idx := range seq { |
| 157 |
k := int(idx) |
| 158 |
if _, ok := c.Get(k); !ok { |
| 159 |
c.Add(k, k) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
b.ResetTimer() |
| 164 |
b.RunParallel(func(pb *testing.PB) { |
| 165 |
local := shuffledCopy(seq, rand.Int63()) |
| 166 |
n := len(local) |
| 167 |
i := 0 |
| 168 |
for pb.Next() { |
| 169 |
c.Get(int(local[i%n])) |
| 170 |
i++ |
| 171 |
} |
| 172 |
}) |
| 173 |
}) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// Parallel Get-or-Add: the steady-state cache pattern. |
| 178 |
// On miss, the key is added (possibly triggering eviction). |
| 179 |
func BenchmarkSieve_Zipf_GetOrAdd(b *testing.B) { |
| 180 |
for _, sk := range zipfSkews { |
| 181 |
b.Run(sk.name, func(b *testing.B) { |
| 182 |
c := Must(New[int, int](_ZipfCacheSize)) |
| 183 |
seq := zipfIndices(_ZipfSeqLen, _ZipfKeyRange, sk.s, 42) |
| 184 |
|
| 185 |
b.ResetTimer() |
| 186 |
b.RunParallel(func(pb *testing.PB) { |
| 187 |
local := shuffledCopy(seq, rand.Int63()) |
| 188 |
n := len(local) |
| 189 |
i := 0 |
| 190 |
for pb.Next() { |
| 191 |
k := int(local[i%n]) |
| 192 |
if _, ok := c.Get(k); !ok { |
| 193 |
c.Add(k, k) |
| 194 |
} |
| 195 |
i++ |
| 196 |
} |
| 197 |
}) |
| 198 |
}) |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
// Parallel Probe (insert-if-absent). Exercises the Probe-specific |
| 203 |
// code path which is distinct from Get+Add. |
| 204 |
func BenchmarkSieve_Zipf_Probe(b *testing.B) { |
| 205 |
for _, sk := range zipfSkews { |
| 206 |
b.Run(sk.name, func(b *testing.B) { |
| 207 |
c := Must(New[int, int](_ZipfCacheSize)) |
| 208 |
seq := zipfIndices(_ZipfSeqLen, _ZipfKeyRange, sk.s, 42) |
| 209 |
|
| 210 |
b.ResetTimer() |
| 211 |
b.RunParallel(func(pb *testing.PB) { |
| 212 |
local := shuffledCopy(seq, rand.Int63()) |
| 213 |
n := len(local) |
| 214 |
i := 0 |
| 215 |
for pb.Next() { |
| 216 |
k := int(local[i%n]) |
| 217 |
c.Probe(k, k) |
| 218 |
i++ |
| 219 |
} |
| 220 |
}) |
| 221 |
}) |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
// 60% Get, 30% Add, 10% Delete — the standard mixed workload. |
| 226 |
func BenchmarkSieve_Zipf_Mixed(b *testing.B) { |
| 227 |
for _, sk := range zipfSkews { |
| 228 |
b.Run(sk.name, func(b *testing.B) { |
| 229 |
c := Must(New[int, int](_ZipfCacheSize)) |
| 230 |
seq := zipfIndices(_ZipfSeqLen, _ZipfKeyRange, sk.s, 42) |
| 231 |
|
| 232 |
// Pre-fill so deletes have something to hit |
| 233 |
for i := 0; i < _ZipfCacheSize; i++ { |
| 234 |
c.Add(i, i) |
| 235 |
} |
| 236 |
|
| 237 |
b.ResetTimer() |
| 238 |
b.RunParallel(func(pb *testing.PB) { |
| 239 |
local := shuffledCopy(seq, rand.Int63()) |
| 240 |
r := rand.New(rand.NewSource(rand.Int63())) |
| 241 |
n := len(local) |
| 242 |
i := 0 |
| 243 |
for pb.Next() { |
| 244 |
k := int(local[i%n]) |
| 245 |
op := r.Intn(10) |
| 246 |
switch { |
| 247 |
case op < 6: |
| 248 |
c.Get(k) |
| 249 |
case op < 9: |
| 250 |
c.Add(k, k) |
| 251 |
default: |
| 252 |
c.Delete(k) |
| 253 |
} |
| 254 |
i++ |
| 255 |
} |
| 256 |
}) |
| 257 |
}) |
| 258 |
} |
| 259 |
} |