Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions packages/tools/benchmark-tests/scripts/compare-benchmark.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { existsSync, readFileSync, writeFileSync } from 'node:fs';

const loadJson = (path) => {
return existsSync(path) ? JSON.parse(readFileSync(path, 'utf-8')) : [];
};
const loadJson = (path) => (existsSync(path) ? JSON.parse(readFileSync(path, 'utf-8')) : []);

const current = loadJson('benchmark-result.json');
const baseline = loadJson('benchmark-baseline.json');
Expand Down Expand Up @@ -41,21 +39,21 @@ for (const entry of current) {
});
}

const top5 = rows
.filter((r) => r.percent !== null)
.sort((a, b) => Math.abs(b.percent) - Math.abs(a.percent))
const flop5 = rows
.filter((r) => typeof r.percent === 'number' && r.percent > 0)
.sort((a, b) => b.percent - a.percent)
.slice(0, 5);

const rest = rows.filter((r) => !top5.includes(r)).sort((a, b) => a.name.localeCompare(b.name));
const rest = rows.filter((r) => !flop5.includes(r)).sort((a, b) => a.name.localeCompare(b.name));

let markdown = `## Hydration Benchmark Report (vs Baseline)\n\n`;

markdown += `### 📊 Top 5 Änderungen\n\n`;
markdown += `### 📊 Flop 5 Regressions\n\n`;
markdown += `| Component | Current | Baseline | Δ% | Result |\n`;
markdown += `|-----------|---------|----------|-----|--------|\n`;
markdown += top5.map((r) => r.markdown).join('\n') + '\n\n';
markdown += flop5.map((r) => r.markdown).join('\n') + '\n\n';

markdown += `<details>\n<summary>📋 Alle Ergebnisse anzeigen</summary>\n\n`;
markdown += `<details>\n<summary>📋 Show all results</summary>\n\n`;
markdown += `| Component | Current | Baseline | Δ% | Result |\n`;
markdown += `|-----------|---------|----------|-----|--------|\n`;
markdown += rest.map((r) => r.markdown).join('\n') + '\n';
Expand Down
51 changes: 30 additions & 21 deletions packages/tools/benchmark-tests/tests/benchmark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,24 @@ const TAGS = [

type TagType = (typeof TAGS)[number];

const results: {
type ResultEntry = {
name: TagType;
value: number;
values: number[];
unit: 'ms';
}[] = [];
};

const TEST_ITERATIONS = parseInt(process.env.TEST_ITERATIONS || '1', 10);
const results: Map<TagType, ResultEntry> = new Map();

const TEST_ITERATIONS = Math.max(parseInt(process.env.TEST_ITERATIONS || '1', 10), !!process.env.CI ? 5 : 1);
const TEST_TIMEOUT = parseInt(process.env.TEST_TIMEOUT || '5000', 10);

test.beforeEach(async ({ page }) => {
await page.goto('http://localhost:3000/test-page.html');
});

for (const tag of TAGS) {
test(`${tag} hydrates`, async ({ page }) => {
const durations: number[] = [];

for (let idx = 0; idx < TEST_ITERATIONS; idx++) {
for (let idx = 0; idx < TEST_ITERATIONS; idx++) {
test(`${tag} hydration iteration ${idx + 1}`, async ({ page }) => {
await page.evaluate(() => window.gc?.());

const duration = await page.evaluate(
Expand All @@ -92,7 +92,6 @@ for (const tag of TAGS) {
function cleanup() {
if (cleaned) return;
cleaned = true;

clearTimeout(timeoutId);
observer.disconnect();
if (el.parentNode) el.remove();
Expand All @@ -111,20 +110,30 @@ for (const tag of TAGS) {
{ tag, timeout: TEST_TIMEOUT, idx },
);

durations.push(duration);
}

durations.sort((a, b) => a - b);
const median = durations[Math.floor(durations.length / 2)];

results.push({
name: tag,
value: Math.round(median),
unit: 'ms',
if (!results.has(tag)) {
results.set(tag, {
name: tag,
values: [],
unit: 'ms',
});
}
results.get(tag)!.values.push(Math.round(duration));
});
});
}
}

test.afterAll(() => {
writeFileSync('benchmark-result.json', JSON.stringify(results, null, 2));
const finalResults = Array.from(results.values()).map(({ name, values, unit }) => {
values.sort((a, b) => a - b);
const mid = Math.floor(values.length / 2);
const median = values.length % 2 === 0 ? Math.round((values[mid - 1] + values[mid]) / 2) : values[mid];

return {
name,
value: median,
unit,
};
});

writeFileSync('benchmark-result.json', JSON.stringify(finalResults, null, 2));
});
Loading