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