Unverified Commit c8e811ee authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

bench: add warm cached build benchmark to nested-deps-app-router-many-pages (#100570)

### What?

Adds `bench-warm-build.mjs` (plus a `bench-warm-build` script entry) to
`bench/nested-deps-app-router-many-pages`. It benchmarks warm Turbopack
compiles of `next build` with the persistent filesystem cache, after
changing N source files.

```bash
cd bench/nested-deps-app-router-many-pages
pnpm prepare-bench
node bench-warm-build.mjs --files=1000
```

### Why?

To measure the incremental rebuild work of a cached build after many
changes on a large app, e.g. for turbo-tasks scheduler changes like
#100561. Until now there was no easy way to do that.

### How?

- **Cold compile:** an optional cold compile (deletes `.next`) fills the
cache. It is reported but not counted.
- **Warm compiles:** by default, 20 warm compiles run
(`--iterations=20`). The first 5 are warm-up (`--warmup=5`): they are
printed but excluded from the statistics.
- Each warm iteration changes N component files from `components/`,
picked deterministically with a seeded shuffle.
- The change is a rendered JSX attribute with a per-run/per-iteration
value (digits only), so it changes the emitted code (not just a comment)
and always differs from what is cached. A file that is still changed
from an earlier, killed run is changed again rather than failing.
- **Statistics:** wall time and the "Compiled successfully in" time are
printed per iteration. Over the non-warm-up builds, the script prints
count, median, mean, sample standard deviation, min and max.
- **Compile only:** builds use `--experimental-build-mode=compile`, so
prerendering the 3,000 routes doesn't add noise.
- **Cache safety:** the build cache is enabled by default, so the app
config is unchanged. The script sets `TURBO_ENGINE_IGNORE_DIRTY=1`,
because a native binary built locally from a dirty checkout otherwise
disables the cache. It also fails if the build reports a disabled cache.
- **Restoring files:** changed files are restored after success,
failure, and SIGINT/SIGTERM. On a signal, it waits for the running build
to exit before restoring.
- **Options:** strict validation of `--files`, `--iterations`,
`--warmup` (must be smaller than `--iterations`), `--cold` and `--seed`.

### Results

Example output of this script with the default options (20 warm builds,
the first 5 excluded) for the native binary of this PR's base commit
(canary). Times are in seconds. "Compile" is the "Compiled successfully
in" time and "wall" is the whole `next build` process.

| | cold compile | cold wall | warm compile median (mean ± std dev,
min–max) | warm wall median (mean ± std dev, min–max) |
|---|---|---|---|---|
| `--files=0` | 23.6 | 33.4 | 7.5 (7.44 ± 0.17, 7.1–7.7) | 12.9 (12.86 ±
0.60, 11.9–13.9) |
| `--files=1000` | 24.9 | 37.8 | 8.6 (8.54 ± 0.17, 8.2–8.7) | 14.6
(14.51 ± 0.63, 13.2–15.5) |

- **Warm builds without changes:** a warm build takes about a third of
the cold compile time, even with no files changed (7.5 s vs 23.6 s).
- **1,000 changed components** add about 1.1 s of compile time on top of
that.
- **First warm build:** it is noticeably slower than the rest (14.2 s
compile with 1,000 files, 10.9 s with 0), so it is a warm-up build.
- **Setup:**
- Machine: 32 logical CPUs (Intel Xeon @ 2.90 GHz), 64 GiB RAM, Linux,
Node.js 24.
- App: the generated app with 3,000 pages and 3,020 components (`pnpm
prepare-bench`).
  - Native binary: a locally built release build.
- **Caveats:** one machine and one generated app; the numbers vary
between runs and machines, so compare binaries on the same machine.

### Verification

- Ran end to end with locally built release native bindings on a 32-core
machine:
  - cold + warm compiles, including the default 20/5 runs;
  - the changed marker appears in the emitted chunks;
- the generated components are byte-identical afterwards, also after a
SIGINT during a build, with no leftover build process.
- The cache-disabled guard fails as expected when the ignore-dirty
override is removed and the binary was built from a dirty tree.
- Invalid options are rejected before any build: unknown, `__proto__`,
unsafe or non-integer values, `--iterations=0`, bad booleans, and
`--warmup` that is negative or not smaller than `--iterations`.
- The change regex matches both an unchanged `<div>` and one that was
already changed.
- prettier and eslint pass.

<!-- NEXT_JS_LLM -->


<!-- fleet 519ddd31-d876-47cd-9933-de3e7be9444a -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent f27683f2
// Benchmarks warm Turbopack compiles of `next build` (with the persistent
// filesystem cache, which is enabled by default) after changing N source files,
// e.g. to measure the incremental rebuild work after many changes.
//
// Run `pnpm prepare-bench` in this directory first to generate the app. Then:
//
// node bench-warm-build.mjs [--files=100] [--iterations=20] [--warmup=5]
// [--cold=true] [--seed=1]
//
// - `--files`: the number of component files changed before every warm build.
// They are picked deterministically (by `--seed`) from `components/`, so
// different runs change the same files. `0` measures a rebuild without
// changes.
// - `--iterations`: the number of warm builds. Every iteration changes the
// files differently, so every build has to recompute them.
// - `--warmup`: how many of the first warm builds are only warm-up. They are
// printed, but excluded from the statistics, which are computed over the
// remaining `iterations - warmup` builds.
// - `--cold`: whether to delete `.next` and do a (measured, but not counted)
// cold build first that fills the cache. Use `--cold=false` to reuse the
// cache of a previous run.
// - `--seed`: the seed for picking the changed files.
//
// Builds run with `--experimental-build-mode=compile`, which skips
// prerendering the 3000 routes, as that doesn't depend on the bundler's work.
// The reported compile time is the one `next build` prints. The first warm
// builds after a cold one tend to be slower than the following ones, hence the
// warm-up builds.
//
// The changed files are restored afterwards, also when the run fails or is
// interrupted.
import { spawn } from 'node:child_process'
import { existsSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'
import { rm } from 'node:fs/promises'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
const CWD = fileURLToPath(new URL('.', import.meta.url))
const ROOT_DIR = join(CWD, '..', '..')
const NEXT_BIN = join(ROOT_DIR, 'packages', 'next', 'dist', 'bin', 'next')
const COMPONENTS_DIR = join(CWD, 'components')
function parseInteger(name, value, min = Number.MIN_SAFE_INTEGER) {
const number = /^-?\d+$/.test(value) ? Number(value) : NaN
if (!Number.isSafeInteger(number) || number < min) {
throw new Error(
min === Number.MIN_SAFE_INTEGER
? `--${name} must be an integer`
: `--${name} must be an integer >= ${min}`
)
}
return number
}
function parseArgs(argv) {
const options = {
files: 100,
iterations: 20,
warmup: 5,
cold: true,
seed: 1,
}
for (const arg of argv) {
const match = /^--([^=]+)=(.*)$/.exec(arg)
if (!match || !Object.hasOwn(options, match[1])) {
throw new Error(`Unknown argument: ${arg}`)
}
const [, name, value] = match
switch (name) {
case 'files':
options.files = parseInteger(name, value, 0)
break
case 'iterations':
options.iterations = parseInteger(name, value, 1)
break
case 'warmup':
options.warmup = parseInteger(name, value, 0)
break
case 'seed':
options.seed = parseInteger(name, value)
break
case 'cold':
if (value !== 'true' && value !== 'false') {
throw new Error('--cold must be true or false')
}
options.cold = value === 'true'
break
default:
throw new Error(`Unknown argument: ${arg}`)
}
}
if (options.warmup >= options.iterations) {
throw new Error(
`--warmup=${options.warmup} must be smaller than --iterations=${options.iterations}`
)
}
return options
}
// A small seeded PRNG (mulberry32), so the picked files are reproducible.
function createRandom(seed) {
let state = seed >>> 0
return () => {
state = (state + 0x6d2b79f5) >>> 0
let t = state
t = Math.imul(t ^ (t >>> 15), t | 1)
t ^= t + Math.imul(t ^ (t >>> 7), t | 61)
return ((t ^ (t >>> 14)) >>> 0) / 4294967296
}
}
function pickFiles(count, seed) {
const files = readdirSync(COMPONENTS_DIR)
.filter((file) => file.endsWith('.jsx') && file !== 'index.jsx')
.sort()
if (count > files.length) {
throw new Error(
`--files=${count} is larger than the ${files.length} available components`
)
}
// Fisher-Yates shuffle, then take the first `count`, so the files are spread
// over the whole component tree.
const random = createRandom(seed)
for (let i = files.length - 1; i > 0; i--) {
const j = Math.floor(random() * (i + 1))
;[files[i], files[j]] = [files[j], files[i]]
}
return files.slice(0, count).map((file) => join(COMPONENTS_DIR, file))
}
// Changes the rendered output of every file, not just a comment, so the change
// propagates beyond parsing. The value (digits only) differs per run and
// iteration, so every build differs from what is in the cache, also with
// `--cold=false`.
function changeFiles(originals, value) {
for (const [file, original] of originals) {
// Also matches a file that is still changed, e.g. after the script was
// killed without restoring it.
const changed = original.replace(
/<div(?: data-warm-build="\d*")?>/,
`<div data-warm-build="${value}">`
)
if (changed === original) {
throw new Error(`Found no <div> to change in ${file}`)
}
writeFileSync(file, changed)
}
}
function restoreFiles(originals) {
for (const [file, original] of originals) {
writeFileSync(file, original)
}
}
/** The running `next build` process, if any. */
let currentBuild = null
function build() {
const args = [NEXT_BIN, 'build', '--experimental-build-mode=compile']
return new Promise((resolve, reject) => {
const start = process.hrtime.bigint()
const child = spawn(process.execPath, args, {
cwd: CWD,
env: {
...process.env,
NODE_ENV: 'production',
NEXT_TELEMETRY_DISABLED: '1',
// A native binary built locally from a dirty checkout disables the
// filesystem cache, which would turn every warm build into a cold one.
TURBO_ENGINE_IGNORE_DIRTY: '1',
},
stdio: ['ignore', 'pipe', 'pipe'],
})
currentBuild = child
let output = ''
child.stdout.on('data', (data) => (output += data))
child.stderr.on('data', (data) => (output += data))
child.on('error', reject)
child.on('close', (code, signal) => {
currentBuild = null
const wallMs = Number(process.hrtime.bigint() - start) / 1e6
if (code !== 0) {
reject(
new Error(
`next build failed (${signal ?? `exit code ${code}`}):\n${output}`
)
)
return
}
if (/File System Cache is disabled/.test(output)) {
reject(
new Error(
`The filesystem cache is disabled, see the output:\n${output}`
)
)
return
}
// e.g. "✓ Compiled successfully in 12.3s" or "in 850ms"
const match = /Compiled successfully in (\d+(?:\.\d+)?)(ms|s|min)/.exec(
output
)
const compileMs = match
? Number(match[1]) * { ms: 1, s: 1000, min: 60_000 }[match[2]]
: undefined
resolve({ wallMs, compileMs })
})
})
}
function formatMs(ms) {
return ms === undefined ? '-' : `${(ms / 1000).toFixed(2)}s`
}
function median(values) {
const sorted = [...values].sort((a, b) => a - b)
const mid = Math.floor(sorted.length / 2)
return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2
}
function summarize(label, values) {
if (values.length === 0) {
return `${label}: -`
}
const mean = values.reduce((sum, value) => sum + value, 0) / values.length
// The sample standard deviation, which needs at least two samples.
const stdDev =
values.length > 1
? Math.sqrt(
values.reduce((sum, value) => sum + (value - mean) ** 2, 0) /
(values.length - 1)
)
: undefined
return `${label} (${values.length} builds): median ${formatMs(
median(values)
)}, mean ${formatMs(mean)}, std dev ${formatMs(stdDev)}, min ${formatMs(
Math.min(...values)
)}, max ${formatMs(Math.max(...values))}`
}
async function main() {
const options = parseArgs(process.argv.slice(2))
if (!existsSync(join(COMPONENTS_DIR, 'index.jsx'))) {
throw new Error('The app is not generated yet, run `pnpm prepare-bench`')
}
if (!existsSync(NEXT_BIN)) {
throw new Error(`${NEXT_BIN} doesn't exist, build next first`)
}
const originals = new Map(
pickFiles(options.files, options.seed).map((file) => [
file,
readFileSync(file, 'utf8'),
])
)
const restore = () => restoreFiles(originals)
let interrupted = false
const onSignal = (signal) => {
if (interrupted) {
return
}
interrupted = true
const exit = () => {
restore()
process.exit(128 + (signal === 'SIGINT' ? 2 : 15))
}
// Wait for the build to exit, so it doesn't keep reading the changed files
// or writing to `.next` after the files were restored.
const child = currentBuild
if (child && child.exitCode === null && child.signalCode === null) {
child.once('exit', exit)
child.kill(signal)
} else {
exit()
}
}
process.on('SIGINT', onSignal)
process.on('SIGTERM', onSignal)
console.log(
`Warm builds after changing ${options.files} files, ${options.iterations} iterations (${options.warmup} warm-up)`
)
try {
if (options.cold) {
await rm(join(CWD, '.next'), { recursive: true, force: true })
const cold = await build()
console.log(
`cold: wall ${formatMs(cold.wallMs)}, compile ${formatMs(cold.compileMs)}`
)
}
const runId = Date.now()
const wall = []
const compile = []
for (let iteration = 1; iteration <= options.iterations; iteration++) {
changeFiles(originals, `${runId}${String(iteration).padStart(4, '0')}`)
const result = await build()
const isWarmup = iteration <= options.warmup
if (!isWarmup) {
wall.push(result.wallMs)
if (result.compileMs !== undefined) {
compile.push(result.compileMs)
}
}
console.log(
`warm #${iteration}${isWarmup ? ' (warm-up, excluded)' : ''}: wall ${formatMs(result.wallMs)}, compile ${formatMs(result.compileMs)}`
)
}
console.log(summarize('warm wall', wall))
console.log(summarize('warm compile', compile))
} finally {
restore()
}
}
await main()
......@@ -7,7 +7,8 @@
"start": "cross-env NEXT_PRIVATE_LOCAL_WEBPACK=1 next start",
"dev-nocache": "rimraf .next && pnpm dev-application",
"dev-cpuprofile-nocache": "rimraf .next && cross-env NEXT_PRIVATE_LOCAL_WEBPACK=1 node --cpu-prof ../../node_modules/next/dist/bin/next",
"build-nocache": "rimraf .next && pnpm build-application"
"build-nocache": "rimraf .next && pnpm build-application",
"bench-warm-build": "node ./bench-warm-build.mjs"
},
"devDependencies": {
"fuzzponent": "workspace:*",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment