|
| 1 | +import "./build.mts"; |
| 2 | + |
| 3 | +import os from "os"; |
| 4 | +import path from "path"; |
| 5 | +import fs from "fs/promises"; |
| 6 | +import cp from "child_process"; |
| 7 | +import { promisify } from "util"; |
| 8 | + |
| 9 | +import degit from "degit"; |
| 10 | +import { group, bench, run } from "mitata"; |
| 11 | + |
| 12 | +const exec = promisify(cp.exec); |
| 13 | +const api = (await import("./dist/index.mjs")) as unknown as typeof import("./src"); |
| 14 | +const FIXTURES = path.resolve("src/__tests__/fixtures"); |
| 15 | +const GREP = new RegExp(process.env.GREP || ".", "g"); |
| 16 | +const COMPARE = process.env.COMPARE; |
| 17 | +let compareAPI: undefined | typeof api; |
| 18 | + |
| 19 | +if (COMPARE) { |
| 20 | + const cwd = path.join(os.tmpdir(), `htmljs-bench-${COMPARE}`); |
| 21 | + const exists = await fs.mkdir(cwd).catch(() => true); |
| 22 | + if (!exists) { |
| 23 | + await degit(`marko-js/htmljs-parser#${COMPARE}`).clone(cwd); |
| 24 | + await exec("npm ci && npm run --if-present build", { cwd }); |
| 25 | + } |
| 26 | + |
| 27 | + const pkg = JSON.parse( |
| 28 | + await fs.readFile(path.join(cwd, "package.json"), "utf-8") |
| 29 | + ); |
| 30 | + compareAPI = await import( |
| 31 | + path.join(cwd, pkg.exports?.["."].import ?? pkg.main) |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +for (const entry of await fs.readdir(FIXTURES)) { |
| 36 | + if (!GREP.test(entry)) continue; |
| 37 | + const filename = path.join(FIXTURES, entry, "input.marko"); |
| 38 | + const src = await fs.readFile(filename, "utf-8"); |
| 39 | + const check = (mod: typeof api) => () => mod.createParser({}).parse(src); |
| 40 | + if (compareAPI) { |
| 41 | + group(() => { |
| 42 | + bench(entry, check(api)); |
| 43 | + bench(`${entry}#${COMPARE}`, check(compareAPI)); |
| 44 | + }); |
| 45 | + } else { |
| 46 | + bench(entry, check(api)); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +await run(); |
0 commit comments