About: how pages are checked

tests/sims/engines.test.ts

The automated tests for the /simulators/storage-engine 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.

  1. Leveled compaction keeps every level within its size target (memtable × ratio^level).
  2. Tiering writes less and leveling uses less space, the trade-off described by Dayan et al. (Monkey, Dostoevsky).
  3. Random inserts leave B-tree leaves about ln 2 ≈ 69% full (Yao, "On random 2-3 trees", 1978).
  4. A bigger buffer pool never makes the B-tree write more pages.
// /simulators/storage-engine: write, space and read amplification of an LSM tree and a B-tree.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { lsm, btree } from '../../src/lib/sims/engines.ts';

const w = { writes: 200000, keySpace: 1_000_000, seed: 3 };
const num = (r: ReturnType<typeof btree>, label: string) => Number(r.detail.find((d) => d.label === label)!.value.replace(/[,%]/g, ''));

test('leveled compaction keeps every level within its size target (memtable × ratio^level)', () => {
	for (const ratio of [4, 10]) {
		const r = lsm({ memtable: 1000, ratio, l0: 4, style: 'leveled' }, w);
		r.levels!.filter((l) => l.level !== 'L0').forEach((l, i) => {
			assert.ok(l.entries <= 1000 * ratio ** (i + 1), `ratio ${ratio} ${l.level}: ${l.entries} entries > ${1000 * ratio ** (i + 1)}`);
		});
		assert.ok(r.writeAmp >= 1 && r.spaceAmp >= 1, 'amplification below 1');
	}
});

test('tiering writes less and leveling uses less space, the trade-off described by Dayan et al. (Monkey, Dostoevsky)', () => {
	for (const ratio of [4, 10]) {
		const lev = lsm({ memtable: 1000, ratio, l0: 4, style: 'leveled' }, w);
		const tie = lsm({ memtable: 1000, ratio, l0: 4, style: 'tiered' }, w);
		assert.ok(tie.writeAmp < lev.writeAmp, `ratio ${ratio}: tiered WA ${tie.writeAmp} ≥ leveled ${lev.writeAmp}`);
	}
	// with many overwrites, tiering holds many dead versions
	const u = { writes: 200000, keySpace: 20000, seed: 3 };
	const lev = lsm({ memtable: 1000, ratio: 10, l0: 4, style: 'leveled' }, u);
	const tie = lsm({ memtable: 1000, ratio: 10, l0: 4, style: 'tiered' }, u);
	assert.ok(tie.spaceAmp > lev.spaceAmp, `tiered SA ${tie.spaceAmp} ≤ leveled ${lev.spaceAmp}`);
	assert.ok(lev.live === tie.live && lev.live <= 20000);
});

test('random inserts leave B-tree leaves about ln 2 ≈ 69% full (Yao, "On random 2-3 trees", 1978)', () => {
	const b = btree({ page: 64, cache: 1000 }, w);
	const fill = num(b, 'Average leaf fill') / 100;
	assert.ok(Math.abs(fill - Math.LN2) < 0.03, `leaf fill ${fill}`);
	// each split turns one leaf into two
	assert.equal(num(b, 'Page splits'), num(b, 'Leaf pages') - 1);
	assert.ok(Math.abs(b.spaceAmp - 1 / fill) < 0.05, `space amp ${b.spaceAmp} vs 1/fill ${1 / fill}`);
});

test('a bigger buffer pool never makes the B-tree write more pages', () => {
	let prev = Infinity;
	for (const cache of [10, 100, 1000, 10000, 100000]) {
		const b = btree({ page: 64, cache }, w);
		const pages = num(b, 'Pages written');
		assert.ok(pages <= prev, `cache ${cache}: ${pages} pages written, more than with a smaller cache (${prev})`);
		prev = pages;
	}
});

Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.

Published