Watch
1
0
Fork
You've already forked packages
0
packages/markdown/scripts/bench.mjs
Weft packages 306b143104 packages/ at markup-kit dd1e6eb5270e
A snapshot of packages/ in the Weft repository; the official packages are published from these commits.

Markup-Kit-Commit: dd1e6eb5270e62e0c8b6c202b3d01f0da6e00ddc
2026-11-19 20:43:26 +00:00

49 lines
2.6 KiB
JavaScript

// 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=<path to its folder>.
//
// 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)}`)
}