Commit 20fba0f3 authored by Tim Neutkens's avatar Tim Neutkens

Add many nested suspense benchmark

parent 88d412a7
import { Suspense } from 'react'
import {
fetchComponentDetails,
fetchComponentMeta,
fetchComponentPayload,
type ComponentSeed,
} from '../testdata'
async function AsyncPayload({ id }: { id: ComponentSeed['id'] }) {
const payload = await fetchComponentPayload(id)
return (
<ul>
{payload.tags.map((tag) => (
<li key={tag}>{tag}</li>
))}
</ul>
)
}
async function AsyncDetails({ id }: { id: ComponentSeed['id'] }) {
const details = await fetchComponentDetails(id)
return (
<p>
{details.detail} | {details.checksum}
</p>
)
}
function PayloadFallback() {
return (
<ul>
<li>loading-tag-0</li>
<li>loading-tag-1</li>
</ul>
)
}
function DetailsFallback() {
return <p>loading-details</p>
}
async function AsyncCardBody({ id }: { id: ComponentSeed['id'] }) {
const meta = await fetchComponentMeta(id)
return (
<article data-id={id}>
<h2>{meta.name}</h2>
<p>{meta.group}</p>
<Suspense fallback={<DetailsFallback />}>
<AsyncDetails id={id} />
</Suspense>
<Suspense fallback={<PayloadFallback />}>
<AsyncPayload id={id} />
</Suspense>
</article>
)
}
export function AsyncComponentCard({ id }: { id: ComponentSeed['id'] }) {
return (
<Suspense fallback={<DetailsFallback />}>
<AsyncCardBody id={id} />
</Suspense>
)
}
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { Suspense } from 'react'
import { type ComponentSeed, testData } from '../testdata'
import { AsyncComponentCard } from './component-card'
export const dynamic = 'force-dynamic'
const CHUNK_SIZE = 50
function CardFallback({ item }: { item: ComponentSeed }) {
return (
<article data-id={item.id}>
<h2>loading-{item.id}</h2>
<p>{item.group}</p>
<ul>
<li>loading</li>
<li>loading</li>
</ul>
</article>
)
}
function ChunkFallback({ chunk }: { chunk: ComponentSeed[] }) {
return (
<>
{chunk.map((item) => (
<CardFallback key={item.id} item={item} />
))}
</>
)
}
function chunkItems(items: ComponentSeed[]) {
const chunks: ComponentSeed[][] = []
for (let i = 0; i < items.length; i += CHUNK_SIZE) {
chunks.push(items.slice(i, i + CHUNK_SIZE))
}
return chunks
}
function CardChunk({ chunk }: { chunk: ComponentSeed[] }) {
return (
<>
{chunk.map((item) => (
<Suspense key={item.id} fallback={<CardFallback item={item} />}>
<AsyncComponentCard id={item.id} />
</Suspense>
))}
</>
)
}
export default function App() {
const items = testData()
const chunks = chunkItems(items)
return (
<main>
<section>
{chunks.map((chunk) => (
<Suspense
key={chunk[0].id}
fallback={<ChunkFallback chunk={chunk} />}
>
<CardChunk chunk={chunk} />
</Suspense>
))}
</section>
</main>
)
}
import { execSync, spawn } from 'child_process'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import { promises as fs } from 'fs'
import prettyMs from 'pretty-ms'
import treeKill from 'tree-kill'
import { Bench } from 'tinybench'
const __dirname = dirname(fileURLToPath(import.meta.url))
const ROOT_DIR = join(__dirname, '..', '..')
const CWD = __dirname
const NEXT_BIN = join(ROOT_DIR, 'packages', 'next', 'dist', 'bin', 'next')
const PORT = 3199
const URL = `http://localhost:${PORT}/`
const WARMUP_DURATION_MS = 10_000
const BENCH_REQUESTS = 200
function killApp(instance) {
return new Promise((resolve, reject) => {
treeKill(instance.pid, (err) => {
if (err) {
if (
process.platform === 'win32' &&
typeof err.message === 'string' &&
(err.message.includes('no running instance of the task') ||
err.message.includes('not found'))
) {
return resolve()
}
return reject(err)
}
resolve()
})
})
}
function startServer() {
return new Promise((resolve, reject) => {
const instance = spawn(
'node',
[NEXT_BIN, 'start', '--port', String(PORT)],
{
cwd: CWD,
env: {
...process.env,
NODE_ENV: 'production',
},
}
)
let didResolve = false
function handleStdout(data) {
const message = data.toString()
process.stdout.write(message)
if (/Ready/i.test(message) || /started server/i.test(message)) {
if (!didResolve) {
didResolve = true
resolve(instance)
}
}
}
function handleStderr(data) {
process.stderr.write(data.toString())
}
instance.stdout.on('data', handleStdout)
instance.stderr.on('data', handleStderr)
instance.on('close', () => {
if (!didResolve) {
didResolve = true
reject(new Error('Server closed before it was ready'))
}
})
instance.on('error', reject)
})
}
async function measureRequest(url) {
const start = performance.now()
const res = await fetch(url)
if (!res.ok) {
throw new Error(`Request failed with status ${res.status}`)
}
// Consume the body to ensure the full response is received
await res.text()
const duration = performance.now() - start
return duration
}
function percentile(sorted, p) {
const idx = Math.ceil((p / 100) * sorted.length) - 1
return sorted[Math.max(0, idx)]
}
function statistics(durations) {
const sorted = [...durations].sort((a, b) => a - b)
const sum = sorted.reduce((a, b) => a + b, 0)
return {
min: sorted[0],
max: sorted[sorted.length - 1],
avg: sum / sorted.length,
p50: percentile(sorted, 50),
p75: percentile(sorted, 75),
p95: percentile(sorted, 95),
p99: percentile(sorted, 99),
}
}
async function main() {
// Build the application
console.log('Building application...')
execSync(`node ${NEXT_BIN} build`, {
cwd: CWD,
stdio: 'inherit',
env: {
...process.env,
NODE_ENV: 'production',
},
})
console.log('\nStarting server...')
const instance = await startServer()
try {
// Benchmark requests using tinybench built-in warmup.
console.log(`\nPreparing tinybench benchmark...`)
const bench = new Bench({
name: 'rsc-many-suspense-components',
time: 0,
iterations: BENCH_REQUESTS,
warmup: true,
warmupTime: WARMUP_DURATION_MS,
retainSamples: true,
throws: true,
})
bench.add(
'request',
async () => {
await measureRequest(URL)
},
{
async: true,
}
)
console.log(
`\nRunning ${BENCH_REQUESTS} benchmark requests with tinybench (${prettyMs(WARMUP_DURATION_MS)} built-in warmup)...\n`
)
await bench.run()
const latency = bench.tasks[0]?.result?.latency
const durations = Array.isArray(latency?.samples)
? [...latency.samples]
: []
const stats =
latency &&
typeof latency.min === 'number' &&
typeof latency.max === 'number' &&
typeof latency.mean === 'number' &&
typeof latency.p50 === 'number' &&
typeof latency.p75 === 'number' &&
typeof latency.p95 === 'number' &&
typeof latency.p99 === 'number'
? {
min: latency.min,
max: latency.max,
avg: latency.mean,
p50: latency.p50,
p75: latency.p75,
p95: latency.p95,
p99: latency.p99,
}
: statistics(durations)
console.log('Results:')
console.log('─'.repeat(40))
console.log(` Requests: ${BENCH_REQUESTS}`)
console.log(` Min: ${prettyMs(stats.min)}`)
console.log(` Max: ${prettyMs(stats.max)}`)
console.log(` Avg: ${prettyMs(stats.avg)}`)
console.log(` P50: ${prettyMs(stats.p50)}`)
console.log(` P75: ${prettyMs(stats.p75)}`)
console.log(` P95: ${prettyMs(stats.p95)}`)
console.log(` P99: ${prettyMs(stats.p99)}`)
console.log('─'.repeat(40))
// Output raw durations as JSON for further analysis
const output = {
timestamp: new Date().toISOString(),
config: {
warmupDurationMs: WARMUP_DURATION_MS,
benchRequests: BENCH_REQUESTS,
url: URL,
},
statistics: {
min: stats.min,
max: stats.max,
avg: stats.avg,
p50: stats.p50,
p75: stats.p75,
p95: stats.p95,
p99: stats.p99,
},
durations,
}
await fs.writeFile(
join(CWD, 'bench-result.json'),
JSON.stringify(output, null, 2)
)
console.log('\nDetailed results written to bench-result.json')
} finally {
await killApp(instance)
}
}
main().catch((err) => {
console.error(err)
process.exit(1)
})
/// <reference types="next" />
/// <reference types="next/image-types/global" />
import "./.next/types/routes.d.ts";
// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
module.exports = {
typescript: {
ignoreBuildErrors: true,
},
}
{
"name": "bench-rsc-many-suspense-components",
"private": true,
"scripts": {
"build-application": "next build",
"start": "next start",
"bench": "node bench.mjs"
},
"dependencies": {
"next": "workspace:*"
},
"devDependencies": {
"@types/react": "^19.0.0",
"tinybench": "^6.0.0",
"typescript": "^5.0.0",
"pretty-ms": "^7.0.1"
}
}
export type ComponentSeed = {
id: string
name: string
group: string
weight: number
}
const data: ComponentSeed[] = []
const byId = new Map<string, ComponentSeed>()
for (let i = 0; i < 1000; i++) {
const entry: ComponentSeed = {
id: `id-${i}`,
name: `component-${i}`,
group: `group-${i % 40}`,
weight: i % 13,
}
data.push(entry)
byId.set(entry.id, entry)
}
function sleep(ms: number) {
return new Promise<void>((resolve) => setTimeout(resolve, ms))
}
function getSeed(id: string) {
const seed = byId.get(id)
if (!seed) {
throw new Error(`Unknown id: ${id}`)
}
return seed
}
export function testData() {
return data
}
export async function fetchComponentMeta(id: string) {
const seed = getSeed(id)
await sleep((seed.weight % 3) + 1)
return {
id: seed.id,
name: seed.name,
group: seed.group,
}
}
export async function fetchComponentPayload(id: string) {
const seed = getSeed(id)
await sleep((seed.weight % 5) + 1)
return {
score: seed.weight * 10 + id.length,
tags: [`tag-${seed.weight}`, `bucket-${seed.weight % 4}`],
}
}
export async function fetchComponentDetails(id: string) {
const seed = getSeed(id)
await sleep((seed.weight % 4) + 1)
return {
detail: `detail-${seed.id}-${seed.weight}`,
checksum: `${seed.group}-${seed.weight * 7}`,
}
}
{
"compilerOptions": {
"target": "es2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react-jsx",
"plugins": [
{
"name": "next"
}
]
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
".next/dev/types/**/*.ts"
],
"exclude": ["node_modules"]
}
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