// Executed by the test through docker exec; does not change the API process. import { readFileSync, readdirSync } from 'node:fs'; import { createInterface } from 'node:readline'; import { performance } from 'node:perf_hooks'; const emit = (value) => process.stdout.write(`${JSON.stringify(value)}\n`); const candidates = readdirSync('/proc').filter((name) => { if (!/^\d+$/.test(name) || Number(name) === process.pid) return false; try { const args = readFileSync(`/proc/${name}/cmdline`, 'utf8').split('\0'); return args.some((arg) => /(^|\/)dist\/main\.js$/.test(arg)); } catch { return false; } }); if (candidates.length !== 1) throw new Error('Expected exactly one API process'); const pid = Number(candidates[0]); const startTime = () => readFileSync(`/proc/${pid}/stat`, 'utf8').split(') ').at(-1).split(' ')[19]; const identity = startTime(); function snapshot() { if (startTime() !== identity) throw new Error('API process changed during test'); const status = readFileSync(`/proc/${pid}/status`, 'utf8'); const rss = Number(status.match(/^VmRSS:\s+(\d+) kB$/m)?.[1]) * 1024; const hwm = Number(status.match(/^VmHWM:\s+(\d+) kB$/m)?.[1]) * 1024; if (!Number.isFinite(rss) || !Number.isFinite(hwm)) throw new Error('Missing RSS/HWM metrics'); const events = Object.fromEntries( readFileSync('/sys/fs/cgroup/memory.events', 'utf8') .trim() .split('\n') .map((line) => { const [key, value] = line.split(' '); return [key, Number(value)]; }), ); let rx = 0; let tx = 0; for (const line of readFileSync('/proc/net/dev', 'utf8') .split('\n') .slice(2)) { if (!line.includes(':')) continue; const [name, counters] = line.split(':'); if (name.trim() === 'lo') continue; const values = counters.trim().split(/\s+/).map(Number); rx += values[0]; tx += values[8]; } const cgroup = Number( readFileSync('/sys/fs/cgroup/memory.current', 'utf8').trim(), ); return { rss, hwm, oom: events.oom, oomKill: events.oom_kill, rx, tx, cgroup, }; } let active; function sample() { const value = snapshot(); if (active) { active.peakRss = Math.max(active.peakRss, value.rss); active.peakCgroup = Math.max(active.peakCgroup, value.cgroup); active.maxSampleGapMs = Math.max( active.maxSampleGapMs, performance.now() - active.lastSample, ); active.lastSample = performance.now(); active.samples++; } return value; } const timer = setInterval(() => { if (!active) return; try { sample(); } catch { emit({ event: 'error', message: 'API memory probe failed; process may have restarted', }); process.exit(1); } }, 50); const input = createInterface({ input: process.stdin }); input.on('line', (command) => { try { if (command === 'begin') { if (active) throw new Error('Memory phase already active'); const baseline = snapshot(); active = { baseline, peakRss: baseline.rss, peakCgroup: baseline.cgroup, samples: 0, started: performance.now(), lastSample: performance.now(), maxSampleGapMs: 0, }; emit({ event: 'begin', pid }); } else if (command === 'end') { if (!active) throw new Error('No memory phase active'); const end = sample(); emit({ event: 'summary', pid, baselineRssBytes: active.baseline.rss, sampledPeakRssBytes: active.peakRss, finalRssBytes: end.rss, baselineCgroupBytes: active.baseline.cgroup, sampledPeakCgroupBytes: active.peakCgroup, finalCgroupBytes: end.cgroup, lifetimeHwmBeforeBytes: active.baseline.hwm, lifetimeHwmAfterBytes: end.hwm, oomDelta: end.oom - active.baseline.oom, oomKillDelta: end.oomKill - active.baseline.oomKill, networkRxBytes: end.rx - active.baseline.rx, networkTxBytes: end.tx - active.baseline.tx, samples: active.samples, maxSampleGapMs: active.maxSampleGapMs, durationMs: performance.now() - active.started, }); active = undefined; } else if (command === 'snapshot') { emit({ event: 'snapshot', ...snapshot() }); } else if (command === 'quit') { input.close(); process.stdin.destroy(); } else throw new Error('Unknown memory probe command'); } catch { emit({ event: 'error', message: 'Memory probe command failed' }); process.exit(1); } }); input.on('close', () => clearInterval(timer)); emit({ event: 'ready', pid, instance: readFileSync('/etc/hostname', 'utf8').trim(), intervalMs: 50, });