Skip to content

Commit

Permalink
test: add benchmark script
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Mar 16, 2022
1 parent faa7120 commit b81384d
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"env": {
"node": true,
"mocha": true,
"es2020": true,
"browser": true
"es2020": true
},
"overrides": [
{
Expand Down
78 changes: 78 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
"devDependencies": {
"@commitlint/cli": "^16.2.3",
"@commitlint/config-conventional": "^16.2.1",
"@types/benchmark": "^2.1.1",
"@types/degit": "^2.8.3",
"@types/mocha": "^9.1.0",
"@types/node": "^17.0.21",
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
"benchmark": "^2.1.4",
"degit": "^2.8.4",
"esbuild": "0.14.27",
"esbuild-register": "^3.3.2",
"eslint": "^8.11.0",
Expand Down Expand Up @@ -56,6 +60,7 @@
"url": "https://github.com/marko-js/htmljs-parser.git"
},
"scripts": {
"bench": "node -r esbuild-register ./src/__tests__/bench",
"build": "tsc -b && node -r esbuild-register build",
"ci:test": "nyc npm run mocha -- --forbid-pending --forbid-only",
"format": "npm run lint:eslint -- --fix && npm run lint:prettier -- --write && (fixpack || true)",
Expand Down
88 changes: 88 additions & 0 deletions src/__tests__/bench.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import fs from "fs";
import os from "os";
import path from "path";
import cp from "child_process";
import degit from "degit";
import Benchmark from "benchmark";
import { repository } from "../../package.json";
import { createParser } from "..";

const FIXTURES = path.join(__dirname, "fixtures");
const GREP = new RegExp(process.env.GREP || ".", "g");
const COMPARE = process.env.COMPARE;

(async () => {
let altParser:
| undefined
| {
createParser(handlers: unknown): {
parse(src: string, filename: string): void;
};
};

if (COMPARE) {
const tempDir = await fs.promises.mkdtemp(
path.join(os.tmpdir(), "htmljs-parser-bench")
);
await degit(`${repository.url}#${COMPARE}`).clone(tempDir);
await new Promise<void>((resolve, reject) => {
cp.exec("npm ci && npm run --if-present build", { cwd: tempDir }, (err) =>
err ? reject(err) : resolve()
);
});

altParser = require(tempDir);
}

for (const entry of fs.readdirSync(FIXTURES)) {
const filename = path.join(FIXTURES, entry, "input.marko");
if (!GREP.test(entry)) continue;

const src = await fs.promises.readFile(filename, "utf-8");
const suite = new Benchmark.Suite();
suite.add(entry, () => {
const parser = createParser({
onError() {},
onText() {},
onCDATA() {},
onDoctype() {},
onDeclaration() {},
onComment() {},
onTagName() {},
onTagShorthandId() {},
onTagShorthandClass() {},
onTagVar() {},
onTagArgs() {},
onTagParams() {},
onAttrName() {},
onAttrArgs() {},
onAttrValue() {},
onAttrMethod() {},
onAttrSpread() {},
onOpenTagEnd() {},
onCloseTag() {},
onScriptlet() {},
});
parser.parse(src, filename);
});

if (altParser) {
suite.add(`${entry} #${COMPARE}`, () => {
const parser = altParser!.createParser({});
parser.parse(src, filename);
});
}

suite.on("cycle", (event: Benchmark.Event) => {
console.log(String(event.target));
});

const pending = new Promise((r) => suite.on("complete", r));
suite.run();
await pending;

if (altParser) {
console.log(`Fastest is ${suite.filter("fastest").map("name")}`);
}
}
})();

0 comments on commit b81384d

Please sign in to comment.