Skip to content

Commit

Permalink
feat(benchmark): Test specific detectors + table report
Browse files Browse the repository at this point in the history
  • Loading branch information
jubnzv committed Aug 30, 2024
1 parent 874853d commit 2091d17
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions benchmarks/detectors.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
import Benchmark from "benchmark";
import { executeMisti } from "../src/cli";
import { getAllDetectors } from "../src/detectors/detector";

const ALL_DETECTORS_NAME = "All Detectors";

const args = process.argv.slice(2);
const filePath = args[0];
const showProgress = args.includes("--progress");

if (!filePath) {
if (!filePath || filePath === "--progress") {
console.error("Error: No file path provided.");
process.exit(1);
}

const suite = new Benchmark.Suite();
const results: { name: string; result: string }[] = [];

suite
.add(`executeMisti --all-detectors ${filePath}`, {
// Add the benchmark for --all-detectors
suite.add(ALL_DETECTORS_NAME, {
defer: true,
async: true,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fn: async function (deferred: any) {
await executeMisti(["--all-detectors", filePath]);
deferred.resolve();
},
});

// Add benchmarks for each individual detector
getAllDetectors().forEach((detectorName) => {
suite.add(`${detectorName}`, {
defer: true,
async: true,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fn: async function (deferred: any) {
await executeMisti(["--all-detectors", filePath]);
await executeMisti([`--detectors`, detectorName, filePath]);
deferred.resolve();
},
})
});
});

suite
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.on("cycle", function (event: any) {
console.log(String(event.target));
if (showProgress) {
console.log(String(event.target));
}
const name =
event.target.name === ALL_DETECTORS_NAME
? `**${event.target.name}**`
: `\`${event.target.name}\``;
const result = [
`${event.target.hz.toFixed(2)} ops/sec`,
`(~${(event.target.stats.mean * 1000).toFixed(2)} ms/run)`,
${event.target.stats.rme.toFixed(2)}%`,
].join(" ");
results.push({ name, result });
})
.on("complete", () => {
console.log("| Detector Name | Result |");
console.log("|---------------|--------|");
results.forEach(({ name, result }) => {
console.log(`| ${name} | ${result} |`);
});
})
.on("complete", () => {})
.run({ async: true });

0 comments on commit 2091d17

Please sign in to comment.