| 1 |
# bench — comparison benchmarks and trace replay |
| 2 |
|
| 3 |
This directory is a separate Go module (`github.com/opencoff/go-sieve/bench`) |
| 4 |
that benchmarks go-sieve against hashicorp/golang-lru (LRU and ARC). It uses |
| 5 |
a `replace` directive to point at the parent directory, so changes to `../sieve.go` |
| 6 |
are picked up immediately without publishing. |
| 7 |
|
| 8 |
## Contents |
| 9 |
|
| 10 |
| File | Build tag | Purpose | |
| 11 |
|------|-----------|---------| |
| 12 |
| `doc.go` | (none) | Stub `package bench` declaration; always present. | |
| 13 |
| `bench_test.go` | `!trace` | Synthetic micro-benchmarks: parallel Get/Add/Probe/Delete/Mixed, memory footprint, GC impact. Compares Sieve vs LRU vs ARC. | |
| 14 |
| `trace.go` | `trace` | Trace file parsers: `LoadCSV` (Twitter, Meta CDN) and `LoadOracleGeneral` (mmap-based binary parser). | |
| 15 |
| `trace_test.go` | `trace` | Smoke tests that load each trace format and print request count / unique keys. | |
| 16 |
| `replay_test.go` | `trace` | Trace-replay harness: `TestMissRatio`, `BenchmarkReplay`, `BenchmarkParallelGet`, `BenchmarkParallelReplay`, `TestGCPressure`. | |
| 17 |
| `fetch-traces.sh` | — | Downloads and decompresses trace datasets (see below). | |
| 18 |
| `trace-bench-design.md` | — | Design document for the SIEVE-k extension and trace benchmarks. | |
| 19 |
| `cmd/mktables/` | — | Regenerates every markdown results table from the raw output files. | |
| 20 |
| `results/` | — | Saved benchmark output (metadata-stamped) for benchstat and mktables. | |
| 21 |
|
| 22 |
The `!trace` / `trace` tags are mutually exclusive: `go test -bench=.` without |
| 23 |
`-tags=trace` picks up only the synth benchmarks; with `-tags=trace`, only the |
| 24 |
trace benchmarks + tests. The Makefile leverages this so no `-bench=FILTER` |
| 25 |
regex is needed anywhere. |
| 26 |
|
| 27 |
## API asymmetry: SIEVE replay uses Probe, LRU/ARC use Get+Add |
| 28 |
|
| 29 |
The trace-replay harness and `BenchmarkProbe_Parallel` use `sieve.Probe()` |
| 30 |
for SIEVE — a single call that inserts on miss and marks the visited bit |
| 31 |
on hit. It's the idiomatic API for the get-or-insert pattern a trace |
| 32 |
replay exercises, and it preserves SIEVE's promotion semantics exactly |
| 33 |
(both `Get` and `Probe` call `LockAndMark` on hit). |
| 34 |
|
| 35 |
LRU and ARC use the `Get` + `Add`-on-miss idiom. Their superficial |
| 36 |
lookalikes are **not** semantic equivalents: |
| 37 |
|
| 38 |
| Method | Promotes recency on hit? | |
| 39 |
|--------|:-:| |
| 40 |
| `lru.Cache.ContainsOrAdd` | No — uses `Contains` | |
| 41 |
| `lru.Cache.PeekOrAdd` | No — uses `Peek` | |
| 42 |
| `arc.ARCCache.*OrAdd` | Does not exist | |
| 43 |
|
| 44 |
Both LRU lookalikes deliberately skip recency promotion on the hit path. |
| 45 |
Using them in a replay harness would corrupt LRU's eviction order (items |
| 46 |
would never get re-promoted) and inflate its miss ratio. The honest |
| 47 |
comparison therefore uses each library's idiomatic pattern: |
| 48 |
|
| 49 |
- **SIEVE** — `Probe(k, v)` (one call, TOCTOU-safe) |
| 50 |
- **LRU/ARC** — `Get(k)` fast path, fallback to `Add(k, v)` on miss |
| 51 |
|
| 52 |
Miss ratios remain comparable because the sequence of nodes visited and |
| 53 |
evicted is identical; only the SIEVE call path collapses into one |
| 54 |
function invocation. |
| 55 |
|
| 56 |
## Trace Datasets |
| 57 |
|
| 58 |
All benchmarks replay real-world cache access traces from published research |
| 59 |
datasets. Trace files live in `../data/` (gitignored) and are loaded at test |
| 60 |
time via mmap (oracleGeneral) or buffered I/O (CSV). Benchmarks skip |
| 61 |
gracefully when trace files are absent. |
| 62 |
|
| 63 |
### Sources |
| 64 |
|
| 65 |
We use traces from the [CacheLib / libCacheSim](https://cachelib.org/) trace |
| 66 |
repository, hosted on S3 at `s3://cache-datasets/`. |
| 67 |
|
| 68 |
| Dataset | Format | Records | Source | |
| 69 |
|---------|--------|---------|--------| |
| 70 |
| MSR Cambridge 2007 | oracleGeneral | 14 volumes, 3.9M–45.7M requests each | Enterprise block I/O (file servers, web, proxy, print) | |
| 71 |
| Meta Storage 2022 (Tectonic) | oracleGeneral | 5 block traces, 13–14M requests each | Distributed storage block I/O | |
| 72 |
|
| 73 |
**oracleGeneral** is a packed binary format (24 bytes/record, little-endian): |
| 74 |
|
| 75 |
| Offset | Type | Field | |
| 76 |
|--------|------|-------| |
| 77 |
| 0 | uint32 | timestamp | |
| 78 |
| 4 | uint64 | obj_id (cache key) | |
| 79 |
| 12 | uint32 | obj_size | |
| 80 |
| 16 | int64 | next_access_vtime | |
| 81 |
|
| 82 |
The parser (`LoadOracleGeneral` in `trace.go`) mmaps the file and extracts |
| 83 |
`obj_id` from each record. Unique key count is computed during load. |
| 84 |
|
| 85 |
### Downloading Traces |
| 86 |
|
| 87 |
```bash |
| 88 |
cd bench |
| 89 |
bash fetch-traces.sh |
| 90 |
``` |
| 91 |
|
| 92 |
The script downloads from the S3 bucket, decompresses `.zst` files with |
| 93 |
`zstd`, and places them under `../data/`: |
| 94 |
|
| 95 |
``` |
| 96 |
../data/ |
| 97 |
├── meta_storage/ |
| 98 |
│ └── block_traces_{1..5}.oracleGeneral.bin |
| 99 |
└── msr_2007/ |
| 100 |
├── msr_hm_0.oracleGeneral |
| 101 |
├── msr_prn_{0,1}.oracleGeneral |
| 102 |
├── msr_proj_{0,1,2,4}.oracleGeneral |
| 103 |
├── msr_prxy_{0,1}.oracleGeneral |
| 104 |
├── msr_src1_{0,1}.oracleGeneral |
| 105 |
├── msr_usr_{1,2}.oracleGeneral |
| 106 |
└── msr_web_2.oracleGeneral |
| 107 |
``` |
| 108 |
|
| 109 |
Total disk: ~11 GB decompressed. `msr_prxy_1` (3.8 GB) is the largest single |
| 110 |
file and is skipped by default in benchmarks (>2 GB threshold). |
| 111 |
|
| 112 |
Prerequisites: `zstd` (`brew install zstd`), `curl` or `wget`. |
| 113 |
|
| 114 |
## What We Measured |
| 115 |
|
| 116 |
### Trace-driven (`-tags=trace`, requires `../data/`) |
| 117 |
|
| 118 |
#### 1. Miss Ratio (`TestMissRatio`) |
| 119 |
|
| 120 |
For each trace, we create a cache sized at **10% of unique keys** and replay |
| 121 |
every request sequentially. SIEVE uses `Probe()`; LRU/ARC use `Get+Add`. |
| 122 |
We compare five cache variants: |
| 123 |
|
| 124 |
- **SIEVE k=1** — classic single-bit visited flag |
| 125 |
- **SIEVE k=2** — 2-bit saturating counter (survives 2 eviction passes) |
| 126 |
- **SIEVE k=3** — 3-level saturating counter |
| 127 |
- **LRU** — hashicorp/golang-lru |
| 128 |
- **ARC** — hashicorp/golang-lru/arc (adaptive replacement cache) |
| 129 |
|
| 130 |
#### 2. Sequential Replay Throughput (`BenchmarkReplay`) |
| 131 |
|
| 132 |
Same replay loop as miss ratio, but measured as a Go benchmark with |
| 133 |
`-benchmem`. Reports ns/op, bytes/op, allocs/op, and miss ratio per |
| 134 |
iteration. Exercises the full get-or-insert + eviction path. |
| 135 |
|
| 136 |
#### 3. Parallel Get Throughput (`BenchmarkParallelGet`) |
| 137 |
|
| 138 |
Pre-warms the cache with a full replay, then hammers `Get()` from |
| 139 |
`GOMAXPROCS` goroutines using `b.RunParallel`. Isolates the |
| 140 |
lock-free read path — the headline number for concurrent read-heavy |
| 141 |
workloads where the cache is already warm. |
| 142 |
|
| 143 |
Each goroutine walks the trace as a ring buffer starting at a |
| 144 |
**staggered offset** (`staggerStart`): worker w of g starts at |
| 145 |
`w*(n/g)`. Without staggering, every goroutine would walk the trace |
| 146 |
from offset 0 in near-lockstep — each key requested by all goroutines |
| 147 |
almost simultaneously — which inflates hit ratios and understates |
| 148 |
contention relative to a real replay. |
| 149 |
|
| 150 |
#### 4. Parallel Replay Throughput (`BenchmarkParallelReplay`) |
| 151 |
|
| 152 |
Starts with a cold cache, no warmup. Goroutines hammer the trace |
| 153 |
through `Probe()` (SIEVE) or `Get+Add` (LRU/ARC) in parallel, again |
| 154 |
from staggered ring offsets. This is the complement to |
| 155 |
`BenchmarkParallelGet`: together they bracket the steady-state |
| 156 |
workload. `BenchmarkParallelGet` shows the warm-read ceiling; |
| 157 |
`BenchmarkParallelReplay` shows throughput when misses, writes, and |
| 158 |
evictions are still happening alongside reads. |
| 159 |
|
| 160 |
Note this is still not a faithful concurrent replay of the original |
| 161 |
trace — g goroutines each replay the full trace, so every request is |
| 162 |
issued g times in total. It is a defined, identical workload for all |
| 163 |
three caches; treat the numbers as comparative, not absolute. |
| 164 |
|
| 165 |
#### 5. GC Pressure (`TestGCPressure`) |
| 166 |
|
| 167 |
Replays a trace and measures `runtime.MemStats` deltas: NumGC, |
| 168 |
PauseTotalNs, TotalAlloc, HeapObjects. Shows the memory efficiency |
| 169 |
advantage of the array-backed design. |
| 170 |
|
| 171 |
### Synthetic (no trace tag, no data required) |
| 172 |
|
| 173 |
| Benchmark | SIEVE | LRU | ARC | Notes | |
| 174 |
|---|:-:|:-:|:-:|---| |
| 175 |
| `BenchmarkGet_Parallel` | yes | yes | yes | Warm cache, uniform random Get | |
| 176 |
| `BenchmarkAdd_Parallel` | yes | yes | yes | Random Add over 2x cache-size key range | |
| 177 |
| `BenchmarkProbe_Parallel` | yes | – | – | SIEVE only — see API asymmetry section | |
| 178 |
| `BenchmarkDelete_Parallel` | yes | yes | yes | Pre-fill 2x cache-size, then parallel Delete | |
| 179 |
| `BenchmarkMixed_Parallel` | yes | yes | yes | 60% Get / 30% Add / 10% Delete | |
| 180 |
| `BenchmarkZipf_Get_Parallel` | yes | yes | yes | Zipfian distribution, three skews | |
| 181 |
| `BenchmarkMemoryFootprint` | yes | yes | yes | HeapAlloc delta at 100k / 500k / 1M fill | |
| 182 |
| `BenchmarkGCImpact` | yes | yes | yes | GC pause at 1M entries under mixed workload | |
| 183 |
|
| 184 |
`BenchmarkProbe_Parallel` is SIEVE-only because LRU's `PeekOrAdd` / |
| 185 |
`ContainsOrAdd` skip recency promotion and are not semantic |
| 186 |
equivalents (see the asymmetry section above). ARC has no Probe-like |
| 187 |
method at all. |
| 188 |
|
| 189 |
## Running |
| 190 |
|
| 191 |
The Makefile is the canonical entry point — it uses the build-tag |
| 192 |
separation described above to run the right benchmark set for each |
| 193 |
target, with no `-bench=FILTER` regex anywhere. |
| 194 |
|
| 195 |
```bash |
| 196 |
cd bench |
| 197 |
|
| 198 |
# Synthetic comparison benchmarks (no trace data needed). |
| 199 |
# Writes results/synthetic.txt. |
| 200 |
make bench |
| 201 |
|
| 202 |
# Trace replay + miss ratio + GC pressure (requires ../data/). |
| 203 |
# Writes results/trace.txt. |
| 204 |
make trace |
| 205 |
|
| 206 |
# Core-scaling sweep: parallel benchmarks at increasing GOMAXPROCS. |
| 207 |
# Writes results/sweep.txt. Override the core list to match your machine: |
| 208 |
make sweep SWEEP_PROCS="1 2 4 8 10" |
| 209 |
|
| 210 |
# Compile-check (no Test* functions without trace tag, so effectively a |
| 211 |
# type-check pass). |
| 212 |
make test |
| 213 |
make race |
| 214 |
|
| 215 |
# Clean results. |
| 216 |
make clean |
| 217 |
``` |
| 218 |
|
| 219 |
From the repo root, there's also a top-level `Makefile` that cascades |
| 220 |
into `bench/`: |
| 221 |
|
| 222 |
```bash |
| 223 |
# From repo root: |
| 224 |
make test # parent tests + bench compile-check |
| 225 |
make race # parent race tests + bench race compile-check |
| 226 |
make bench # parent SIEVE regression benches + bench comparison benches |
| 227 |
make trace # trace replay (delegates to bench/) |
| 228 |
make all # everything |
| 229 |
``` |
| 230 |
|
| 231 |
### Running a subset |
| 232 |
|
| 233 |
If you want to filter to a single trace or benchmark for faster |
| 234 |
iteration, invoke `go test` directly — the Makefile deliberately does |
| 235 |
not offer filter flags: |
| 236 |
|
| 237 |
```bash |
| 238 |
cd bench |
| 239 |
|
| 240 |
# One trace: |
| 241 |
go test -tags=trace -bench='BenchmarkReplay/msr_2007/msr_hm_0/' \ |
| 242 |
-benchmem -count=3 |
| 243 |
|
| 244 |
# One synth benchmark: |
| 245 |
go test -bench=BenchmarkProbe_Parallel -benchmem -count=6 |
| 246 |
``` |
| 247 |
|
| 248 |
### benchstat |
| 249 |
|
| 250 |
```bash |
| 251 |
# Save a baseline, make changes, compare: |
| 252 |
cp results/synthetic.txt results/baseline.txt |
| 253 |
# ... edit code ... |
| 254 |
make bench |
| 255 |
make compare OLD=results/baseline.txt NEW=results/synthetic.txt |
| 256 |
``` |
| 257 |
|
| 258 |
### Regenerating the markdown tables |
| 259 |
|
| 260 |
Every results table in `../bench-results.md`, `../README.md`, and the |
| 261 |
headline summary below is generated by `cmd/mktables` from the raw |
| 262 |
results files — including derived claims like "k=1 beats LRU on N of M |
| 263 |
traces", which are computed, never counted by hand: |
| 264 |
|
| 265 |
```bash |
| 266 |
go run ./cmd/mktables -synth results/synthetic.txt -trace results/trace.txt |
| 267 |
``` |
| 268 |
|
| 269 |
Raw results files carry a `#`-prefixed metadata header (date, host, |
| 270 |
CPU, go version, git commit) written by the Makefile, so each file is |
| 271 |
self-describing. |
| 272 |
|
| 273 |
## Running With Your Own Traces |
| 274 |
|
| 275 |
The benchmark harness auto-discovers all `.oracleGeneral` and |
| 276 |
`.oracleGeneral.bin` files under `../data/`, recursively. To add your |
| 277 |
own trace: |
| 278 |
|
| 279 |
1. Convert to oracleGeneral format (24 bytes/record, little-endian — see |
| 280 |
table above). Many traces from the libCacheSim project are already in |
| 281 |
this format. |
| 282 |
2. Place the file anywhere under `../data/`, e.g. `../data/my_traces/workload.oracleGeneral`. |
| 283 |
3. Run benchmarks — it will appear automatically as a subtest named after |
| 284 |
its path relative to `data/`. |
| 285 |
|
| 286 |
For CSV traces, add a parse function in `trace.go` (see `ParseTwitter` for |
| 287 |
the pattern) and wire it into `replay_test.go`. |
| 288 |
|
| 289 |
## Results |
| 290 |
|
| 291 |
This file documents **methodology**. The numbers live in exactly two |
| 292 |
places, both generated by `cmd/mktables` from the raw results files: |
| 293 |
|
| 294 |
- [`../bench-results.md`](../bench-results.md) — machine config plus the |
| 295 |
full per-trace tables for every benchmark. |
| 296 |
- [`../README.md`](../README.md) — curated summary tables and the |
| 297 |
interpretation, including the explanation of why parallel ns/op |
| 298 |
numbers are aggregate throughput rather than per-operation latency |
| 299 |
("Reading the parallel ns/op numbers"). |
| 300 |
|
| 301 |
Keeping a single generated source for each number is deliberate: an |
| 302 |
earlier revision of this file hand-summarized the miss-ratio table and |
| 303 |
drifted out of agreement with the root README. |
| 304 |
|
| 305 |
Headlines from the current run (`mktables` output; Apple M4, 10 cores — |
| 306 |
see bench-results.md for full machine config): |
| 307 |
|
| 308 |
<!-- mktables:headlines --> |
| 309 |
- Warm parallel Get (SIEVE k=1): 2.24–12.20 ns/op across 18 traces; |
| 310 |
13x–55x faster than the better of LRU/ARC per trace |
| 311 |
- Cold parallel replay (SIEVE k=1): 37.4–274.7 ns/op across 18 traces; |
| 312 |
2.0x–6.1x faster than the better of LRU/ARC per trace |
| 313 |
- Miss ratio (computed): best-in-row ARC 11, LRU 5, SIEVE k-variants 2; |
| 314 |
head-to-head k=1 vs LRU: k=1 lower on 10 of 18, LRU on 8 |
| 315 |
- Memory during replay (block_traces_1): SIEVE 135 MB TotalAlloc vs |
| 316 |
LRU 418 MB, ARC 997 MB |
| 317 |
|