tests/sims/s3rate.test.ts
The automated tests for the /simulators/s3-prefix-sharding 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.
- The per-prefix limits are the ones AWS publishes: 3,500 writes and 5,500 reads a second.
- Load under the limits is never throttled.
- Without retries, one prefix serves exactly its limit and the rest fail.
- With retries, served requests never exceed the limit and none are created or lost.
- One hot prefix is throttled even when the others have room.
// /simulators/s3-prefix-sharding: S3's per-prefix request rates (3,500 PUT/COPY/POST/DELETE and 5,500 GET/HEAD per second).
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { simulate, PUT_RATE, GET_RATE } from '../../src/lib/sims/s3rate.ts';
const base = { put: 10000, get: 20000, attempts: 3, baseMs: 100, capMs: 20000, seconds: 60 };
test('the per-prefix limits are the ones AWS publishes: 3,500 writes and 5,500 reads a second', () => {
assert.equal(PUT_RATE, 3500);
assert.equal(GET_RATE, 5500);
});
test('load under the limits is never throttled', () => {
assert.equal(simulate({ ...base, put: 3000, get: 5000, prefixes: 1, hot: 1 }).slowdown, 0);
assert.equal(simulate({ ...base, prefixes: 10, hot: 0.1 }).slowdown, 0); // ten even prefixes: 1,000 PUT and 2,000 GET each
});
test('without retries, one prefix serves exactly its limit and the rest fail', () => {
const r = simulate({ ...base, prefixes: 1, hot: 1, attempts: 1 });
assert.ok(Math.abs(r.ok / 60 - (3500 + 5500)) < 1e-6, `ok/s ${r.ok / 60}`);
assert.ok(Math.abs(r.failed / 60 - (10000 - 3500 + 20000 - 5500)) < 1e-6, `failed/s ${r.failed / 60}`);
});
test('with retries, served requests never exceed the limit and none are created or lost', () => {
const c = simulate({ ...base, prefixes: 1, hot: 1 });
assert.ok(c.ok / 60 <= 9000 + 1e-6, `ok/s ${c.ok / 60}`);
assert.ok(c.ok + c.failed <= c.requests + 1e-6, `${c.ok} + ${c.failed} > ${c.requests}`);
for (const s of c.perSecond) assert.ok(s.ok <= 9000 + 1e-6, `second ${s.t}: ${s.ok}`);
assert.ok(c.attemptsPerRequest > 1 && c.attemptsPerRequest <= 3);
});
test('one hot prefix is throttled even when the others have room', () => {
// 40% of 20,000 GET/s is 8,000 on one prefix, over its 5,500
const r = simulate({ ...base, put: 0, get: 20000, prefixes: 40, hot: 0.4, attempts: 1 });
assert.ok(Math.abs(r.failed / 60 - (8000 - 5500)) < 1e-6, `failed/s ${r.failed / 60}`);
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.