About: how pages are checked

tests/sims/loadbalancer.test.ts

The automated tests for the /simulators/load-balancer 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. With random routing each server is an M/M/4 queue, and the mean response time is within 3% of Erlang's C formula.
  2. No algorithm beats one shared M/M/16 queue, least connections comes closest, and random is the slowest.
  3. With health checks a stopped server is taken out of rotation and errors stop; without them round robin keeps sending it a quarter of the traffic.
// /simulators/load-balancer: round robin, random, least connections and power of two choices, against queueing theory.
import { test } from 'node:test';
import { LBSim, ALGOS, NOMINAL, S, SLOTS, MEAN } from '../../src/lib/sims/loadBalancer.ts';
import { checker } from './check.ts';

const mean = (a: number[]) => a.reduce((x, y) => x + y, 0) / a.length;
// mean response time of an M/M/c queue from Erlang's C formula, written out here rather than taken from the model
function erlangW(lam: number, mu: number, c: number) {
	const a = lam / mu, rho = a / c;
	let sum = 0, term = 1;
	for (let k = 0; k < c; k++) { if (k) term *= a / k; sum += term; }
	const top = (term * a) / c / (1 - rho);
	return top / (sum + top) / (c * mu - lam) + 1 / mu;
}
function measured(load: number) {
	const res: Record<string, number[]> = {};
	for (let seed = 1; seed <= 4; seed++) {
		const s = new LBSim({ load, keepAll: true, seed });
		s.runUntil(50);
		for (const lb of s.lbs) lb.all = [];
		s.runUntil(1050);
		for (const lb of s.lbs) (res[lb.id] ??= []).push(mean(lb.all));
	}
	return Object.fromEntries(ALGOS.map((a) => [a.id, mean(res[a.id])]));
}

test("with random routing each server is an M/M/4 queue, and the mean response time is within 3% of Erlang's C formula", () => {
	const { ok, done } = checker();
	for (const load of [0.5, 0.8]) {
		const m = measured(load), want = erlangW((load * NOMINAL) / S, 1 / MEAN, SLOTS);
		ok(Math.abs(m.random / want - 1) < 0.03, `load ${load}: ${(m.random * 1000).toFixed(1)} ms, Erlang C ${(want * 1000).toFixed(1)} ms`);
	}
	done();
});

test('no algorithm beats one shared M/M/16 queue, least connections comes closest, and random is the slowest', () => {
	const { ok, done } = checker();
	for (const load of [0.5, 0.8]) {
		const m = measured(load), bound = erlangW(load * NOMINAL, 1 / MEAN, S * SLOTS);
		for (const a of ALGOS) ok(m[a.id] >= bound * 0.98, `load ${load}: ${a.id} at ${(m[a.id] * 1000).toFixed(1)} ms beats the M/M/16 bound ${(bound * 1000).toFixed(1)} ms`);
		ok(m.lc <= m.p2c && m.p2c <= m.rr && m.rr <= m.random, `load ${load}: order ${JSON.stringify(m)}`);
	}
	done();
});

test('with health checks a stopped server is taken out of rotation and errors stop; without them round robin keeps sending it a quarter of the traffic', () => {
	const { ok, done } = checker();
	const on = new LBSim({ load: 0.8 });
	on.down = true;
	on.runUntil(30);
	for (const [i, st] of on.stats().entries()) ok(st.errs === 0, `${ALGOS[i].id}: ${st.errs} errors in the last 10 s with checks on`);
	const off = new LBSim({ load: 0.8 });
	off.down = true; off.checks = false;
	off.runUntil(30);
	const rr = off.stats()[ALGOS.findIndex((a) => a.id === 'rr')];
	ok(Math.abs(rr.share[3] - 0.25) < 0.02 && rr.errs > 0, `round robin without checks: ${(rr.share[3] * 100).toFixed(0)}% to the stopped server, ${rr.errs} errors`);
	done();
});

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

Published