oxedyne/fe2o3/fe2o3_austenite/tools/bench/wasm_bench.mjs
2.2 KiB, 1 run, executable
created by r1870400018:60954, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #!/usr/bin/env node |
| 2 | // S0 wasm full-compile bench: Austenite wasm vs typst.ts wasm, in node, driven |
| 3 | // as Daimond drives them (see lib/wasm_common.mjs for the one deviation -- |
| 4 | // fs reads in place of the browser's fetch). One process per (doc, engine) |
| 5 | // invocation, so the memory cap and peak-RSS reading in wasm_bench_runner.sh |
| 6 | // isolate one engine at a time; warm-ups and measured runs share the one long- |
| 7 | // lived compiler instance, matching how Daimond keeps it (see getCompiler()). |
| 8 | // |
| 9 | // Usage: wasm_bench.mjs --engine=austenite|typstts --vendor=DIR --doc=FILE |
| 10 | // [--warmups=N] [--runs=N] [--fmt=pdf|vector] |
| 11 | // Prints one JSON object to stdout: { doc, engine, fmt, runs: [wall_s...], ok }. |
| 12 | import { loadEngine, projectFromFile, timeOnce } from './lib/wasm_common.mjs'; |
| 13 | |
| 14 | function parseArgs(argv) { |
| 15 | const out = { warmups: 1, runs: 3, fmt: 'pdf' }; |
| 16 | for (const a of argv.slice(2)) { |
| 17 | const m = a.match(/^--([^=]+)=(.*)$/); |
| 18 | if (!m) continue; |
| 19 | const [, k, v] = m; |
| 20 | out[k] = /^\d+$/.test(v) ? Number(v) : v; |
| 21 | } |
| 22 | return out; |
| 23 | } |
| 24 | |
| 25 | async function main() { |
| 26 | const args = parseArgs(process.argv); |
| 27 | for (const req of ['engine', 'vendor', 'doc']) { |
| 28 | if (!args[req]) { |
| 29 | process.stderr.write(`wasm_bench: --${req} is required\n`); |
| 30 | process.exit(2); |
| 31 | } |
| 32 | } |
| 33 | const engine = await loadEngine(args.engine, args.vendor); |
| 34 | const project = projectFromFile(args.doc); |
| 35 | const compileFn = args.fmt === 'vector' ? engine.compileVector : engine.compilePdf; |
| 36 | |
| 37 | for (let i = 0; i < args.warmups; i++) { |
| 38 | const { ret } = timeOnce(() => compileFn.call(engine, project)); |
| 39 | if (ret && ret.error) { |
| 40 | process.stderr.write(`wasm_bench: warm-up compile error: ${ret.error}\n`); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | const runs = []; |
| 45 | let ok = true; |
| 46 | for (let i = 0; i < args.runs; i++) { |
| 47 | const { wall_s, ret } = timeOnce(() => compileFn.call(engine, project)); |
| 48 | if (!ret || ret.error) { |
| 49 | ok = false; |
| 50 | process.stderr.write(`wasm_bench: run ${i} error: ${ret && ret.error}\n`); |
| 51 | } |
| 52 | runs.push(wall_s); |
| 53 | } |
| 54 | |
| 55 | process.stdout.write( |
| 56 | JSON.stringify({ doc: args.doc, engine: args.engine, fmt: args.fmt, runs, ok }) + '\n' |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | main().catch((e) => { |
| 61 | process.stdout.write(JSON.stringify({ ok: false, error: String((e && e.stack) || e) }) + '\n'); |
| 62 | process.exit(1); |
| 63 | }); |