import { describe, expect, it } from "vitest"; import { dbConnectionBudget, planCapacity, Pressure, type Reading } from "../src/lib/capacity.js"; const GB = 1e9; describe("capacity", () => { it("does more at once on a bigger container", () => { const small = planCapacity({}, { cpus: 4, memoryBytes: 8 * GB }); const big = planCapacity({}, { cpus: 32, memoryBytes: 64 * GB }); expect(small).toMatchObject({ faces: 3, ingest: 4, archive: 2, render: 2, autopilot: 2, delivery: 4 }); expect(big).toMatchObject({ cpus: 32, faces: 6, ingest: 8, archive: 9, archiveBurst: 91, render: 16, autopilot: 16, delivery: 8 }); expect(planCapacity({}, { cpus: 48, memoryBytes: 64 * GB })).toMatchObject({ faces: 6, archiveBurst: 91, archiveDownloads: 960 }); // A fixed ARCHIVE_CONCURRENCY never holds a backlog back; downloads run many at once even on a small machine. expect(planCapacity({ archive: 2 }, { cpus: 4, memoryBytes: 8 * GB })).toMatchObject({ archive: 2, archiveBurst: 7, archiveDownloads: 80 }); }); it("is held back by memory when cores are many and memory is short", () => { const tight = planCapacity({}, { cpus: 32, memoryBytes: 6 * GB }); expect(tight.faces).toBe(2); expect(tight.archive).toBe(2); }); it("keeps what the environment sets", () => { expect(planCapacity({ faces: 3, archive: 40 }, { cpus: 32, memoryBytes: 64 * GB })).toMatchObject({ faces: 3, archive: 40 }); }); it("lets bulk work step aside while the machine is busy", () => { const at = (r: Partial) => new Pressure(() => ({ cpuStall: null, load: 0, cpus: 32, free: 32 * GB, total: 64 * GB, ...r })); expect(at({ cpuStall: 5 }).allow(16)).toBe(16); expect(at({ cpuStall: 45 }).allow(16)).toBe(8); expect(at({ cpuStall: 80 }).allow(16)).toBe(4); expect(at({ load: 40 }).allow(16)).toBe(4); expect(at({ free: 3 * GB }).allow(16)).toBe(4); expect(at({ cpuStall: 80 }).allow(2)).toBe(1); // A big backlog: full speed through a saturated CPU, back off only near the limits. expect(at({ cpuStall: 80 }).allow(36, true)).toBe(36); expect(at({ cpuStall: 95 }).allow(36, true)).toBe(18); expect(at({ free: 6 * GB }).allow(36, true)).toBe(18); }); }); describe("dbConnectionBudget", () => { it("stays under what the API's cluster, a deploy's reload overlap, the superuser reserve, the broadcaster and headroom leave of max_connections", () => { // 100 - 3 superuser reserved - ((2 api instances + 1 reload overlap) * (10 pool + 3 realtime)) - 1 broadcaster - 10 headroom = 47. expect(dbConnectionBudget(500)).toBe(47); expect(dbConnectionBudget(10)).toBe(10); }); it("follows a differently sized API cluster or Postgres limit", () => { expect(dbConnectionBudget(500, { max: 200, apiInstances: 4, apiPoolSize: 15 })).toBe(96); expect(dbConnectionBudget(500, { max: 40, apiInstances: 2, apiPoolSize: 10 })).toBe(1); }); it("can be told a deploy never overlaps instances (e.g. a non-cluster or single-instance API)", () => { expect(dbConnectionBudget(500, { apiReloadOverlap: 0 })).toBe(60); }); it("never goes below 1, even if the API and headroom already exceed max_connections", () => { expect(dbConnectionBudget(50, { max: 20, apiInstances: 2, apiPoolSize: 10 })).toBe(1); }); });