mirror of
https://github.com/stablyai/orca.git
synced 2026-09-23 16:02:24 +00:00
* perf(main): add watchdog boundary memory benchmark Add a repeatable Electron 43 RSS harness that measures the production-built watchdog entry across the child-process and worker-thread boundaries. Record per-trial samples, the median, revision, runtime, and settling procedure for reproducible PR evidence. * perf(main): move hang watchdog into a worker thread Keep main-thread hang detection independent of the blocked Electron event loop without paying for a second ELECTRON_RUN_AS_NODE process. Preserve the marker and telemetry contract while moving timing configuration and heartbeats onto a bundled worker entry. * test(main): smoke packaged hang watchdog worker * fix(main): make packaged watchdog smoke able to fail The smoke reported failure only through process.exitCode, but its finally block quit Electron gracefully, and Electron takes its status from the browser exit code. Every failure mode — entry missing from app.asar, worker error, marker timeout, non-zero worker exit — exited 0 with the diagnostic discarded on stderr, so the required PR check could never go red. Propagate a real status via app.exit, assert the success line in stdout, and surface stderr. Verified against a packaged tree with the entry removed: exit 0 before, exit 1 after. Co-authored-by: Orca <help@stably.ai> --------- Co-authored-by: Orca <help@stably.ai>
109 lines
3.4 KiB
JavaScript
109 lines
3.4 KiB
JavaScript
import { execFileSync } from 'node:child_process'
|
|
import { monitorEventLoopDelay } from 'node:perf_hooks'
|
|
|
|
export function median(values) {
|
|
const sorted = [...values].sort((left, right) => left - right)
|
|
const middle = Math.floor(sorted.length / 2)
|
|
return sorted.length % 2 === 0 ? (sorted[middle - 1] + sorted[middle]) / 2 : sorted[middle]
|
|
}
|
|
|
|
export async function sampleMemory(readRss, readPhysicalFootprint, options) {
|
|
const rssSamples = []
|
|
const physicalFootprintSamples = []
|
|
for (let index = 0; index < options.sampleCount; index += 1) {
|
|
rssSamples.push(readRss())
|
|
physicalFootprintSamples.push(readPhysicalFootprint())
|
|
await options.sleep(options.sampleIntervalMs)
|
|
}
|
|
return {
|
|
rssBytes: median(rssSamples),
|
|
physicalFootprintBytes: median(physicalFootprintSamples)
|
|
}
|
|
}
|
|
|
|
export function childRssBytes(pid) {
|
|
const raw = execFileSync('ps', ['-o', 'rss=', '-p', String(pid)], {
|
|
encoding: 'utf8'
|
|
}).trim()
|
|
const rssKiB = Number(raw)
|
|
if (!Number.isFinite(rssKiB) || rssKiB <= 0) {
|
|
throw new Error(`Could not read watchdog child RSS for PID ${pid}`)
|
|
}
|
|
return rssKiB * 1024
|
|
}
|
|
|
|
export function parsePhysicalFootprintBytes(output, processCount) {
|
|
const match =
|
|
processCount > 1
|
|
? output.match(/^Summary Footprint:\s+(\d+) B$/m)
|
|
: output.match(/^[^\s].*\sFootprint:\s+(\d+) B/m)
|
|
const bytes = Number(match?.[1])
|
|
return Number.isFinite(bytes) && bytes > 0 ? bytes : null
|
|
}
|
|
|
|
export function physicalFootprintBytes(pids) {
|
|
const pidArgs = pids.flatMap((pid) => ['--pid', String(pid)])
|
|
const output = execFileSync(
|
|
'/usr/bin/footprint',
|
|
[...pidArgs, '--format', 'bytes', '--noCategories'],
|
|
{ encoding: 'utf8' }
|
|
)
|
|
const bytes = parsePhysicalFootprintBytes(output, pids.length)
|
|
if (bytes === null) {
|
|
throw new Error(`Could not read physical footprint for PIDs ${pids.join(', ')}`)
|
|
}
|
|
return bytes
|
|
}
|
|
|
|
export function parseProcessCpuTimeMs(raw) {
|
|
if (!raw.trim()) {
|
|
return null
|
|
}
|
|
const parts = raw.split(':').map(Number)
|
|
if (!parts.length || parts.some((part) => !Number.isFinite(part))) {
|
|
return null
|
|
}
|
|
const seconds = parts.reduce((total, part) => total * 60 + part, 0)
|
|
const milliseconds = seconds * 1_000
|
|
return milliseconds >= 0 ? milliseconds : null
|
|
}
|
|
|
|
function processCpuTimeMs(pid) {
|
|
const raw = execFileSync('ps', ['-o', 'time=', '-p', String(pid)], {
|
|
encoding: 'utf8'
|
|
}).trim()
|
|
const milliseconds = parseProcessCpuTimeMs(raw)
|
|
if (milliseconds === null) {
|
|
throw new Error(`Could not read CPU time for PID ${pid}`)
|
|
}
|
|
return milliseconds
|
|
}
|
|
|
|
function combinedCpuTimeMs(pids) {
|
|
return pids.reduce((total, pid) => total + processCpuTimeMs(pid), 0)
|
|
}
|
|
|
|
export async function sampleProductionPerformance(boundary, options) {
|
|
const loopDelay = monitorEventLoopDelay({ resolution: 10 })
|
|
let heartbeatCount = 0
|
|
const heartbeat = setInterval(() => {
|
|
heartbeatCount += 1
|
|
boundary.sendHeartbeat()
|
|
}, options.heartbeatIntervalMs)
|
|
const cpuBefore = combinedCpuTimeMs(boundary.pids)
|
|
loopDelay.enable()
|
|
try {
|
|
await options.sleep(options.sampleMs)
|
|
} finally {
|
|
loopDelay.disable()
|
|
clearInterval(heartbeat)
|
|
}
|
|
return {
|
|
cpuMs: Math.max(0, combinedCpuTimeMs(boundary.pids) - cpuBefore),
|
|
heartbeatCount,
|
|
eventLoopDelayP95Ms: loopDelay.percentile(95) / 1e6,
|
|
eventLoopDelayP99Ms: loopDelay.percentile(99) / 1e6,
|
|
eventLoopDelayMaxMs: loopDelay.max / 1e6
|
|
}
|
|
}
|