// Times @weft/markdown on the VM against markdown-it on Node (JIT and --jitless), on the same text: the CommonMark // spec's examples, joined and repeated. Needs markdown-it: MARKDOWN_IT=. // // node packages/markdown/scripts/bench.mjs commonmark.json 1,10,50 import { spawnSync } from "node:child_process" import { readFileSync, writeFileSync } from "node:fs" 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 spec = process.argv[2] const sizes = (process.argv[3] ?? "1,10,50").split(",").map(Number) const lib = process.env.MARKDOWN_IT const script = ` const MarkdownIt = require(${JSON.stringify(lib)}) const fs = require("fs") const cases = JSON.parse(fs.readFileSync(${JSON.stringify(spec)}, "utf8")) const text = cases.map((c) => c.markdown).join("\\n\\n").repeat(Number(process.argv[2])) const md = new MarkdownIt({ html: false, linkify: true }) const start = process.hrtime.bigint() const html = md.render(text) console.log(Number(process.hrtime.bigint() - start) / 1e6, text.length, html.length) ` const file = join(process.env.TMPDIR ?? "/workspace/core/target/tools", "bench-markdown-it.cjs") writeFileSync(file, script) function ms(cmd, args, env = {}) { const start = process.hrtime.bigint() const r = spawnSync(cmd, args, { cwd: root, encoding: "utf8", env: { ...process.env, ...env } }) if (r.status !== 0) throw new Error(r.stderr) return { ms: Number(process.hrtime.bigint() - start) / 1e6, out: r.stdout.trim() } } const limits = { WEFT_FUEL: "100000000000", WEFT_MEMORY_MB: "4000" } const base = Math.min(...[0, 1, 2].map(() => ms(weft, ["run", "bench/bench.weft", "0"], limits).ms)) console.log(`VM start-up (loading the package and the spec's text): ${base.toFixed(0)} ms`) console.log("size KB | weft VM ms | markdown-it JIT ms | markdown-it --jitless ms | VM / JIT | VM / jitless") for (const n of sizes) { const vm = Math.min(...[0, 1, 2].map(() => ms(weft, ["run", "bench/bench.weft", String(n)], limits).ms)) - base const best = (flags) => { const runs = [0, 1, 2].map(() => ms("node", [...flags, file, String(n)]).out.split(" ").map(Number)) return { ms: Math.min(...runs.map((r) => r[0])), size: runs[0][1] } } const jit = best([]) const nojit = best(["--jitless"]) console.log(`${(jit.size / 1024).toFixed(0).padStart(7)} | ${vm.toFixed(0).padStart(10)} | ${jit.ms.toFixed(1).padStart(18)} | ${nojit.ms.toFixed(1).padStart(24)} | ${(vm / jit.ms).toFixed(1).padStart(8)} | ${(vm / nojit.ms).toFixed(1).padStart(12)}`) }