Repository navigation
Expand file tree
/
Copy pathbenchmark.js
More file actions
152 lines (136 loc) · 5.03 KB
/
Copy pathbenchmark.js
File metadata and controls
152 lines (136 loc) · 5.03 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
// Cold-build benchmark of every fixture in benchmark-manifest.json.
//
// Sampling policy: every target gets the same number of cold samples
// (`samples` in the manifest). Samples run in rounds, and each round starts the
// list at a different place, so no target always runs first (when the runner is
// coolest) or last. A row is reported as the median of its samples with its
// spread (min and max); the median is what gets compared and published.
//
// The machine-readable run goes to benchmark-run.json: `benchmark-check.js`
// compares it with the checked baseline, and the README tables and the landing
// page are written from the checked results (benchmark-results.json), which CI
// refreshes from this file after every merge to main (benchmark-refresh.js).
//
// Every size is measured on every sample; a row reports the median and keeps
// the samples with their range (`sizes`). `bytes` counts only the deployable
// build output (benchmark-output.js), never caches or type-check artifacts.
import { existsSync, readFileSync, rmSync, writeFileSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { execSync } from 'child_process'
import { roundOrder } from './benchmark-gate.js'
import { BYTES_DEFINITION, measureOutput } from './benchmark-output.js'
import { summarizeRow } from './benchmark-record.js'
const manifest = JSON.parse(readFileSync('benchmark-manifest.json', 'utf8'))
function clearBuildFile(dir) {
const base = join('./benchmark', dir)
for (const output of ['.next', 'dist', 'df', 'tsconfig.tsbuildinfo']) {
const target = join(base, output)
if (existsSync(target)) rmSync(target, { recursive: true, force: true })
}
}
let benchmarkRun = 0
/** Short names ('tailwind' -> next-tailwind) and full names ('vinext-devup-ui') */
function fixtureDir(target) {
return existsSync(join('./benchmark', target, 'package.json'))
? target
: 'next-' + target
}
/** The installed version of a package as a fixture resolves it, or null */
function installedVersion(dir, name) {
let directory = join(process.cwd(), 'benchmark', dir)
for (;;) {
const file = join(directory, 'node_modules', name, 'package.json')
if (existsSync(file)) return JSON.parse(readFileSync(file, 'utf8')).version
if (dirname(directory) === directory) return null
directory = dirname(directory)
}
}
/** `ubuntu-26.04` from /etc/os-release, or null off Linux */
function runnerName() {
if (!existsSync('/etc/os-release')) return null
const release = Object.fromEntries(
readFileSync('/etc/os-release', 'utf8')
.split('\n')
.map((line) => line.match(/^(\w+)="?([^"]*)"?$/)?.slice(1))
.filter(Boolean),
)
return release.ID && release.VERSION_ID
? `${release.ID}-${release.VERSION_ID}`
: null
}
function benchmark(target) {
const dir = fixtureDir(target)
const run = `${target}-${benchmarkRun++}`
clearBuildFile(dir)
performance.mark(run + '-start')
console.profile(run)
execSync('bun run --filter ' + dir + '-benchmark build', {
stdio: 'inherit',
})
console.profileEnd(run)
performance.mark(run + '-end')
performance.measure(run, run + '-start', run + '-end')
const benchmarkDir = join('./benchmark', dir)
return {
seconds: performance.getEntriesByName(run)[0].duration / 1000,
...measureOutput(benchmarkDir),
}
}
const names = manifest.targets.map((target) => target.name)
const samples = new Map(
names.map((name) => [
name,
{ seconds: [], bytes: [], cssBytes: [], clientBytes: [] },
]),
)
for (let round = 0; round < manifest.samples; round++) {
for (const name of roundOrder(names, round, manifest.samples)) {
const sample = benchmark(name)
const row = samples.get(name)
row.seconds.push(sample.seconds)
row.bytes.push(sample.bytes)
row.cssBytes.push(sample.cssBytes)
row.clientBytes.push(sample.clientBytes)
}
}
const rows = Object.fromEntries(
names.map((name) => [
name,
{
...summarizeRow(samples.get(name)),
// The library version the fixture built with, for the README tables
version: manifest.packages[name]
? installedVersion(fixtureDir(name), manifest.packages[name])
: null,
},
]),
)
console.info(
names
.map((name) => {
const row = rows[name]
return `${name} median ${row.median.toFixed(2)}s (${row.samples.length} cold samples: ${row.samples.map((sample) => sample.toFixed(2) + 's').join(', ')}) ${row.bytes.toLocaleString()} bytes (css ${row.cssBytes.toLocaleString()} bytes, client ${row.clientBytes.toLocaleString()} bytes)`
})
.join('\n'),
)
writeFileSync(
'benchmark-run.json',
JSON.stringify(
{
run: {
id: process.env.GITHUB_RUN_ID ?? null,
commit: process.env.GITHUB_SHA ?? null,
date: new Date().toISOString(),
runner: runnerName(),
next: installedVersion(fixtureDir('tailwind'), 'next'),
devupUi: JSON.parse(readFileSync('packages/react/package.json', 'utf8'))
.version,
bytesDefinition: BYTES_DEFINITION,
},
samples: manifest.samples,
rows,
},
null,
2,
),
)