Support section filter in benchmarks

This commit is contained in:
Igor Savin
2026-03-18 23:58:30 +02:00
committed by Jay Linski
parent 61659fbe63
commit 60d17f546d
2 changed files with 60 additions and 29 deletions
+7
View File
@@ -115,6 +115,13 @@ npm run bench
# Run with a custom label # Run with a custom label
npm run bench -- --label my-optimization npm run bench -- --label my-optimization
# Filter templates by name (regex, case-insensitive)
npm run bench -- --grep "complex|recursive"
# Run only specific sections (regex, case-insensitive)
npm run bench -- --section precompil
npm run bench -- --section "compilation|precompil"
# Compare results # Compare results
npm run bench:compare npm run bench:compare
+53 -29
View File
@@ -51,6 +51,17 @@ if (grepIdx !== -1 && args[grepIdx + 1]) {
} }
} }
const sectionIdx = args.indexOf('--section');
let sectionPattern = null;
if (sectionIdx !== -1 && args[sectionIdx + 1]) {
try {
sectionPattern = new RegExp(args[sectionIdx + 1], 'i');
} catch (e) {
console.error(`Invalid --section pattern: ${e.message}`);
process.exit(1);
}
}
// ─── Filter templates ─────────────────────────────────────────────────────── // ─── Filter templates ───────────────────────────────────────────────────────
const templates = Object.fromEntries( const templates = Object.fromEntries(
@@ -92,7 +103,14 @@ function createEnv(def) {
return hb; return hb;
} }
function shouldRunSection(title) {
if (!sectionPattern) return true;
return sectionPattern.test(title);
}
async function runSection(title, config, setup) { async function runSection(title, config, setup) {
if (!shouldRunSection(title)) return null;
printSectionHeader(title); printSectionHeader(title);
const bench = newBench(config); const bench = newBench(config);
@@ -120,6 +138,11 @@ async function run() {
const allSections = []; const allSections = [];
if (sectionPattern) {
console.log(`Filtering sections: /${sectionPattern.source}/i`);
console.log();
}
// ─── COMPILATION ──────────────────────────────────────────────────────────── // ─── COMPILATION ────────────────────────────────────────────────────────────
allSections.push( allSections.push(
@@ -143,40 +166,41 @@ async function run() {
const expectedOutputs = {}; const expectedOutputs = {};
allSections.push( const execResult = await runSection(
await runSection( 'EXECUTION (template rendering)',
'EXECUTION (template rendering)', EXEC_BENCH_CONFIG,
EXEC_BENCH_CONFIG, (bench) => {
(bench) => { for (const [name, def] of Object.entries(templates)) {
for (const [name, def] of Object.entries(templates)) { const compiled = createEnv(def).compile(def.template);
const compiled = createEnv(def).compile(def.template); expectedOutputs[name] = compiled(def.context);
expectedOutputs[name] = compiled(def.context); bench.add(`exec: ${name}`, () => {
bench.add(`exec: ${name}`, () => { compiled(def.context);
compiled(def.context); });
});
}
} }
) }
); );
allSections.push(execResult);
// Verify outputs haven't changed during benchmarking // Verify outputs haven't changed during benchmarking
let verifyFails = 0; if (execResult) {
for (const [name, def] of Object.entries(templates)) { let verifyFails = 0;
const compiled = createEnv(def).compile(def.template); for (const [name, def] of Object.entries(templates)) {
const actual = compiled(def.context); const compiled = createEnv(def).compile(def.template);
if (actual !== expectedOutputs[name]) { const actual = compiled(def.context);
console.warn( if (actual !== expectedOutputs[name]) {
` WARNING: output mismatch for "${name}"\n expected: ${JSON.stringify(expectedOutputs[name])}\n actual: ${JSON.stringify(actual)}` console.warn(
); ` WARNING: output mismatch for "${name}"\n expected: ${JSON.stringify(expectedOutputs[name])}\n actual: ${JSON.stringify(actual)}`
verifyFails++; );
verifyFails++;
}
} }
if (verifyFails === 0) {
console.log('Output verification: all templates OK');
} else {
console.warn(`Output verification: ${verifyFails} mismatch(es)`);
}
console.log();
} }
if (verifyFails === 0) {
console.log('Output verification: all templates OK');
} else {
console.warn(`Output verification: ${verifyFails} mismatch(es)`);
}
console.log();
// ─── PRECOMPILATION ───────────────────────────────────────────────────────── // ─── PRECOMPILATION ─────────────────────────────────────────────────────────
@@ -249,7 +273,7 @@ async function run() {
) )
); );
const filepath = saveMarkdownReport(allSections, { const filepath = saveMarkdownReport(allSections.filter(Boolean), {
label, label,
compileConfig: COMPILE_BENCH_CONFIG, compileConfig: COMPILE_BENCH_CONFIG,
execConfig: EXEC_BENCH_CONFIG, execConfig: EXEC_BENCH_CONFIG,