tests/sims/hpa.test.ts
The automated tests for the /simulators/autoscaling 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 replica formula gives the Kubernetes docs results: double at 200% of target, halve at 50%, no change inside the 10% tolerance.
- Pods that are not ready count as 0% on a scale-up, and a pod that just became ready is set aside, as the docs describe.
- The default scale-up policy allows 4 pods or 100% per 15 s, whichever is more.
- The docs scale-down example (4 pods or 10% per minute, from 80) goes 80, 72, 64, and a 300 s window holds scale-down back.
- Steady load of 1,200m at a 50% target of 500m settles on 5 pods, and after a spike ends nothing scales down for 300 s.
// /simulators/autoscaling: the Kubernetes Horizontal Pod Autoscaler's replica formula, policies and stabilization.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { resourceReplicas, Controller, Cluster, DEFAULT_UP, type PodView } from '../../src/lib/sims/hpa.ts';
const pod = (i: number, v: number, extra: Partial<PodView> = {}): PodView => ({ name: 'p' + i, phase: 'Running', ready: true, start: -1000, transition: -990, metric: { value: v, timestamp: 0, window: 15 }, request: 100, ...extra });
test('the replica formula gives the Kubernetes docs results: double at 200% of target, halve at 50%, no change inside the 10% tolerance', () => {
assert.equal(resourceReplicas(0, [0, 1, 2, 3].map((i) => pod(i, 200)), 4, 100).replicas, 8);
assert.equal(resourceReplicas(0, [0, 1, 2, 3].map((i) => pod(i, 50)), 4, 100).replicas, 2);
assert.equal(resourceReplicas(0, [0, 1].map((i) => pod(i, 108)), 2, 100).replicas, 2);
assert.equal(resourceReplicas(0, [0, 1].map((i) => pod(i, 111)), 2, 100).replicas, 3);
});
test('pods that are not ready count as 0% on a scale-up, and a pod that just became ready is set aside, as the docs describe', () => {
const pending = (i: number) => pod(i, 0, { phase: 'Pending', metric: undefined });
assert.equal(resourceReplicas(0, [pod(0, 200), pod(1, 200), pending(2), pending(3)], 4, 100).replicas, 4);
assert.equal(resourceReplicas(100, [pod(0, 50), pod(1, 900, { start: 40, transition: 90, metric: { value: 900, timestamp: 100, window: 15 } })], 2, 100).ready, 1);
});
test('the default scale-up policy allows 4 pods or 100% per 15 s, whichever is more', () => {
assert.equal(new Controller().normalize(0, 1, 100, 1, 1000).final, 5);
const c = new Controller();
assert.equal(c.normalize(0, 10, 100, 1, 1000).final, 20);
assert.equal(c.normalize(10, 20, 100, 1, 1000).final, 20, 'no second step inside the same 15 s');
assert.equal(c.normalize(16, 20, 100, 1, 1000).final, 40);
});
test('the docs scale-down example (4 pods or 10% per minute, from 80) goes 80, 72, 64, and a 300 s window holds scale-down back', () => {
const c = new Controller(DEFAULT_UP, { stabilizationWindowSeconds: 0, selectPolicy: 'Max', policies: [{ type: 'Pods', value: 4, periodSeconds: 60 }, { type: 'Percent', value: 10, periodSeconds: 60 }] });
assert.equal(c.normalize(0, 80, 10, 1, 100).final, 72);
assert.equal(c.normalize(15, 72, 10, 1, 100).final, 72);
assert.equal(c.normalize(61, 72, 10, 1, 100).final, 64);
const d = new Controller();
d.normalize(0, 10, 10, 1, 100);
assert.equal(d.normalize(240, 10, 3, 1, 100).final, 10, 'inside the default 300 s window');
assert.equal(d.normalize(310, 10, 3, 1, 100).final, 3, 'after the window');
});
test('steady load of 1,200m at a 50% target of 500m settles on 5 pods, and after a spike ends nothing scales down for 300 s', () => {
const s = new Cluster({ scenario: 'steady' });
s.run(1200);
assert.equal(s.spec, 5);
const sp = new Cluster({ scenario: 'spike' });
let firstDown = -1;
for (let t = 0; t < 1500; t++) { const before = sp.spec; sp.tick(); if (sp.now > 600 && sp.spec < before && firstDown < 0) firstDown = sp.now; }
assert.ok(firstDown < 0 || firstDown >= 900, `scaled down at ${firstDown} s, inside the stabilization window`);
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.