import type { ThreadRuntimeSample } from "../memory/thread-memory.js";

export function captureRuntimeSampleStart(): { cpu: NodeJS.CpuUsage; startedAt: bigint } {
  return {
    cpu: process.cpuUsage(),
    startedAt: process.hrtime.bigint(),
  };
}

export function finishRuntimeSample(start: {
  cpu: NodeJS.CpuUsage;
  startedAt: bigint;
}): ThreadRuntimeSample {
  const cpu = process.cpuUsage(start.cpu);
  return {
    cpuSystemMs: cpu.system / 1000,
    cpuUserMs: cpu.user / 1000,
    wallTimeMs: Number(process.hrtime.bigint() - start.startedAt) / 1_000_000,
  };
}
