Open History to see the last commit
1#!/usr/bin/env node
2β
3import fs from 'node:fs'
4import { promises as fsp } from 'node:fs'
5import path from 'node:path'
6import { parseArgs as parseNodeArgs } from 'node:util'
7import vm from 'node:vm'
8β
9const DEFAULT_MARKER = '<!-- bundle-size-benchmark -->'
10const INT_FORMAT = new Intl.NumberFormat('en-US', {
11 maximumFractionDigits: 0,
12})
13const FIXED_1_FORMAT = new Intl.NumberFormat('en-US', {
14 minimumFractionDigits: 1,
15 maximumFractionDigits: 1,
16})
17const METRIC_KEYS = ['gzipBytes', 'initialGzipBytes', 'rawBytes', 'brotliBytes']
18β
19function parseArgs(argv) {
20 const { values } = parseNodeArgs({
21 args: argv,
22 allowPositionals: false,
23 strict: true,
24 options: {
25 current: { type: 'string' },
26 baseline: { type: 'string' },
27 history: { type: 'string' },
28 output: { type: 'string' },
29 'dashboard-url': { type: 'string' },
30 'base-sha': { type: 'string' },
31 marker: { type: 'string' },
32 'trend-points': { type: 'string' },
33 },
34 })
35β
36 const args = {
37 current: values.current,
38 baseline: values.baseline,
39 history: values.history,
40 output: values.output,
41 dashboardUrl: values['dashboard-url'],
42 baseSha: values['base-sha'],
43 marker: values.marker ?? DEFAULT_MARKER,
44 trendPoints: values['trend-points']
45 ? Number.parseInt(values['trend-points'], 10)
46 : 12,
47 }
48β
49 if (!Number.isFinite(args.trendPoints) || args.trendPoints < 2) {
50 throw new Error(`Invalid trend points: ${values['trend-points']}`)
51 }
52β
53 if (!args.current) {
54 throw new Error('Missing required argument: --current')
55 }
56β
57 if (!args.output) {
58 throw new Error('Missing required argument: --output')
59 }
60β
61 return args
62}
63β
64function parseMaybeDataJs(raw) {
65 const trimmed = raw.trim()
66β
67 if (trimmed.startsWith('window.BENCHMARK_DATA')) {
68 const sandbox = { window: {} }
69 vm.runInNewContext(trimmed, sandbox, { timeout: 1000 })
70 return sandbox.window.BENCHMARK_DATA
71 }
72β
73 return JSON.parse(trimmed)
74}
75β
76function readJsonMaybeData(filePath) {
77 return parseMaybeDataJs(fs.readFileSync(filePath, 'utf8'))
78}
79β
80function readOptionalJsonMaybeData(filePath) {
81 if (!filePath || !fs.existsSync(filePath)) {
82 return undefined
83 }
84β
85 const raw = fs.readFileSync(filePath, 'utf8')
86 if (!raw.trim()) {
87 return undefined
88 }
89β
90 return parseMaybeDataJs(raw)
91}
92β
93function formatBytes(bytes, opts = {}) {
94 const signed = opts.signed === true
95β
96 if (!Number.isFinite(bytes)) {
97 return 'n/a'
98 }
99β
100 const sign = signed && bytes !== 0 ? (bytes > 0 ? '+' : '-') : ''
101 const absBytes = Math.abs(bytes)
102β
103 let value
104 if (absBytes < 1024) {
105 value = `${INT_FORMAT.format(absBytes)} B`
106 } else {
107 const kib = absBytes / 1024
108 if (kib < 1024) {
109 value = `${FIXED_1_FORMAT.format(kib)} KiB`
110 } else {
111 const mib = kib / 1024
112 value = `${FIXED_1_FORMAT.format(mib)} MiB`
113 }
114 }
115β
116 return `${sign}${value}`
117}
118β
119function formatDelta(current, baseline) {
120 if (!Number.isFinite(current) || !Number.isFinite(baseline)) {
121 return 'n/a'
122 }
123β
124 const delta = current - baseline
125 return formatBytes(delta, { signed: true })
126}
127β
128function formatMetricCell(current, baseline) {
129 return `${formatBytes(current)}<br>${formatDelta(current, baseline)}`
130}
131β
132function sparkline(values) {
133 if (!values.length) {
134 return 'n/a'
135 }
136β
137 const blocks = 'βββββ
βββ'
138 const min = Math.min(...values)
139 const max = Math.max(...values)
140β
141 if (max === min) {
142 return 'β
'.repeat(values.length)
143 }
144β
145 return values
146 .map((value) => {
147 const normalized = (value - min) / (max - min)
148 const idx = Math.min(
149 blocks.length - 1,
150 Math.max(0, Math.round(normalized * (blocks.length - 1))),
151 )
152 return blocks[idx]
153 })
154 .join('')
155}
156β
157function normalizeHistoryEntries(history, benchmarkName) {
158 if (!history || typeof history !== 'object' || !history.entries) {
159 return []
160 }
161β
162 const byName = history.entries[benchmarkName]
163 if (Array.isArray(byName)) {
164 return byName
165 }
166β
167 const firstEntry = Object.values(history.entries).find((value) =>
168 Array.isArray(value),
169 )
170 return Array.isArray(firstEntry) ? firstEntry : []
171}
172β
173function buildSeriesByScenario(historyEntries) {
174 const map = new Map()
175β
176 for (const entry of historyEntries) {
177 for (const bench of entry?.benches || []) {
178 if (typeof bench?.name !== 'string' || !Number.isFinite(bench?.value)) {
179 continue
180 }
181β
182 if (!map.has(bench.name)) {
183 map.set(bench.name, [])
184 }
185β
186 map.get(bench.name).push(Number(bench.value))
187 }
188 }
189β
190 return map
191}
192β
193function parseBenchmarkExtra(extra) {
194 if (typeof extra !== 'string') {
195 return {}
196 }
197β
198 const values = {}
199 for (const part of extra.split(';')) {
200 const [key, rawValue] = part.trim().split('=')
201 const value = Number(rawValue)
202 if (key && Number.isFinite(value)) {
203 values[key] = value
204 }
205 }
206β
207 return {
208 rawBytes: values.raw,
209 brotliBytes: values.brotli,
210 initialGzipBytes: values.initial_gzip,
211 }
212}
213β
214function resolveBaselineFromHistory(historyEntries, baseSha) {
215 if (!historyEntries.length) {
216 return {
217 source: 'none',
218 benchesByName: new Map(),
219 }
220 }
221β
222 const baseEntry =
223 (baseSha &&
224 historyEntries.find(
225 (entry) =>
226 entry?.commit?.id === baseSha ||
227 entry?.commit?.id?.startsWith(baseSha),
228 )) ||
229 historyEntries[historyEntries.length - 1]
230β
231 const benchesByName = new Map()
232 for (const bench of baseEntry?.benches || []) {
233 if (typeof bench?.name === 'string' && Number.isFinite(bench?.value)) {
234 benchesByName.set(bench.name, {
235 gzipBytes: Number(bench.value),
236 ...parseBenchmarkExtra(bench.extra),
237 })
238 }
239 }
240β
241 const commitId = baseEntry?.commit?.id || 'unknown'
242β
243 return {
244 source: `history:${commitId.slice(0, 12)}`,
245 benchesByName,
246 }
247}
248β
249function resolveBaselineFromCurrentJson(currentJson) {
250 const benchesByName = new Map()
251 for (const metric of currentJson?.metrics || []) {
252 if (typeof metric?.id === 'string' && Number.isFinite(metric?.gzipBytes)) {
253 benchesByName.set(metric.id, metric)
254 }
255 }
256β
257 const sourceSha =
258 typeof currentJson?.sha === 'string' ? currentJson.sha : 'unknown'
259β
260 return {
261 source: `current:${sourceSha.slice(0, 12)}`,
262 benchesByName,
263 }
264}
265β
266function formatShortSha(value) {
267 if (!value || typeof value !== 'string') {
268 return 'unknown'
269 }
270β
271 return value.slice(0, 12)
272}
273β
274async function main() {
275 const args = parseArgs(process.argv.slice(2))
276 const currentPath = path.resolve(args.current)
277 const outputPath = path.resolve(args.output)
278 const baselinePath = args.baseline ? path.resolve(args.baseline) : undefined
279 const historyPath = args.history ? path.resolve(args.history) : undefined
280β
281 const current = readJsonMaybeData(currentPath)
282 const history = readOptionalJsonMaybeData(historyPath)
283 const baselineCurrent = readOptionalJsonMaybeData(baselinePath)
284β
285 const historyEntries = normalizeHistoryEntries(history, current.benchmarkName)
286 const seriesByScenario = buildSeriesByScenario(historyEntries)
287β
288 const baseline =
289 baselineCurrent != null
290 ? resolveBaselineFromCurrentJson(baselineCurrent)
291 : resolveBaselineFromHistory(historyEntries, args.baseSha)
292β
293 const metrics = current.metrics || []
294 const rows = []
295β
296 for (const metric of metrics) {
297 const baselineMetric = baseline.benchesByName.get(metric.id)
298 const hasBaseline = METRIC_KEYS.every((key) =>
299 Number.isFinite(baselineMetric?.[key]),
300 )
301β
302 if (
303 hasBaseline &&
304 METRIC_KEYS.every((key) => metric[key] === baselineMetric[key])
305 ) {
306 continue
307 }
308β
309 const historySeries = (seriesByScenario.get(metric.id) || []).slice(
310 // Reserve one slot for the current metric so the sparkline stays at trendPoints.
311 -args.trendPoints + 1,
312 )
313β
314 if (
315 !historySeries.length ||
316 historySeries[historySeries.length - 1] !== metric.gzipBytes
317 ) {
318 historySeries.push(metric.gzipBytes)
319 }
320β
321 rows.push({
322 id: metric.id,
323 currentCell: formatMetricCell(
324 metric.gzipBytes,
325 baselineMetric?.gzipBytes,
326 ),
327 initialCell: formatMetricCell(
328 metric.initialGzipBytes,
329 baselineMetric?.initialGzipBytes,
330 ),
331 rawCell: formatMetricCell(metric.rawBytes, baselineMetric?.rawBytes),
332 brotliCell: formatMetricCell(
333 metric.brotliBytes,
334 baselineMetric?.brotliBytes,
335 ),
336 hasBaseline,
337 trendCell: sparkline(historySeries.slice(-args.trendPoints)),
338 })
339 }
340β
341 const lines = []
342 lines.push(args.marker)
343 lines.push('## Bundle Size Benchmarks')
344 lines.push('')
345β
346 if (metrics.length > 0 && rows.length === 0) {
347 lines.push(
348 'This pull request does not affect bundle size in any measured scenario.',
349 )
350 } else if (metrics.length === 0) {
351 lines.push('No bundle-size scenarios were measured.')
352 } else {
353 lines.push(`- Commit: \`${formatShortSha(current.sha)}\``)
354 lines.push(
355 `- Measured at: \`${current.measuredAt || current.generatedAt || 'unknown'}\``,
356 )
357 lines.push(`- Baseline source: \`${baseline.source}\``)
358 if (args.dashboardUrl) {
359 lines.push(`- Dashboard: [bundle-size history](${args.dashboardUrl})`)
360 }
361 lines.push('')
362 lines.push(
363 rows.some((row) => !row.hasBaseline)
364 ? 'The following scenarios have bundle-size changes or lack baseline data for comparison:'
365 : 'The following scenarios have bundle-size changes compared with the baseline:',
366 )
367 lines.push('')
368 lines.push(
369 '| Scenario | Current (gzip) | Initial (gzip) | Raw | Brotli | Trend |',
370 )
371 lines.push('| --- | ---: | ---: | ---: | ---: | --- |')
372β
373 for (const row of rows) {
374 lines.push(
375 `| \`${row.id}\` | ${row.currentCell} | ${row.initialCell} | ${row.rawCell} | ${row.brotliCell} | <pre>${row.trendCell}</pre> |`,
376 )
377 }
378β
379 lines.push('')
380 lines.push(
381 '_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._',
382 )
383 }
384β
385 const markdown = lines.join('\n') + '\n'
386 await fsp.mkdir(path.dirname(outputPath), { recursive: true })
387 await fsp.writeFile(outputPath, markdown, 'utf8')
388β
389 process.stdout.write(`Wrote PR benchmark report: ${outputPath}\n`)
390}
391β
392main().catch((error) => {
393 console.error(error)
394 process.exit(1)
395})