Commit cd0496f9 authored by Tim Neutkens's avatar Tim Neutkens

Add table bench

parent fa327485
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html>
<body>{children}</body>
</html>
)
}
export const dynamic = 'force-dynamic'
import { testData } from '../testdata'
import { Table } from './table'
export default function App() {
return <Table data={testData()} />
}
'use client'
export function Table({ data }: { data: { id: string; name: string }[] }) {
return (
<table>
<tbody>
{data.map((entry) => (
<tr key={entry.id}>
<td>{entry.id}</td>
<td>{entry.name}</td>
</tr>
))}
</tbody>
</table>
)
}
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'
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 {
// Warmup phase: send requests continuously for WARMUP_DURATION_MS
console.log(`\nWarming up for ${prettyMs(WARMUP_DURATION_MS)}...`)
let warmupCount = 0
const warmupEnd = performance.now() + WARMUP_DURATION_MS
while (performance.now() < warmupEnd) {
await measureRequest(URL)
warmupCount++
}
console.log(`Warmup complete (${warmupCount} requests)`)
// Benchmark requests
console.log(`\nRunning ${BENCH_REQUESTS} benchmark requests...\n`)
const durations = []
for (let i = 0; i < BENCH_REQUESTS; i++) {
const duration = await measureRequest(URL)
durations.push(duration)
}
const stats = 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-table-1000-items-client-component",
"private": true,
"scripts": {
"build-application": "next build",
"start": "next start",
"bench": "node bench.mjs"
},
"dependencies": {
"next": "workspace:*"
},
"devDependencies": {
"@types/react": "^19.0.0",
"typescript": "^5.0.0",
"pretty-ms": "^7.0.1"
}
}
const data: { id: string; name: string }[] = []
for (let i = 0; i < 1000; i++) {
data.push({ id: `id-${i}`, name: `name-${i}` })
}
export function testData() {
return data
}
{
"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"]
}
export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return (
<html>
<body>{children}</body>
</html>
)
}
export const dynamic = 'force-dynamic'
import { testData } from '../testdata'
import { Table } from './table'
export default function App() {
return <Table data={testData()} />
}
export function Table({ data }: { data: { id: string; name: string }[] }) {
return (
<table>
<tbody>
{data.map((entry) => (
<tr key={entry.id}>
<td>{entry.id}</td>
<td>{entry.name}</td>
</tr>
))}
</tbody>
</table>
)
}
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'
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 {
// Warmup phase: send requests continuously for WARMUP_DURATION_MS
console.log(`\nWarming up for ${prettyMs(WARMUP_DURATION_MS)}...`)
let warmupCount = 0
const warmupEnd = performance.now() + WARMUP_DURATION_MS
while (performance.now() < warmupEnd) {
await measureRequest(URL)
warmupCount++
}
console.log(`Warmup complete (${warmupCount} requests)`)
// Benchmark requests
console.log(`\nRunning ${BENCH_REQUESTS} benchmark requests...\n`)
const durations = []
for (let i = 0; i < BENCH_REQUESTS; i++) {
const duration = await measureRequest(URL)
durations.push(duration)
}
const stats = 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-table-1000-items-server-component",
"private": true,
"scripts": {
"build-application": "next build",
"start": "next start",
"bench": "node bench.mjs"
},
"dependencies": {
"next": "workspace:*"
},
"devDependencies": {
"@types/react": "^19.0.0",
"typescript": "^5.0.0",
"pretty-ms": "^7.0.1"
}
}
const data: { id: string; name: string }[] = []
for (let i = 0; i < 1000; i++) {
data.push({ id: `id-${i}`, name: `name-${i}` })
}
export function testData() {
return data
}
{
"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"]
}
......@@ -912,6 +912,38 @@ importers:
specifier: 2.0.11
version: 2.0.11
bench/table-1000-items-client-component:
dependencies:
next:
specifier: workspace:*
version: link:../../packages/next
devDependencies:
'@types/react':
specifier: 19.2.10
version: 19.2.10
pretty-ms:
specifier: ^7.0.1
version: 7.0.1
typescript:
specifier: ^5.0.0
version: 5.9.2
bench/table-1000-items-server-component:
dependencies:
next:
specifier: workspace:*
version: link:../../packages/next
devDependencies:
'@types/react':
specifier: 19.2.10
version: 19.2.10
pretty-ms:
specifier: ^7.0.1
version: 7.0.1
typescript:
specifier: ^5.0.0
version: 5.9.2
bench/vercel:
dependencies:
'@szmarczak/http-timer':
......@@ -8983,9 +9015,6 @@ packages:
resolution: {integrity: sha512-AZL67abkUzIuvcHqk7c09cezpGNcxUxU4Ioi/05xHk4DQeTkWmGYftIE6ctU6AEt+Gn4n1lDStOtj7FKycP71A==}
engines: {node: '>=8'}
csstype@3.1.2:
resolution: {integrity: sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==}
csstype@3.2.3:
resolution: {integrity: sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==}
......@@ -20105,7 +20134,7 @@ snapshots:
'@emotion/memoize': 0.8.1
'@emotion/unitless': 0.8.1
'@emotion/utils': 1.2.1
csstype: 3.1.2
csstype: 3.2.3
'@emotion/sheet@1.2.2': {}
......@@ -26830,8 +26859,6 @@ snapshots:
dependencies:
cssom: 0.3.8
csstype@3.1.2: {}
csstype@3.2.3: {}
cuss@1.21.0: {}
......@@ -27347,7 +27374,7 @@ snapshots:
dom-helpers@5.2.1:
dependencies:
'@babel/runtime': 7.27.0
csstype: 3.1.2
csstype: 3.2.3
dom-serializer@0.1.1:
dependencies:
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