Skip to content

Commit edf5575

Browse files
ci: omit unchanged bundle size scenarios (#7856)
1 parent 49bd9fa commit edf5575

3 files changed

Lines changed: 184 additions & 22 deletions

File tree

‎benchmarks/bundle-size/package.json‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"private": true,
44
"type": "module",
55
"scripts": {
6-
"build": "node ../../scripts/benchmarks/bundle-size/measure.mjs --skip-package-builds"
6+
"build": "node ../../scripts/benchmarks/bundle-size/measure.mjs --skip-package-builds",
7+
"test:unit": "node --test ../../scripts/benchmarks/bundle-size/pr-report.test.mjs"
78
},
89
"nx": {
910
"targets": {
@@ -23,6 +24,14 @@
2324
"target": "build"
2425
}
2526
]
27+
},
28+
"test:unit": {
29+
"inputs": [
30+
"default",
31+
"^production",
32+
"{workspaceRoot}/scripts/benchmarks/bundle-size/pr-report.mjs",
33+
"{workspaceRoot}/scripts/benchmarks/bundle-size/pr-report.test.mjs"
34+
]
2635
}
2736
}
2837
},

‎scripts/benchmarks/bundle-size/pr-report.mjs‎

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,16 @@ async function main() {
268268
? resolveBaselineFromCurrentJson(baselineCurrent)
269269
: resolveBaselineFromHistory(historyEntries, args.baseSha)
270270

271+
const metrics = current.metrics || []
271272
const rows = []
272273

273-
for (const metric of current.metrics || []) {
274+
for (const metric of metrics) {
274275
const baselineValue = baseline.benchesByName.get(metric.id)
276+
277+
if (Number.isFinite(baselineValue) && metric.gzipBytes === baselineValue) {
278+
continue
279+
}
280+
275281
const historySeries = (seriesByScenario.get(metric.id) || []).slice(
276282
// Reserve one slot for the current metric so the sparkline stays at trendPoints.
277283
-args.trendPoints + 1,
@@ -290,6 +296,7 @@ async function main() {
290296
raw: metric.rawBytes,
291297
brotli: metric.brotliBytes,
292298
initial: metric.initialGzipBytes,
299+
hasBaseline: Number.isFinite(baselineValue),
293300
deltaCell: formatDelta(metric.gzipBytes, baselineValue),
294301
trendCell: sparkline(historySeries.slice(-args.trendPoints)),
295302
})
@@ -299,30 +306,45 @@ async function main() {
299306
lines.push(args.marker)
300307
lines.push('## Bundle Size Benchmarks')
301308
lines.push('')
302-
lines.push(`- Commit: \`${formatShortSha(current.sha)}\``)
303-
lines.push(
304-
`- Measured at: \`${current.measuredAt || current.generatedAt || 'unknown'}\``,
305-
)
306-
lines.push(`- Baseline source: \`${baseline.source}\``)
307-
if (args.dashboardUrl) {
308-
lines.push(`- Dashboard: [bundle-size history](${args.dashboardUrl})`)
309-
}
310-
lines.push('')
311-
lines.push(
312-
'| Scenario | Current (gzip) | Delta vs baseline | Initial gzip | Raw | Brotli | Trend |',
313-
)
314-
lines.push('| --- | ---: | ---: | ---: | ---: | ---: | --- |')
315309

316-
for (const row of rows) {
310+
if (metrics.length > 0 && rows.length === 0) {
317311
lines.push(
318-
`| \`${row.id}\` | ${formatBytes(row.current)} | ${row.deltaCell} | ${formatBytes(row.initial)} | ${formatBytes(row.raw)} | ${formatBytes(row.brotli)} | ${row.trendCell} |`,
312+
'This pull request does not affect bundle size in any measured scenario.',
319313
)
320-
}
314+
} else if (metrics.length === 0) {
315+
lines.push('No bundle-size scenarios were measured.')
316+
} else {
317+
lines.push(`- Commit: \`${formatShortSha(current.sha)}\``)
318+
lines.push(
319+
`- Measured at: \`${current.measuredAt || current.generatedAt || 'unknown'}\``,
320+
)
321+
lines.push(`- Baseline source: \`${baseline.source}\``)
322+
if (args.dashboardUrl) {
323+
lines.push(`- Dashboard: [bundle-size history](${args.dashboardUrl})`)
324+
}
325+
lines.push('')
326+
lines.push(
327+
rows.some((row) => !row.hasBaseline)
328+
? 'The following scenarios have bundle-size changes or lack baseline data for comparison:'
329+
: 'The following scenarios have bundle-size changes compared with the baseline:',
330+
)
331+
lines.push('')
332+
lines.push(
333+
'| Scenario | Current (gzip) | Delta vs baseline | Initial gzip | Raw | Brotli | Trend |',
334+
)
335+
lines.push('| --- | ---: | ---: | ---: | ---: | ---: | --- |')
321336

322-
lines.push('')
323-
lines.push(
324-
'_Current gzip tracks all emitted client JS chunks. Initial gzip tracks only the entry/import graph. Trend sparkline is historical current gzip ending with this PR measurement; lower is better._',
325-
)
337+
for (const row of rows) {
338+
lines.push(
339+
`| \`${row.id}\` | ${formatBytes(row.current)} | ${row.deltaCell} | ${formatBytes(row.initial)} | ${formatBytes(row.raw)} | ${formatBytes(row.brotli)} | ${row.trendCell} |`,
340+
)
341+
}
342+
343+
lines.push('')
344+
lines.push(
345+
'_Current gzip tracks all emitted client JS chunks. Initial gzip tracks only the entry/import graph. Trend sparkline is historical current gzip ending with this PR measurement; lower is better._',
346+
)
347+
}
326348

327349
const markdown = lines.join('\n') + '\n'
328350
await fsp.mkdir(path.dirname(outputPath), { recursive: true })
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import assert from 'node:assert/strict'
2+
import { execFile } from 'node:child_process'
3+
import { promises as fs } from 'node:fs'
4+
import os from 'node:os'
5+
import path from 'node:path'
6+
import { promisify } from 'node:util'
7+
import test from 'node:test'
8+
9+
const execFileAsync = promisify(execFile)
10+
const reportScript = new URL('./pr-report.mjs', import.meta.url)
11+
12+
function metric(id, gzipBytes) {
13+
return {
14+
id,
15+
gzipBytes,
16+
rawBytes: gzipBytes * 3,
17+
brotliBytes: gzipBytes - 100,
18+
initialGzipBytes: gzipBytes - 10,
19+
}
20+
}
21+
22+
async function generateReport({ current, baseline, history, baseSha }) {
23+
const fixtureDir = await fs.mkdtemp(
24+
path.join(os.tmpdir(), 'bundle-size-pr-report-'),
25+
)
26+
27+
try {
28+
const currentPath = path.join(fixtureDir, 'current.json')
29+
const outputPath = path.join(fixtureDir, 'report.md')
30+
const args = [
31+
reportScript.pathname,
32+
'--current',
33+
currentPath,
34+
'--output',
35+
outputPath,
36+
]
37+
38+
await fs.writeFile(currentPath, JSON.stringify(current))
39+
40+
if (baseline) {
41+
const baselinePath = path.join(fixtureDir, 'baseline.json')
42+
await fs.writeFile(baselinePath, JSON.stringify(baseline))
43+
args.push('--baseline', baselinePath)
44+
}
45+
46+
if (history) {
47+
const historyPath = path.join(fixtureDir, 'history.json')
48+
await fs.writeFile(historyPath, JSON.stringify(history))
49+
args.push('--history', historyPath)
50+
}
51+
52+
if (baseSha) {
53+
args.push('--base-sha', baseSha)
54+
}
55+
56+
await execFileAsync(process.execPath, args)
57+
return await fs.readFile(outputPath, 'utf8')
58+
} finally {
59+
await fs.rm(fixtureDir, { recursive: true, force: true })
60+
}
61+
}
62+
63+
function currentJson(metrics) {
64+
return {
65+
benchmarkName: 'Bundle Size (gzip)',
66+
measuredAt: '2026-07-19T09:45:56.787Z',
67+
sha: '126aa13f296b1234',
68+
metrics,
69+
}
70+
}
71+
72+
test('renders a concise message when no measured scenario changed', async () => {
73+
const current = currentJson([metric('react-router.minimal', 1_000)])
74+
const report = await generateReport({ current, baseline: current })
75+
76+
assert.equal(
77+
report,
78+
'<!-- bundle-size-benchmark -->\n## Bundle Size Benchmarks\n\nThis pull request does not affect bundle size in any measured scenario.\n',
79+
)
80+
})
81+
82+
test('renders only scenarios that changed against the historical baseline', async () => {
83+
const current = currentJson([
84+
metric('react-router.minimal', 1_000),
85+
metric('react-router.full', 1_200),
86+
])
87+
const history = {
88+
entries: {
89+
[current.benchmarkName]: [
90+
{
91+
commit: { id: 'baseline-sha' },
92+
benches: [
93+
{ name: 'react-router.minimal', value: 1_000 },
94+
{ name: 'react-router.full', value: 1_100 },
95+
],
96+
},
97+
],
98+
},
99+
}
100+
const report = await generateReport({
101+
current,
102+
history,
103+
baseSha: 'baseline-sha',
104+
})
105+
106+
assert.match(
107+
report,
108+
/The following scenarios have bundle-size changes compared with the baseline:/,
109+
)
110+
assert.doesNotMatch(report, /`react-router\.minimal`/)
111+
assert.match(
112+
report,
113+
/\| `react-router\.full` \| 1\.17 KiB \| \+100 B \(\+9\.09%\) \|/,
114+
)
115+
})
116+
117+
test('keeps scenarios that do not have baseline data visible', async () => {
118+
const current = currentJson([
119+
metric('react-router.minimal', 1_000),
120+
metric('new-scenario', 900),
121+
])
122+
const baseline = currentJson([metric('react-router.minimal', 1_000)])
123+
const report = await generateReport({ current, baseline })
124+
125+
assert.match(
126+
report,
127+
/The following scenarios have bundle-size changes or lack baseline data for comparison:/,
128+
)
129+
assert.doesNotMatch(report, /`react-router\.minimal`/)
130+
assert.match(report, /\| `new-scenario` \| 900 B \| n\/a \|/)
131+
})

0 commit comments

Comments
 (0)