Public: anyone can read this; the crew can change it.
Every worker VM writes its own `memory worker <name>` line, not only the engine worker #12938
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/12925-every-worker-vm-writes-its-own-memory-wo → main 4 commits fork 2 threads · 2 unresolved
20 files
changed +153
−11
docs/features/memory-growth.md +4 −3
| @@ -4,11 +4,12 @@ Since Bun 1.4.4 the serving process has held several GiB after hours. This chang | ||
| 4 | 4 | |
| 5 | 5 | ## Where it shows |
| 6 | 6 | |
| 7 | - **The minute line.** Each minute the log carries `memory main: rss …, heap …, malloc … (peak …, committed peak …), up … min`. A worker VM writes `memory worker engine: heap …, malloc …, process rss …` on its own minute. RSS rising while the main heap stays flat is native memory; a worker line rising is that worker's. Read them in `/system`'s log or the process log. | |
| 7 | - **The minute line.** Each minute the log carries `memory main: rss …, heap …, malloc … (peak …, committed peak …), up … min`. Every worker VM writes `memory worker <name>: heap …, malloc …, process rss …` on its own minute (#12925). RSS rising while the main heap stays flat is native memory; a worker line rising is that worker's. Read them in `/system`'s log or the process log. | |
| 8 | 8 | - **The stall line.** Each loop-stall line ends with `heap X (was Y N s ago; …)`, the heap now against the last minute's sample. |
| 9 | 9 | - `fell: a collection ran` means the heap shrank by more than a tenth since the sample, so the collector ran inside that stall. Bun 1.4.4 reports no collector time, so this is inferred, not measured. |
| 10 | 10 | - `did not fall: no collection seen` means the stall was not the collector's. |
| 11 | 11 | - **The need.** When RSS is at least twice the mean of the process's first hour (at least ten samples), `/system`, the board's forge strip and the terminal's home each list one need: the figure, the first-hour figure and when it began growing. |
| 12 | - **The worker names.** `engine` (src/engine/worker.ts); `disk`, `tree` (src/apps); `lanes`, `pages`, `pipeline` (src/flow); `scan`, `du`, `leak` (the `answerAsks` workers of src/kernel/worker_ask.ts); `thumb` (src/looks); `log-writer` (src/runs); `mirror`, `vacuum` (src/snapshot); `code-rows`, `map`, `raster` (src/web). A one-shot worker (`mirror`, `vacuum`) that ends inside its first minute writes no line; the timer is unref'd, so it never keeps a worker alive. | |
| 12 | 13 | |
| 13 | 14 | ## What to do when the need shows |
| 14 | 15 | |
| @@ -20,8 +21,8 @@ The need clears by itself once RSS falls under twice the baseline. A process tha | ||
| 20 | 21 | |
| 21 | 22 | ## How it is built |
| 22 | 23 | |
| 23 | `src/perf/memory_watch.ts` owns the readings (`heapSize()` and `memoryUsage()`, constant time; never `heapStats()`, which held the loop ~280 ms a minute, #11118). `vitals()` takes the watch as `watch`, as it takes `disk`. Tests: `test/memory-watch.test.ts`, `test/vitals.test.ts`, `test/tui-memory-growth.test.ts`. | |
| 24 | `src/perf/memory_watch.ts` owns the readings (`heapSize()` and `memoryUsage()`, constant time; never `heapStats()`, which held the loop ~280 ms a minute, #11118). `vitals()` takes the watch as `watch`, as it takes `disk`. Each worker entry calls `workerMemory(<name>)` once at start; `FORGE_WORKER_MEMORY_MS` shortens the minute for tests. Tests: `test/memory-watch.test.ts`, `test/worker-memory-lines.test.ts`, `test/vitals.test.ts`, `test/tui-memory-growth.test.ts`. | |
| 24 | 25 | |
| 25 | 26 | ## Not done |
| 26 | 27 | |
| 27 | Finding what grows and fixing it, and wiring the other workers to write their own line. | |
| 28 | Finding what grows and fixing it. |
src/apps/disk_worker.ts +3
| @@ -1,9 +1,12 @@ | ||
| 1 | 1 | /** The worker side of `diskWalkOff` (`contain.ts`): walk the directories and say what they hold. */ |
| 2 | 2 | |
| 3 | 3 | import { diskWalk } from "./disk_walk.ts"; |
| 4 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 4 | 5 | |
| 5 | 6 | declare const self: Worker; |
| 6 | 7 | |
| 8 | workerMemory("disk"); | |
| 9 | ||
| 7 | 10 | self.onmessage = (e: MessageEvent<{ id: number; dirs: string[]; max: number }>) => { |
| 8 | 11 | try { |
| 9 | 12 | postMessage({ id: e.data.id, walk: diskWalk(e.data.dirs, e.data.max) }); |
src/apps/tree_worker.ts +3
| @@ -1,9 +1,12 @@ | ||
| 1 | 1 | /** The worker side of `readTreesOff` (`contain.ts`): read every watched tree, as plain data. */ |
| 2 | 2 | |
| 3 | 3 | import { readTrees, type TreeAsk } from "./tree_read.ts"; |
| 4 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 4 | 5 | |
| 5 | 6 | declare const self: Worker; |
| 6 | 7 | |
| 8 | workerMemory("tree"); | |
| 9 | ||
| 7 | 10 | self.onmessage = (e: MessageEvent<{ id: number; asks: TreeAsk[] }>) => { |
| 8 | 11 | try { |
| 9 | 12 | postMessage({ id: e.data.id, trees: readTrees(e.data.asks) }); |
src/checks/scan_worker.ts +1 −1
| @@ -3,4 +3,4 @@ | ||
| 3 | 3 | import { answerAsks } from "../kernel/worker_ask.ts"; |
| 4 | 4 | import { findingsIn, type Finding } from "./scan_rules.ts"; |
| 5 | 5 | |
| 6 | answerAsks<{ files: [string, string][]; withStatic: boolean }, Finding[]>((ask) => findingsIn(new Map(ask.files), ask.withStatic)); | |
| 6 | answerAsks<{ files: [string, string][]; withStatic: boolean }, Finding[]>("scan", (ask) => findingsIn(new Map(ask.files), ask.withStatic)); |
src/crew/leak_worker.ts +1 −1
| @@ -3,4 +3,4 @@ | ||
| 3 | 3 | import { answerAsks } from "../kernel/worker_ask.ts"; |
| 4 | 4 | import { type ScrubAsk, scrubFiles } from "./leak_files.ts"; |
| 5 | 5 | |
| 6 | answerAsks<ScrubAsk, string[]>(scrubFiles); | |
| 6 | answerAsks<ScrubAsk, string[]>("leak", scrubFiles); |
src/engine/worker.ts +2 −3
| @@ -14,7 +14,7 @@ import { startDuties } from "./duties.ts"; | ||
| 14 | 14 | import { timeStatements } from "../kernel/slow_sql.ts"; |
| 15 | 15 | import { createLog } from "../kernel/log.ts"; |
| 16 | 16 | import { during } from "../perf/jobs.ts"; |
| 17 | import { startWorkerMemory } from "../perf/memory_watch.ts"; | |
| 17 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 18 | 18 | import { BEAT_MS, FAILED } from "./beat.ts"; |
| 19 | 19 | |
| 20 | 20 | declare const self: Worker; |
| @@ -66,8 +66,7 @@ async function boot(init: EngineInit): Promise<void> { | ||
| 66 | 66 | }; |
| 67 | 67 | // Alive: a thread stuck in one statement stops saying so, and the serving thread ends it (`host.ts`). |
| 68 | 68 | const beat = setInterval(() => during("engine-beat", () => postMessage("h"), undefined, app), BEAT_MS); |
| 69 | const memoryLog = createLog("memory"); | |
| 70 | const stopMemory = startWorkerMemory("engine", (line) => memoryLog.info(line)); | |
| 69 | const stopMemory = workerMemory("engine"); | |
| 71 | 70 | const was = stop; |
| 72 | 71 | stop = () => { |
| 73 | 72 | clearInterval(beat); |
src/flow/lanes_worker.ts +3
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | |
| 12 | 12 | import { Database } from "bun:sqlite"; |
| 13 | 13 | import { compose, readFast, readSlow, SLOW_MS, type LanePicture, type Slow } from "./lanes_read.ts"; |
| 14 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 14 | 15 | |
| 15 | 16 | /** What the worker is asked. */ |
| 16 | 17 | export interface LaneJob { |
| @@ -37,6 +38,8 @@ export interface LaneAnswer { | ||
| 37 | 38 | |
| 38 | 39 | declare const self: Worker; |
| 39 | 40 | |
| 41 | workerMemory("lanes"); | |
| 42 | ||
| 40 | 43 | const handles = new Map<string, Database>(); |
| 41 | 44 | const kept = new Map<string, { at: number; slow: Slow }>(); |
| 42 | 45 |
src/flow/pages_worker.ts +3
| @@ -8,6 +8,7 @@ | ||
| 8 | 8 | |
| 9 | 9 | import { Database } from "bun:sqlite"; |
| 10 | 10 | import { readPagePicture, type PageJob, type PagesConfig, type PagesEnv, type Picture } from "./pages_read.ts"; |
| 11 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 11 | 12 | |
| 12 | 13 | /** What the worker is asked. */ |
| 13 | 14 | export interface PagesAsk { |
| @@ -29,6 +30,8 @@ export interface PagesAnswer { | ||
| 29 | 30 | |
| 30 | 31 | declare const self: Worker; |
| 31 | 32 | |
| 33 | workerMemory("pages"); | |
| 34 | ||
| 32 | 35 | const envs = new Map<string, PagesEnv>(); |
| 33 | 36 | |
| 34 | 37 | self.onmessage = (e: MessageEvent<PagesAsk>) => { |
src/flow/pipeline_worker.ts +3
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import { Database } from "bun:sqlite"; |
| 9 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 9 | 10 | |
| 10 | 11 | /** What the worker is asked: the database file, the stage's query and its arguments. */ |
| 11 | 12 | export interface WaitingJob { |
| @@ -25,6 +26,8 @@ export interface WaitingAnswer { | ||
| 25 | 26 | |
| 26 | 27 | declare const self: Worker; |
| 27 | 28 | |
| 29 | workerMemory("pipeline"); | |
| 30 | ||
| 28 | 31 | const handles = new Map<string, Database>(); |
| 29 | 32 | |
| 30 | 33 | self.onmessage = (e: MessageEvent<WaitingJob>) => { |
src/kernel/worker_ask.ts +8 −2
| @@ -9,6 +9,8 @@ | ||
| 9 | 9 | * to decide: most do the work on their own thread, so nothing is lost. |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 13 | ||
| 12 | 14 | interface Asked<Ask> { |
| 13 | 15 | readonly id: number; |
| 14 | 16 | readonly ask: Ask; |
| @@ -58,8 +60,12 @@ export function workerAsker<Ask, Answer>(url: string): (ask: Ask) => Promise<Ans | ||
| 58 | 60 | |
| 59 | 61 | declare const self: Worker; |
| 60 | 62 | |
| 61 | /** The worker's side: answer each ask with `work`'s result, or with why it threw. */ | |
| 62 | export function answerAsks<Ask, Answer>(work: (ask: Ask) => Answer): void { | |
| 63 | /** | |
| 64 | * The worker's side: write its `memory worker <name>` line, and answer each ask with | |
| 65 | * `work`'s result, or with why it threw. | |
| 66 | */ | |
| 67 | export function answerAsks<Ask, Answer>(name: string, work: (ask: Ask) => Answer): void { | |
| 68 | workerMemory(name); | |
| 63 | 69 | self.onmessage = (e: MessageEvent<Asked<Ask>>) => { |
| 64 | 70 | const { id, ask } = e.data; |
| 65 | 71 | try { |
src/looks/thumb_worker.ts +3
| @@ -4,9 +4,12 @@ | ||
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | import { reduceHere, type ThumbAnswer, type ThumbAsk } from "./thumb.ts"; |
| 7 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 7 | 8 | |
| 8 | 9 | declare const self: Worker; |
| 9 | 10 | |
| 11 | workerMemory("thumb"); | |
| 12 | ||
| 10 | 13 | self.onmessage = (e: MessageEvent<ThumbAsk>) => { |
| 11 | 14 | const { id, path, size } = e.data; |
| 12 | 15 | try { |
src/perf/memory_watch.ts +21
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | * ran inside the stall. |
| 14 | 14 | */ |
| 15 | 15 | import { heapSize, memoryUsage as jscMemory } from "bun:jsc"; |
| 16 | import { createLog } from "../kernel/log.ts"; | |
| 16 | 17 | |
| 17 | 18 | const MIB = 1048576; |
| 18 | 19 | export const FIRST_HOUR_MS = 3_600_000; |
| @@ -119,3 +120,23 @@ export function startWorkerMemory(name: string, write: (line: string) => void, e | ||
| 119 | 120 | (timer as { unref?: () => void }).unref?.(); |
| 120 | 121 | return () => clearInterval(timer); |
| 121 | 122 | } |
| 123 | ||
| 124 | /** | |
| 125 | * How often a worker writes its line: a minute, or `FORGE_WORKER_MEMORY_MS` (a test seam, | |
| 126 | * a positive integer). | |
| 127 | */ | |
| 128 | export function workerMemoryEvery(env: Record<string, string | undefined> = process.env): number { | |
| 129 | const raw = env.FORGE_WORKER_MEMORY_MS; | |
| 130 | const n = raw !== undefined && /^\d{1,9}$/.test(raw) ? Number(raw) : 0; | |
| 131 | return n > 0 ? n : 60_000; | |
| 132 | } | |
| 133 | ||
| 134 | /** | |
| 135 | * The call each worker entry makes once at start: its `memory worker <name>` line on the | |
| 136 | * `memory` log, each minute. The timer is unref'd, so a one-shot worker is never kept | |
| 137 | * alive by it, and one that ends inside its first minute writes no line. | |
| 138 | */ | |
| 139 | export function workerMemory(name: string): () => void { | |
| 140 | const log = createLog("memory"); | |
| 141 | return startWorkerMemory(name, (line) => log.info(line), workerMemoryEvery()); | |
| 142 | } |
src/runs/log_writer.ts +3
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | 7 | import { appendTo } from "./log_append.ts"; |
| 8 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 8 | 9 | |
| 9 | 10 | /** What the writer is given: a batch number, the shared counter, and `[runs root, file, text]` per file. */ |
| 10 | 11 | export interface LogBatch { |
| @@ -15,6 +16,8 @@ export interface LogBatch { | ||
| 15 | 16 | |
| 16 | 17 | declare const self: Worker; |
| 17 | 18 | |
| 19 | workerMemory("log-writer"); | |
| 20 | ||
| 18 | 21 | self.onmessage = (e: MessageEvent<LogBatch>) => { |
| 19 | 22 | for (const [root, path, text] of e.data.items) { |
| 20 | 23 | try { |
src/snapshot/mirror_worker.ts +3
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | import { mirrorPass, type MirrorState } from "./mirror.ts"; |
| 15 | 15 | import { storeFrom } from "./remake.ts"; |
| 16 | 16 | import type { StoreSpec } from "./store.ts"; |
| 17 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 17 | 18 | |
| 18 | 19 | export interface MirrorJob { |
| 19 | 20 | readonly dataDir: string; |
| @@ -25,6 +26,8 @@ export type MirrorAnswer = { readonly state: MirrorState } | { readonly error: s | ||
| 25 | 26 | |
| 26 | 27 | declare const self: Worker; |
| 27 | 28 | |
| 29 | workerMemory("mirror"); | |
| 30 | ||
| 28 | 31 | self.onmessage = async (event: MessageEvent<MirrorJob>): Promise<void> => { |
| 29 | 32 | const job = event.data; |
| 30 | 33 | try { |
src/snapshot/vacuum_worker.ts +3
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | /** The worker side of `vacuum.ts`: copy `from` into `to` and say so. */ |
| 2 | 2 | |
| 3 | 3 | import { Database } from "bun:sqlite"; |
| 4 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 4 | 5 | |
| 5 | 6 | /** What the worker is asked: the file to read and the copy to make. */ |
| 6 | 7 | export interface VacuumJob { |
| @@ -10,6 +11,8 @@ export interface VacuumJob { | ||
| 10 | 11 | |
| 11 | 12 | declare const self: Worker; |
| 12 | 13 | |
| 14 | workerMemory("vacuum"); | |
| 15 | ||
| 13 | 16 | self.onmessage = (e: MessageEvent<VacuumJob>) => { |
| 14 | 17 | try { |
| 15 | 18 | const db = new Database(e.data.from, { readonly: true }); |
src/system/du_worker.ts +1 −1
| @@ -3,4 +3,4 @@ | ||
| 3 | 3 | import { answerAsks } from "../kernel/worker_ask.ts"; |
| 4 | 4 | import { dirSizes, type DirSize, type DuAsk, treeSize } from "./du.ts"; |
| 5 | 5 | |
| 6 | answerAsks<DuAsk, DirSize[] | number>((ask) => ("dirs" in ask ? dirSizes(ask.dirs) : treeSize(ask.tree))); | |
| 6 | answerAsks<DuAsk, DirSize[] | number>("du", (ask) => ("dirs" in ask ? dirSizes(ask.dirs) : treeSize(ask.tree))); |
src/web/code_rows_worker.ts +3
| @@ -2,9 +2,12 @@ | ||
| 2 | 2 | import { compressBytes, type Encoding } from "../http/compress.ts"; |
| 3 | 3 | import { parseGcode } from "./render/gcode.ts"; |
| 4 | 4 | import { codeRows } from "./code_rows.ts"; |
| 5 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 5 | 6 | |
| 6 | 7 | declare const self: Worker; |
| 7 | 8 | |
| 9 | workerMemory("code-rows"); | |
| 10 | ||
| 8 | 11 | export type Job = |
| 9 | 12 | | { id: number; kind: "rows"; body: Uint8Array; path: string; limit: number } |
| 10 | 13 | | { id: number; kind: "gcode"; body: Uint8Array } |
src/web/map_worker.ts +3
| @@ -12,6 +12,7 @@ import { Database } from "bun:sqlite"; | ||
| 12 | 12 | import type { App } from "../app.ts"; |
| 13 | 13 | import type { PagesConfig } from "../flow/pages_read.ts"; |
| 14 | 14 | import { readMap, type Learned, type MapJob, type MapReading } from "./map_read.ts"; |
| 15 | import { workerMemory } from "../perf/memory_watch.ts"; | |
| 15 | 16 | |
| 16 | 17 | /** What the worker is asked: plain data (never the app's config, which holds objects with methods). */ |
| 17 | 18 | export interface MapAsk { |
| @@ -33,6 +34,8 @@ export interface MapAnswer { | ||
| 33 | 34 | |
| 34 | 35 | declare const self: Worker; |
| 35 | 36 | |
| 37 | workerMemory("map"); | |
| 38 | ||
| 36 | 39 | /** The app the readers that take one are given: its database, its repositories and its settings, nothing that serves. */ |
| 37 | 40 | const apps = new Map<string, App>(); |
| 38 | 41 |
src/web/og/raster_worker.ts +3
| @@ -4,9 +4,12 @@ | ||
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | 6 | import { rasterizer, type RasterAnswer, type RasterAsk } from "./raster.ts"; |
| 7 | import { workerMemory } from "../../perf/memory_watch.ts"; | |
| 7 | 8 | |
| 8 | 9 | declare const self: Worker; |
| 9 | 10 | |
| 11 | workerMemory("raster"); | |
| 12 | ||
| 10 | 13 | self.onmessage = async (e: MessageEvent<RasterAsk>) => { |
| 11 | 14 | const { id, svg } = e.data; |
| 12 | 15 | try { |
test/worker-memory-lines.test.ts added +79
| @@ -0,0 +1,79 @@ | ||
| 1 | /** | |
| 2 | * Every worker VM writes its own `memory worker <name>` line (#12925), not only the | |
| 3 | * engine's. A child process starts two of the forge's workers with the minute shortened | |
| 4 | * through `FORGE_WORKER_MEMORY_MS`, and its output is read until each name appears. | |
| 5 | */ | |
| 6 | import { expect, test } from "bun:test"; | |
| 7 | import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { workerMemoryEvery } from "../src/perf/memory_watch.ts"; | |
| 11 | ||
| 12 | const ROOT = join(import.meta.dir, ".."); | |
| 13 | ||
| 14 | /** Run `script` in a child forge process; stop it once `done(out)` holds, or at the deadline. */ | |
| 15 | async function runChild( | |
| 16 | script: string, | |
| 17 | everyMs: string, | |
| 18 | done: (out: string) => boolean = () => false, | |
| 19 | ): Promise<{ out: string; code: number; ms: number }> { | |
| 20 | const t0 = performance.now(); | |
| 21 | const child = Bun.spawn([process.execPath, "-e", script], { | |
| 22 | cwd: ROOT, | |
| 23 | env: { ...process.env, FORGE_WORKER_MEMORY_MS: everyMs }, | |
| 24 | stdout: "pipe", | |
| 25 | stderr: "pipe", | |
| 26 | }); | |
| 27 | const killer = setTimeout(() => child.kill(), 15_000); | |
| 28 | let out = ""; | |
| 29 | const read = async (stream: ReadableStream<Uint8Array>) => { | |
| 30 | const decoder = new TextDecoder(); | |
| 31 | for await (const chunk of stream) { | |
| 32 | out += decoder.decode(chunk, { stream: true }); | |
| 33 | if (done(out)) child.kill(); | |
| 34 | } | |
| 35 | }; | |
| 36 | const [, , code] = await Promise.all([read(child.stdout), read(child.stderr), child.exited]); | |
| 37 | clearTimeout(killer); | |
| 38 | return { out, code, ms: performance.now() - t0 }; | |
| 39 | } | |
| 40 | ||
| 41 | const named = (out: string) => | |
| 42 | new Set([...out.matchAll(/\[memory\] memory worker ([\w-]+): heap /g)].map((m) => m[1])); | |
outdated
This test passes with a ref d timer too: vacuumFile terminates the worker, so exit is fast either way. Make it fail without unref (a worker that ends by itself, so a held timer would keep the process up until the kill), or assert unref directly. |
||
| 43 | ||
| 44 | test("two workers each write their own memory line, named for themselves", async () => { | |
| 45 | // The child keeps both workers up until the test has read a line from each. | |
| 46 | const script = ` | |
| 47 | new Worker(${JSON.stringify(join(ROOT, "src/flow/pipeline_worker.ts"))}); | |
| 48 | new Worker(${JSON.stringify(join(ROOT, "src/web/code_rows_worker.ts"))}); | |
| 49 | `; | |
| 50 | const { out } = await runChild(script, "50", (o) => named(o).size >= 2); | |
| 51 | expect([...named(out)].sort()).toEqual(["code-rows", "pipeline"]); | |
| 52 | }, 20_000); | |
| 53 | ||
| 54 | test("a worker that ends by itself inside its first interval writes no line and is not held open", async () => { | |
| 55 | // The worker starts its line and then has nothing left to do: with a ref'd timer | |
| 56 | // its loop would never empty, and the process would stay up until the 15 s kill. | |
| 57 | const dir = mkdtempSync(join(tmpdir(), "worker-memory-")); | |
| 58 | try { | |
| 59 | const entry = join(dir, "idle_worker.ts"); | |
| 60 | writeFileSync( | |
| 61 | entry, | |
| 62 | `import { workerMemory } from ${JSON.stringify(join(ROOT, "src/perf/memory_watch.ts"))};\n` + | |
| 63 | `workerMemory("idle");\n`, | |
| 64 | ); | |
| 65 | const script = `new Worker(${JSON.stringify(entry)});`; | |
| 66 | const { out, code, ms } = await runChild(script, "60000"); | |
| 67 | expect(code).toBe(0); | |
| 68 | expect(out).not.toContain("memory worker"); | |
| 69 | expect(ms).toBeLessThan(10_000); | |
| 70 | } finally { | |
| 71 | rmSync(dir, { recursive: true, force: true }); | |
| 72 | } | |
| 73 | }, 20_000); | |
| 74 | ||
| 75 | test("the interval is a minute unless the seam names a positive whole number", () => { | |
| 76 | expect(workerMemoryEvery({})).toBe(60_000); | |
| 77 | expect(workerMemoryEvery({ FORGE_WORKER_MEMORY_MS: "250" })).toBe(250); | |
| 78 | for (const bad of ["0", "-5", "1e3", "abc", ""]) expect(workerMemoryEvery({ FORGE_WORKER_MEMORY_MS: bad })).toBe(60_000); | |
| 79 | }); |
new above
offline
Fixed 1.5 s sleep: poll the output for both names against a deadline so the test is neither slow nor flaky on a loaded machine.