diff --git a/.gitignore b/.gitignore index 6a0711782..bd7d7080b 100644 --- a/.gitignore +++ b/.gitignore @@ -80,3 +80,10 @@ apps/docs/build .pnpm-store benchmark-results .bench/ + +# Homarr backups used by scripts/benchmarks/stress-restore.mts. These contain +# real integration secrets and an encryption key — never commit one. +homarr-backup*.zip +*.sqlite +*.sqlite-wal +*.sqlite-shm diff --git a/scripts/benchmarks/stress-restore-lib.mts b/scripts/benchmarks/stress-restore-lib.mts new file mode 100644 index 000000000..75fa91441 --- /dev/null +++ b/scripts/benchmarks/stress-restore-lib.mts @@ -0,0 +1,174 @@ +/** + * Pure helpers for the dashboard stress benchmark (`stress-restore.mts`). + * + * Kept free of Docker/Playwright so the measurement maths and the container + * snapshot script stay unit-testable. + */ + +export type ProcessSample = { + command: string; + parentPid: number; + pid: number; + pssBytes: number; + rssBytes: number; +}; + +export type StressCheckpoint = { + name: string; + capturedAt: string; + elapsedMs: number; + container: { + currentBytes: number; + peakBytes: number | null; + anonymousBytes: number; + fileBytes: number; + }; + processes: ProcessSample[]; + redis: { usedMemoryBytes: number; peakMemoryBytes: number } | null; +}; + +/** + * Emitted inside the container and parsed by `parseStressSnapshot`. + * + * cgroup v2 `memory.current` is the number that matters for a container memory + * limit (it is what the OOM killer compares against), so it is the headline + * metric rather than the sum of process RSS, which double-counts shared pages. + */ +export const stressMemoryScript = String.raw` +echo "memory_current=$(cat /sys/fs/cgroup/memory.current 2>/dev/null || echo 0)" +memory_peak="$(cat /sys/fs/cgroup/memory.peak 2>/dev/null || true)" +case "$memory_peak" in ''|'max') ;; *) echo "memory_peak=$memory_peak" ;; esac +awk '/^anon / { print "cgroup_anon=" $2 } /^file / { print "cgroup_file=" $2 }' /sys/fs/cgroup/memory.stat 2>/dev/null +for status_file in /proc/[0-9]*/status; do + [ -r "$status_file" ] || continue + process_dir="$(dirname "$status_file")" + process_values="$(awk '/^Name:/ { name=$2 } /^Pid:/ { pid=$2 } /^PPid:/ { ppid=$2 } /^VmRSS:/ { rss=$2 } END { if (rss == "") rss=0; print pid "|" ppid "|" rss "|" name }' "$status_file" 2>/dev/null)" + process_pss="$(awk '/^Pss:/ { print $2 }' "$process_dir/smaps_rollup" 2>/dev/null || echo 0)" + [ -n "$process_pss" ] || process_pss=0 + [ -n "$process_values" ] && echo "process=$process_values|$process_pss" +done +redis_info="$(redis-cli INFO memory 2>/dev/null | tr -d '\r' || true)" +redis_used="$(printf '%s\n' "$redis_info" | awk -F: '/^used_memory:/ { print $2 }')" +redis_peak="$(printf '%s\n' "$redis_info" | awk -F: '/^used_memory_peak:/ { print $2 }')" +if [ -n "$redis_used" ] && [ -n "$redis_peak" ]; then + echo "redis_used_memory=$redis_used" + echo "redis_used_memory_peak=$redis_peak" +fi +`; + +const readNumericLine = (input: string, key: string) => { + const match = new RegExp(`^${key}[=:]\\s*(\\d+)`, "m").exec(input); + return match?.[1] === undefined ? null : Number(match[1]); +}; + +export const parseStressSnapshot = (name: string, elapsedMs: number, output: string): StressCheckpoint => { + const currentBytes = readNumericLine(output, "memory_current"); + if (currentBytes === null) throw new Error(`Snapshot "${name}" is missing memory_current`); + + const processes = output + .split("\n") + .filter((line) => line.startsWith("process=")) + .flatMap((line) => { + const [pid, parentPid, rssKiB, command, pssKiB] = line.slice("process=".length).split("|"); + if (!pid || !parentPid || rssKiB === undefined || !command) return []; + return [ + { + pid: Number(pid), + parentPid: Number(parentPid), + rssBytes: Number(rssKiB) * 1024, + pssBytes: Number(pssKiB ?? 0) * 1024, + command, + }, + ]; + }); + + const redisUsed = readNumericLine(output, "redis_used_memory"); + const redisPeak = readNumericLine(output, "redis_used_memory_peak"); + + return { + name, + capturedAt: new Date().toISOString(), + elapsedMs, + container: { + currentBytes, + peakBytes: readNumericLine(output, "memory_peak"), + anonymousBytes: readNumericLine(output, "cgroup_anon") ?? 0, + fileBytes: readNumericLine(output, "cgroup_file") ?? 0, + }, + processes, + redis: redisUsed === null || redisPeak === null ? null : { usedMemoryBytes: redisUsed, peakMemoryBytes: redisPeak }, + }; +}; + +/** Node processes only — `next-server` plus any child it forks. */ +export const getNodeProcesses = (checkpoint: StressCheckpoint) => + checkpoint.processes.filter((process) => /node|next/i.test(process.command)); + +export const sumNodeRssBytes = (checkpoint: StressCheckpoint) => + getNodeProcesses(checkpoint).reduce((total, process) => total + process.rssBytes, 0); + +/** Largest single node process — the Next.js server in a normal container. */ +export const getPrimaryNodeRssBytes = (checkpoint: StressCheckpoint) => + getNodeProcesses(checkpoint).reduce((max, process) => Math.max(max, process.rssBytes), 0); + +export const toMiB = (bytes: number) => Math.round((bytes / 1024 / 1024) * 10) / 10; + +/** + * Least-squares slope over (elapsedMs, bytes), reported per hour. A large + * positive slope across the idle soak is the signature of a retention leak + * rather than of ordinary warm-up. + */ +export const getGrowthBytesPerHour = (samples: { elapsedMs: number; bytes: number }[]) => { + if (samples.length < 2) return null; + const n = samples.length; + const meanX = samples.reduce((sum, s) => sum + s.elapsedMs, 0) / n; + const meanY = samples.reduce((sum, s) => sum + s.bytes, 0) / n; + let numerator = 0; + let denominator = 0; + for (const sample of samples) { + const dx = sample.elapsedMs - meanX; + numerator += dx * (sample.bytes - meanY); + denominator += dx * dx; + } + if (denominator === 0) return null; + return Math.round((numerator / denominator) * 3_600_000); +}; + +export const summarizeStress = (checkpoints: StressCheckpoint[], soakPrefix = "soak") => { + const containerMiBByName = (name: string) => { + const checkpoint = checkpoints.find((candidate) => candidate.name === name); + return checkpoint ? toMiB(checkpoint.container.currentBytes) : null; + }; + const soakSamples = checkpoints + .filter((checkpoint) => checkpoint.name.startsWith(soakPrefix)) + .map((checkpoint) => ({ elapsedMs: checkpoint.elapsedMs, bytes: checkpoint.container.currentBytes })); + + const peak = checkpoints.reduce( + (max, checkpoint) => Math.max(max, checkpoint.container.peakBytes ?? checkpoint.container.currentBytes), + 0, + ); + + return { + checkpointCount: checkpoints.length, + containerMiB: Object.fromEntries( + checkpoints.map((checkpoint) => [checkpoint.name, toMiB(checkpoint.container.currentBytes)]), + ), + nodeRssMiB: Object.fromEntries( + checkpoints.map((checkpoint) => [checkpoint.name, toMiB(sumNodeRssBytes(checkpoint))]), + ), + headline: { + bootIdleMiB: containerMiBByName("01-boot-idle"), + boardLoadedMiB: containerMiBByName("04-board-loaded"), + afterStressMiB: containerMiBByName("05-after-stress"), + peakMiB: toMiB(peak), + soakGrowthMiBPerHour: (() => { + const slope = getGrowthBytesPerHour(soakSamples); + return slope === null ? null : toMiB(slope); + })(), + }, + redisPeakMiB: (() => { + const peaks = checkpoints.flatMap((c) => (c.redis ? [c.redis.peakMemoryBytes] : [])); + return peaks.length === 0 ? null : toMiB(Math.max(...peaks)); + })(), + }; +}; diff --git a/scripts/benchmarks/stress-restore-lib.spec.ts b/scripts/benchmarks/stress-restore-lib.spec.ts new file mode 100644 index 000000000..6eb07fc3c --- /dev/null +++ b/scripts/benchmarks/stress-restore-lib.spec.ts @@ -0,0 +1,201 @@ +import { describe, expect, it } from "vitest"; + +import { + getGrowthBytesPerHour, + getNodeProcesses, + getPrimaryNodeRssBytes, + parseStressSnapshot, + stressMemoryScript, + sumNodeRssBytes, + summarizeStress, + toMiB, +} from "./stress-restore-lib.mts"; + +const snapshot = ({ + current = 300 * 1024 * 1024, + peak = 400 * 1024 * 1024, + processes = ["1|0|4096|nginx|2048", "20|1|204800|node|180000", "30|1|8192|redis-server|4096"], + redis = true, +}: { + current?: number; + peak?: number; + processes?: string[]; + redis?: boolean; +} = {}) => + [ + `memory_current=${current}`, + `memory_peak=${peak}`, + "cgroup_anon=250000000", + "cgroup_file=50000000", + ...processes.map((process) => `process=${process}`), + ...(redis ? ["redis_used_memory=1048576", "redis_used_memory_peak=2097152"] : []), + ].join("\n"); + +describe("parseStressSnapshot", () => { + it("parses cgroup totals, processes and redis", () => { + const checkpoint = parseStressSnapshot("01-boot-idle", 1_234, snapshot()); + + expect(checkpoint.name).toBe("01-boot-idle"); + expect(checkpoint.elapsedMs).toBe(1_234); + expect(checkpoint.container.currentBytes).toBe(300 * 1024 * 1024); + expect(checkpoint.container.peakBytes).toBe(400 * 1024 * 1024); + expect(checkpoint.container.anonymousBytes).toBe(250_000_000); + expect(checkpoint.processes).toHaveLength(3); + expect(checkpoint.redis).toEqual({ usedMemoryBytes: 1_048_576, peakMemoryBytes: 2_097_152 }); + }); + + it("converts process RSS and PSS from KiB to bytes", () => { + const [, node] = parseStressSnapshot("x", 0, snapshot()).processes; + + expect(node?.command).toBe("node"); + expect(node?.rssBytes).toBe(204_800 * 1024); + expect(node?.pssBytes).toBe(180_000 * 1024); + }); + + it("throws when memory_current is absent so a bad snapshot never scores as 0 MiB", () => { + expect(() => parseStressSnapshot("broken", 0, "cgroup_anon=1\n")).toThrow(/memory_current/); + }); + + it("tolerates a missing redis section", () => { + expect(parseStressSnapshot("x", 0, snapshot({ redis: false })).redis).toBeNull(); + }); + + it("skips malformed process lines rather than emitting NaN samples", () => { + const checkpoint = parseStressSnapshot("x", 0, snapshot({ processes: ["garbage", "20|1|100|node|50"] })); + + expect(checkpoint.processes).toHaveLength(1); + expect(checkpoint.processes[0]?.command).toBe("node"); + }); + + it("defaults PSS to zero when smaps_rollup is unreadable", () => { + const checkpoint = parseStressSnapshot("x", 0, snapshot({ processes: ["20|1|100|node|0"] })); + + expect(checkpoint.processes[0]?.pssBytes).toBe(0); + }); +}); + +describe("node process selection", () => { + it("counts only node processes, excluding nginx and redis", () => { + const checkpoint = parseStressSnapshot("x", 0, snapshot()); + + expect(getNodeProcesses(checkpoint).map((p) => p.command)).toEqual(["node"]); + expect(sumNodeRssBytes(checkpoint)).toBe(204_800 * 1024); + }); + + it("sums multiple node processes but reports the largest as primary", () => { + const checkpoint = parseStressSnapshot( + "x", + 0, + snapshot({ processes: ["20|1|200000|node|1", "21|20|50000|node|1"] }), + ); + + expect(sumNodeRssBytes(checkpoint)).toBe(250_000 * 1024); + expect(getPrimaryNodeRssBytes(checkpoint)).toBe(200_000 * 1024); + }); +}); + +describe("getGrowthBytesPerHour", () => { + it("returns null below two samples", () => { + expect(getGrowthBytesPerHour([])).toBeNull(); + expect(getGrowthBytesPerHour([{ elapsedMs: 0, bytes: 1 }])).toBeNull(); + }); + + it("extrapolates a linear climb to bytes per hour", () => { + // +1 MiB every minute => +60 MiB/hour. + const samples = [0, 1, 2, 3].map((minute) => ({ + elapsedMs: minute * 60_000, + bytes: minute * 1024 * 1024, + })); + + expect(getGrowthBytesPerHour(samples)).toBe(60 * 1024 * 1024); + }); + + it("reports a flat series as zero growth", () => { + const samples = [0, 1, 2].map((minute) => ({ elapsedMs: minute * 60_000, bytes: 500 })); + + expect(getGrowthBytesPerHour(samples)).toBe(0); + }); + + it("returns null when every sample shares one timestamp", () => { + expect( + getGrowthBytesPerHour([ + { elapsedMs: 5, bytes: 1 }, + { elapsedMs: 5, bytes: 2 }, + ]), + ).toBeNull(); + }); + + it("reports shrinking memory as negative growth", () => { + const samples = [ + { elapsedMs: 0, bytes: 100 * 1024 * 1024 }, + { elapsedMs: 3_600_000, bytes: 50 * 1024 * 1024 }, + ]; + + expect(getGrowthBytesPerHour(samples)).toBe(-50 * 1024 * 1024); + }); +}); + +describe("summarizeStress", () => { + const build = (name: string, currentMiB: number, elapsedMs: number) => + parseStressSnapshot(name, elapsedMs, snapshot({ current: currentMiB * 1024 * 1024, peak: 900 * 1024 * 1024 })); + + it("surfaces the headline lifecycle numbers", () => { + const summary = summarizeStress([ + build("01-boot-idle", 200, 0), + build("04-board-loaded", 450, 60_000), + build("05-after-stress", 600, 120_000), + ]); + + expect(summary.headline.bootIdleMiB).toBe(200); + expect(summary.headline.boardLoadedMiB).toBe(450); + expect(summary.headline.afterStressMiB).toBe(600); + expect(summary.headline.peakMiB).toBe(900); + expect(summary.checkpointCount).toBe(3); + }); + + it("derives soak growth only from soak checkpoints", () => { + const summary = summarizeStress([ + build("01-boot-idle", 1_000, 0), + build("soak-01", 100, 0), + build("soak-02", 200, 3_600_000), + ]); + + expect(summary.headline.soakGrowthMiBPerHour).toBe(100); + }); + + it("leaves headline fields null when their checkpoints were not captured", () => { + const summary = summarizeStress([build("soak-01", 100, 0)]); + + expect(summary.headline.bootIdleMiB).toBeNull(); + expect(summary.headline.boardLoadedMiB).toBeNull(); + expect(summary.headline.soakGrowthMiBPerHour).toBeNull(); + }); + + it("reports the peak redis footprint across checkpoints", () => { + expect(summarizeStress([build("01-boot-idle", 100, 0)]).redisPeakMiB).toBe(2); + }); +}); + +describe("stressMemoryScript", () => { + it("reads cgroup v2 memory.current, which is what the container limit applies to", () => { + expect(stressMemoryScript).toContain("/sys/fs/cgroup/memory.current"); + }); + + it("avoids ${...} so JS never interpolates a shell expansion away", () => { + // String.raw suppresses backslash escapes but NOT ${...} interpolation, so the + // script must not contain any — `dirname` replaces the usual ${var%/suffix}. + expect(stressMemoryScript).not.toContain("${"); + expect(stressMemoryScript).toContain('dirname "$status_file"'); + }); + + it("defaults PSS to 0 so an unreadable smaps_rollup cannot emit an empty field", () => { + expect(stressMemoryScript).toContain("process_pss=0"); + }); +}); + +describe("toMiB", () => { + it("rounds to one decimal place", () => { + expect(toMiB(1024 * 1024)).toBe(1); + expect(toMiB(1.55 * 1024 * 1024)).toBe(1.6); + }); +}); diff --git a/scripts/benchmarks/stress-restore.mts b/scripts/benchmarks/stress-restore.mts new file mode 100644 index 000000000..89432e064 --- /dev/null +++ b/scripts/benchmarks/stress-restore.mts @@ -0,0 +1,323 @@ +/** + * Dashboard stress benchmark. + * + * Measures container memory for a *populated* Homarr rather than an empty one: + * boots a fresh container, restores a real backup through the onboarding + * restore UI (the same flow a user takes, including the "I understand" + * confirmation), signs in, opens the busiest board, then hammers navigation and + * soaks idle while sampling cgroup memory. + * + * The backup is supplied by path and never copied into the repo — it contains + * real integration secrets. + * + * Usage: + * STRESS_IMAGE=homarr-bench:baseline \ + * STRESS_BACKUP_ZIP=~/Downloads/homarr-backup.zip \ + * node --experimental-strip-types scripts/benchmarks/stress-restore.mts + */ +import { execFile } from "node:child_process"; +import { mkdir, readFile, rm, writeFile } from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { promisify } from "node:util"; +import { chromium } from "@playwright/test"; +import type { Page } from "@playwright/test"; + +import { parseStressSnapshot, stressMemoryScript, summarizeStress, toMiB } from "./stress-restore-lib.mts"; +import type { StressCheckpoint } from "./stress-restore-lib.mts"; + +const execFileAsync = promisify(execFile); + +const requireEnv = (name: string) => { + const value = process.env[name]; + if (!value) throw new Error(`${name} is required`); + return value; +}; + +const image = requireEnv("STRESS_IMAGE"); +const backupZip = path.resolve(requireEnv("STRESS_BACKUP_ZIP").replace(/^~/, os.homedir())); +const label = process.env.STRESS_LABEL ?? image.replace(/[^a-z0-9]+/gi, "-"); +const memoryLimit = process.env.STRESS_MEMORY_LIMIT ?? "2g"; +const heapMb = process.env.STRESS_HEAP_MB ?? ""; +const navIterations = Number(process.env.STRESS_NAV_ITERATIONS ?? 6); +const soakMs = Number(process.env.STRESS_SOAK_MS ?? 120_000); +const soakIntervalMs = Number(process.env.STRESS_SOAK_INTERVAL_MS ?? 20_000); +const keepContainer = process.env.STRESS_KEEP_CONTAINER === "true"; +const outputDirectory = path.resolve(process.env.STRESS_OUTPUT ?? `.bench/stress/${label}`); + +const password = process.env.STRESS_PASSWORD ?? "demodemo"; +const username = process.env.STRESS_USERNAME ?? "demo"; +/** Board to stress. The backup's busiest board (28 items / 16 widget kinds). */ +const boardName = process.env.STRESS_BOARD ?? "default"; + +const containerName = `homarr-stress-${Date.now()}`; +const checkpoints: StressCheckpoint[] = []; +let startedAt = Date.now(); + +const log = (message: string) => console.log(`[stress:${label}] ${message}`); + +const captureAsync = async (name: string) => { + const { stdout } = await execFileAsync("docker", ["exec", containerName, "sh", "-c", stressMemoryScript], { + maxBuffer: 8 * 1024 * 1024, + }); + await writeFile(path.join(outputDirectory, `${name}.txt`), stdout); + const checkpoint = parseStressSnapshot(name, Date.now() - startedAt, stdout); + checkpoints.push(checkpoint); + log(`${name}: container=${toMiB(checkpoint.container.currentBytes)} MiB`); + return checkpoint; +}; + +const waitForReadyAsync = async (baseUrl: string, timeoutMs = 180_000) => { + const deadline = Date.now() + timeoutMs; + while (Date.now() < deadline) { + try { + const response = await fetch(`${baseUrl}/api/health/ready`); + if (response.ok) return; + } catch { + // Socket not listening yet. + } + await new Promise((resolve) => setTimeout(resolve, 1_000)); + } + throw new Error(`Container never became ready at ${baseUrl}`); +}; + +/** + * The restored backup's boards are private and owned by another account, and + * the demo user has no home board. Grant access explicitly so the measurement + * targets a fully-populated board instead of an empty state. + */ +const applyAccessFixtureAsync = async () => { + const script = ` +const Database = require("/app/node_modules/better-sqlite3"); +const db = new Database("/appdata/db/db.sqlite"); +const user = db.prepare("SELECT id FROM user WHERE name = ?").get(process.argv[1]); +if (!user) throw new Error("user not found: " + process.argv[1]); +const board = db.prepare("SELECT id FROM board WHERE name = ?").get(process.argv[2]); +if (!board) throw new Error("board not found: " + process.argv[2]); +const adminGroup = db.prepare("SELECT group_id AS id FROM groupPermission WHERE permission = 'admin' LIMIT 1").get(); +if (adminGroup) { + db.prepare("INSERT OR IGNORE INTO groupMember (group_id, user_id) VALUES (?, ?)").run(adminGroup.id, user.id); +} +db.prepare("UPDATE user SET home_board_id = ?, mobile_home_board_id = ? WHERE id = ?").run(board.id, board.id, user.id); +db.close(); +console.log("fixture-applied"); +`; + const { stdout } = await execFileAsync("docker", ["exec", containerName, "node", "-e", script, username, boardName]); + if (!stdout.includes("fixture-applied")) throw new Error(`Access fixture failed: ${stdout}`); + log("access fixture applied (admin group + home board)"); +}; + +const restoreBackupAsync = async (page: Page, baseUrl: string) => { + // networkidle (not domcontentloaded): the toggle below is a client component, and + // clicking it before hydration silently does nothing. + await page.goto(`${baseUrl}/init`, { waitUntil: "networkidle" }); + + // The onboarding start step hides the restore dropzone behind a toggle. Retry the + // click until the dropzone's file input exists, so a slow hydration cannot make the + // whole run fail. + const expandRestore = page.getByRole("button", { name: /restore.*sqlite|sqlite.*restore|restore.*backup/i }); + const fileInput = page.locator('input[type="file"]'); + for (let attempt = 1; attempt <= 3; attempt++) { + if ((await fileInput.count()) > 0) break; + await expandRestore.first().click({ timeout: 30_000 }); + await fileInput + .first() + .waitFor({ state: "attached", timeout: 10_000 }) + .catch(() => log(`restore toggle click ${attempt} did not reveal the dropzone, retrying`)); + } + await fileInput.first().waitFor({ state: "attached", timeout: 30_000 }); + + await fileInput.first().setInputFiles(backupZip); + log("backup uploaded, waiting for analysis"); + + const continueButton = page.getByRole("button", { name: /continue to restore|continue/i }); + await continueButton.first().waitFor({ state: "visible", timeout: 180_000 }); + await continueButton.first().click(); + + const confirmInput = page.locator('input[placeholder="I understand"]'); + await confirmInput.waitFor({ state: "visible", timeout: 30_000 }); + await confirmInput.fill("I understand"); + log('typed "I understand"'); + + const submit = page.getByRole("button", { name: /restore|confirm/i }).last(); + await submit.click(); + log("restore submitted; server will exit and be restarted by run.sh"); +}; + +const loginAsync = async (page: Page, baseUrl: string) => { + await page.goto(`${baseUrl}/auth/login`, { waitUntil: "domcontentloaded" }); + // Selectors mirror e2e/lazy-widgets.spec.ts: getByLabel(/password/i) is ambiguous + // because Mantine's PasswordInput also renders a visibility-toggle button. + await page.getByLabel("Username").fill(username); + await page.locator("#password").fill(password); + await page.locator("css=button[type='submit']").click(); + await page.waitForURL((url) => !url.pathname.includes("/auth/login"), { timeout: 60_000 }); + log(`signed in as ${username}`); +}; + +const waitForBoardAsync = async (page: Page) => { + const boardSelector = "[data-homarr-dev-benchmark-board]"; + await page.locator(boardSelector).waitFor({ state: "visible", timeout: 120_000 }); + // Every grid item must have mounted its content (ready or explicitly errored), + // otherwise memory is sampled mid-render and understates the real cost. + await page + .waitForFunction( + (selector) => { + const board = document.querySelector(selector); + const items = [...(board?.querySelectorAll('[data-type="item"]') ?? [])]; + return ( + items.length > 0 && + items.every( + (item) => + item.querySelector("[data-homarr-widget-ready]") !== null || + item.querySelector("[data-homarr-widget-error]") !== null, + ) + ); + }, + boardSelector, + { timeout: 120_000 }, + ) + .catch(() => log("warning: not all widgets reported ready/error before timeout")); + const outcome = await page.evaluate((selector) => { + const board = document.querySelector(selector); + const items = [...(board?.querySelectorAll('[data-type="item"]') ?? [])]; + return { + items: items.length, + ready: items.filter((item) => item.querySelector("[data-homarr-widget-ready]") !== null).length, + errored: items.filter((item) => item.querySelector("[data-homarr-widget-error]") !== null).length, + }; + }, boardSelector); + log(`board rendered: ${outcome.items} items (${outcome.ready} ready, ${outcome.errored} errored)`); + return outcome; +}; + +const main = async () => { + await rm(outputDirectory, { recursive: true, force: true }); + await mkdir(outputDirectory, { recursive: true }); + await readFile(backupZip); // Fail fast if the backup path is wrong. + + log(`image=${image} memoryLimit=${memoryLimit} heapMb=${heapMb || "(image default)"}`); + + const runArgs = [ + "run", + "-d", + "--name", + containerName, + "--cpus", + process.env.STRESS_CPUS ?? "4", + "--memory", + memoryLimit, + "--memory-swap", + memoryLimit, + "-p", + "0:7575", + // Restore re-encrypts the backup's secrets to this key, so any 64-hex value works. + "-e", + `SECRET_ENCRYPTION_KEY=${"0".repeat(64)}`, + // Silences Homarr's own outbound work (update check, icon repos, analytics) so + // the measurement is not perturbed by internet reachability. Integration widget + // requests are NOT gated by this flag, so integration clients still load. + "-e", + "NO_EXTERNAL_CONNECTION=true", + "-e", + "LOG_LEVEL=warn", + ]; + if (heapMb) runArgs.push("-e", `HOMARR_MAX_OLD_SPACE_SIZE=${heapMb}`); + runArgs.push(image); + + await execFileAsync("docker", runArgs); + startedAt = Date.now(); + + const browser = await chromium.launch(); + try { + const { stdout: portOut } = await execFileAsync("docker", ["port", containerName, "7575"]); + const mappedPort = portOut.trim().split("\n")[0]?.split(":").pop(); + if (!mappedPort) throw new Error(`Could not resolve mapped port from: ${portOut}`); + const baseUrl = `http://127.0.0.1:${mappedPort}`; + log(`container up at ${baseUrl}`); + + await waitForReadyAsync(baseUrl); + // Let boot-time work (cron registration, first GC) settle before the idle baseline. + await new Promise((resolve) => setTimeout(resolve, 15_000)); + await captureAsync("01-boot-idle"); + + const context = await browser.newContext({ viewport: { width: 1920, height: 1080 } }); + const page = await context.newPage(); + + await restoreBackupAsync(page, baseUrl); + // The import route exits the process; run.sh restarts it. + await new Promise((resolve) => setTimeout(resolve, 5_000)); + await waitForReadyAsync(baseUrl); + await captureAsync("02-post-restore"); + + await applyAccessFixtureAsync(); + + await context.close(); + const freshContext = await browser.newContext({ viewport: { width: 1920, height: 1080 } }); + const freshPage = await freshContext.newPage(); + + await loginAsync(freshPage, baseUrl); + await captureAsync("03-post-login"); + + await freshPage.goto(`${baseUrl}/boards/${boardName}`, { waitUntil: "domcontentloaded" }); + const boardOutcome = await waitForBoardAsync(freshPage); + await captureAsync("04-board-loaded"); + + log(`stress: ${navIterations} reload iterations`); + for (let iteration = 0; iteration < navIterations; iteration++) { + await freshPage.goto(`${baseUrl}/boards/${boardName}`, { waitUntil: "domcontentloaded" }); + await waitForBoardAsync(freshPage); + await captureAsync(`nav-${String(iteration + 1).padStart(2, "0")}`); + } + await captureAsync("05-after-stress"); + + log(`soak: ${soakMs}ms idle with board open`); + const soakDeadline = Date.now() + soakMs; + let soakIndex = 0; + while (Date.now() < soakDeadline) { + await new Promise((resolve) => setTimeout(resolve, soakIntervalMs)); + await captureAsync(`soak-${String(++soakIndex).padStart(2, "0")}`); + } + await captureAsync("06-final"); + + const summary = { + label, + image, + imageRevision: ( + await execFileAsync("docker", [ + "image", + "inspect", + image, + "--format", + '{{index .Config.Labels "org.opencontainers.image.revision"}}', + ]) + ).stdout.trim(), + imageSizeBytes: Number( + (await execFileAsync("docker", ["image", "inspect", image, "--format", "{{.Size}}"])).stdout.trim(), + ), + config: { memoryLimit, heapMb: heapMb || null, navIterations, soakMs, soakIntervalMs, boardName }, + // Recorded so an A/B pair can be rejected if the two runs rendered + // different amounts of the board (e.g. more widgets errored on one side). + board: boardOutcome, + ...summarizeStress(checkpoints), + }; + await writeFile(path.join(outputDirectory, "summary.json"), `${JSON.stringify(summary, null, 2)}\n`); + await writeFile(path.join(outputDirectory, "checkpoints.json"), `${JSON.stringify(checkpoints, null, 2)}\n`); + console.log(JSON.stringify(summary, null, 2)); + + await freshContext.close(); + } finally { + await browser.close().catch(() => undefined); + await execFileAsync("docker", ["logs", containerName]) + .then(({ stdout, stderr }) => writeFile(path.join(outputDirectory, "container.log"), `${stdout}\n${stderr}`)) + .catch(() => undefined); + if (keepContainer) { + log(`keeping container ${containerName}`); + } else { + // -v also drops the anonymous /appdata volume so repeated runs start clean. + await execFileAsync("docker", ["rm", "-fv", containerName]).catch(() => undefined); + } + } +}; + +await main();