Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

scripts/profile-benchmark.mjs

Raw
import { spawnSync } from "node:child_process";
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";

const [extension, benchmark, ...flags] = process.argv.slice(2);
if (
	!/^[-a-z0-9]+$/.test(extension ?? "") ||
	!/^__bench__\/[a-z0-9-]+\.bench\.ts$/.test(benchmark ?? "") ||
	(flags.length > 0 && (flags.length !== 1 || flags[0] !== "--expose-gc"))
)
	throw new Error(
		"usage: profile-benchmark.mjs <extension> <__bench__/name.bench.ts> [--expose-gc]",
	);
const directory = mkdtempSync(join(tmpdir(), `pi-ext-${extension}-profile-`));
const result = spawnSync(
	process.execPath,
	[
		"--no-warnings",
		"--experimental-strip-types",
		...flags,
		"--cpu-prof",
		"--cpu-prof-dir",
		directory,
		"--cpu-prof-name",
		`${extension}.cpuprofile`,
		benchmark,
	],
	{ stdio: "inherit" },
);
console.log(`CPU profile: ${directory}`);
if (result.error) throw result.error;
if (result.signal) process.kill(process.pid, result.signal);
process.exitCode = result.status ?? 1;