About: how pages are checked

tests/sims/cpusched.test.ts

The automated tests for the /simulators/scheduler 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. The worked examples in Silberschatz's Operating System Concepts (chapter 5) give the book's average waiting times.
  2. On 2,000 random workloads, every process gets exactly its burst after it arrives, the CPU never idles with work waiting, and the stats add up.
  3. When every job arrives at once, shortest-job-first has the lowest average waiting time of all the algorithms.
// /simulators/scheduler: FCFS, SJF, SRTF, round robin and priority scheduling.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { schedule, ALGOS, type Algo, type Proc } from '../../src/lib/sims/cpuSched.ts';
import { checker, lcg } from './check.ts';

const P = (id: string, arrival: number, burst: number, priority = 0): Proc => ({ id, arrival, burst, priority });
const avgWait = (algo: Algo, procs: Proc[], q?: number) => schedule(algo, procs, q).avg.waiting;

test("the worked examples in Silberschatz's Operating System Concepts (chapter 5) give the book's average waiting times", () => {
	// FCFS, P1 24, P2 3, P3 3 arriving together in that order: (0 + 24 + 27) / 3 = 17
	assert.equal(avgWait('FCFS', [P('P1', 0, 24), P('P2', 0, 3), P('P3', 0, 3)]), 17);
	// the same jobs in the order P2, P3, P1: (6 + 0 + 3) / 3 = 3
	assert.equal(avgWait('FCFS', [P('P2', 0, 3), P('P3', 0, 3), P('P1', 0, 24)]), 3);
	// SJF, bursts 6, 8, 7, 3: (3 + 16 + 9 + 0) / 4 = 7
	assert.equal(avgWait('SJF', [P('P1', 0, 6), P('P2', 0, 8), P('P3', 0, 7), P('P4', 0, 3)]), 7);
	// preemptive SJF (SRTF), arrivals 0-3, bursts 8, 4, 9, 5: 26 / 4 = 6.5
	assert.equal(avgWait('SRTF', [P('P1', 0, 8), P('P2', 1, 4), P('P3', 2, 9), P('P4', 3, 5)]), 6.5);
	// round robin, quantum 4, P1 24, P2 3, P3 3: (6 + 4 + 7) / 3 = 5.66
	assert.ok(Math.abs(avgWait('RR', [P('P1', 0, 24), P('P2', 0, 3), P('P3', 0, 3)], 4) - 17 / 3) < 1e-9);
	// non-preemptive priority, lower number first: 41 / 5 = 8.2
	assert.ok(Math.abs(avgWait('PRIO', [P('P1', 0, 10, 3), P('P2', 0, 1, 1), P('P3', 0, 2, 4), P('P4', 0, 1, 5), P('P5', 0, 5, 2)]) - 8.2) < 1e-9);
});

function randomProcs(r: () => number, sameArrival = false): Proc[] {
	const n = 1 + Math.floor(r() * 7);
	return Array.from({ length: n }, (_, i) => P(`P${i + 1}`, sameArrival ? 0 : Math.floor(r() * 12), 1 + Math.floor(r() * 9), 1 + Math.floor(r() * 5)));
}

test('on 2,000 random workloads, every process gets exactly its burst after it arrives, the CPU never idles with work waiting, and the stats add up', () => {
	const { ok, done } = checker();
	const r = lcg(16);
	for (let k = 0; k < 2000; k++) {
		const procs = randomProcs(r), q = 1 + Math.floor(r() * 4);
		for (const { id: algo, preemptive } of ALGOS) {
			const res = schedule(algo, procs, q);
			for (const p of procs) {
				const ts = res.slots.filter((s) => s.run === p.id).map((s) => s.t);
				const st = res.stats.find((s) => s.id === p.id)!;
				ok(ts.length === p.burst, `${algo}: ${p.id} ran ${ts.length} of ${p.burst}`);
				ok(ts.every((t) => t >= p.arrival), `${algo}: ${p.id} ran before it arrived`);
				ok(st.completion === Math.max(...ts) + 1 && st.turnaround === st.completion - p.arrival && st.waiting === st.turnaround - p.burst && st.response === Math.min(...ts) - p.arrival, `${algo}: ${p.id} stats do not add up`);
				if (!preemptive) ok(Math.max(...ts) - Math.min(...ts) + 1 === p.burst, `${algo} is non-preemptive but ${p.id} was interrupted`);
			}
			for (const s of res.slots) if (s.run === null) {
				const waiting = procs.some((p) => p.arrival <= s.t && res.slots.some((x) => x.run === p.id && x.t > s.t));
				ok(!waiting, `${algo}: CPU idle at ${s.t} with work waiting`);
			}
		}
	}
	done();
});

test('when every job arrives at once, shortest-job-first has the lowest average waiting time of all the algorithms', () => {
	const { ok, done } = checker();
	const r = lcg(17);
	for (let k = 0; k < 2000; k++) {
		const procs = randomProcs(r, true), sjf = avgWait('SJF', procs);
		for (const { id } of ALGOS) ok(sjf <= avgWait(id, procs, 2) + 1e-9, `${id} beat SJF on ${JSON.stringify(procs)}`);
	}
	done();
});

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

Published