// Runs tests/hostile.weft: each input at n and 2n, timed from outside the VM (the clock doesn't move within a turn). // A size that doubles must not take much more than twice as long. // // node packages/markdown/scripts/hostile.mjs [n] import { spawnSync } from "node:child_process" import { dirname, join } from "node:path" import { fileURLToPath } from "node:url" const root = join(dirname(fileURLToPath(import.meta.url)), "..") const weft = process.env.WEFT_BIN ?? join(root, "../../core/target/release/weft") const n = Number(process.argv[2] ?? 8000) // The limits are lifted so that a slow path shows up as time rather than as a stop. const env = { ...process.env, WEFT_FUEL: "20000000000", WEFT_MEMORY_MB: "1500" } function run(args) { const start = process.hrtime.bigint() const r = spawnSync(weft, ["run", "tests/hostile.weft", ...args], { cwd: root, env, encoding: "utf8", timeout: 120000 }) return { ms: Number(process.hrtime.bigint() - start) / 1e6, out: r.stdout.trim(), err: r.stderr.trim(), status: r.status } } const names = run(["--list"]).out.split("\n") const base = run(["lines", "0"]).ms let bad = 0 for (const name of names) { const a = run([name, String(n)]) const b = run([name, String(n * 2)]) const [ta, tb] = [Math.max(a.ms - base, 1), Math.max(b.ms - base, 1)] const stopped = a.status !== 0 || b.status !== 0 const quadratic = tb > 1500 && tb / ta > 3.2 if (stopped || quadratic) bad++ const note = stopped ? ` STOPPED: ${(b.err || a.err).split("\n")[0]}` : quadratic ? " QUADRATIC" : "" console.log(`${name.padEnd(24)} ${ta.toFixed(0).padStart(7)} ms ${tb.toFixed(0).padStart(7)} ms${note}`) } const expansion = run(["expansion"]) const size = Number(expansion.out) if (!(size > 0 && size < 1000000)) bad++ console.log(`reference expansion: ${size} bytes of HTML from 50 KB of Markdown`) console.log(bad === 0 ? "ok" : `${bad} failed`) process.exit(bad === 0 ? 0 : 1)