tests/sims/cachepolicies.test.ts
The automated tests for the /simulators/cache-eviction simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.
- LRU, FIFO and the optimal policy score the same hits as small independent versions, on four workloads and three cache sizes.
- No policy ever holds more keys than the cache size or beats the optimal policy.
// /simulators/cache-eviction: LRU, FIFO, LFU, CLOCK, 2Q, ARC, SIEVE and Bélády's optimal policy.
import { test } from 'node:test';
import { POLICIES, WORKLOADS, trace, run, make } from '../../src/lib/sims/cachePolicies.ts';
import { checker } from './check.ts';
const cases = [20, 100, 200].flatMap((c) => WORKLOADS.flatMap((w) => [1, 2, 3].map((seed) => ({ c, w: w.id, seed, t: trace(w.id, c, 10000, seed) }))));
function refLru(c: number, t: number[]) {
const m = new Map<number, true>();
let hits = 0;
for (const k of t) {
if (m.has(k)) { hits++; m.delete(k); }
else if (m.size >= c) m.delete(m.keys().next().value!);
m.set(k, true);
}
return hits;
}
function refFifo(c: number, t: number[]) {
const q: number[] = [], s = new Set<number>();
let hits = 0;
for (const k of t) {
if (s.has(k)) { hits++; continue; }
if (q.length >= c) s.delete(q.shift()!);
q.push(k); s.add(k);
}
return hits;
}
// Bélády: on a miss with a full cache, evict the key whose next use is farthest away (or never)
function refOpt(c: number, t: number[]) {
const next = new Array(t.length), last = new Map<number, number>();
for (let i = t.length - 1; i >= 0; i--) { next[i] = last.get(t[i]) ?? Infinity; last.set(t[i], i); }
const nextUse = new Map<number, number>();
let hits = 0;
t.forEach((k, i) => {
if (nextUse.has(k)) hits++;
else if (nextUse.size >= c) {
let far = -1, victim = -1;
for (const [key, at] of nextUse) if (at > far) { far = at; victim = key; }
nextUse.delete(victim);
}
nextUse.set(k, next[i]);
});
return hits;
}
test('LRU, FIFO and the optimal policy score the same hits as small independent versions, on four workloads and three cache sizes', () => {
const { ok, done } = checker();
for (const { c, w, seed, t } of cases) {
ok(run('LRU', c, t).hits === refLru(c, t), `LRU ${w} c=${c} seed ${seed}`);
ok(run('FIFO', c, t).hits === refFifo(c, t), `FIFO ${w} c=${c} seed ${seed}`);
ok(run('OPT', c, t).hits === refOpt(c, t), `OPT ${w} c=${c} seed ${seed}: ${run('OPT', c, t).hits} vs ${refOpt(c, t)}`);
}
done();
});
test('no policy ever holds more keys than the cache size or beats the optimal policy', () => {
const { ok, done } = checker();
for (const { c, w, seed, t } of cases) {
const opt = run('OPT', c, t).hits;
for (const p of POLICIES) {
const pol = make(p.id, c, t);
let hits = 0, over = false;
for (const k of t) { if (pol.access(k)) hits++; if (pol.size() > c) over = true; }
ok(!over, `${p.id} went over ${c} keys on ${w}`);
ok(hits === run(p.id, c, t).hits, `${p.id}: step-by-step and batch runs disagree`);
ok(hits <= opt, `${p.id} beat OPT on ${w} c=${c} seed ${seed}: ${hits} > ${opt}`);
}
}
done();
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.