Commit 88d412a7 authored by Tim Neutkens's avatar Tim Neutkens

Use tinybench

parent cd0496f9
...@@ -4,6 +4,7 @@ import { fileURLToPath } from 'url' ...@@ -4,6 +4,7 @@ import { fileURLToPath } from 'url'
import { promises as fs } from 'fs' import { promises as fs } from 'fs'
import prettyMs from 'pretty-ms' import prettyMs from 'pretty-ms'
import treeKill from 'tree-kill' import treeKill from 'tree-kill'
import { Bench } from 'tinybench'
const __dirname = dirname(fileURLToPath(import.meta.url)) const __dirname = dirname(fileURLToPath(import.meta.url))
const ROOT_DIR = join(__dirname, '..', '..') const ROOT_DIR = join(__dirname, '..', '..')
...@@ -126,26 +127,56 @@ async function main() { ...@@ -126,26 +127,56 @@ async function main() {
const instance = await startServer() const instance = await startServer()
try { try {
// Warmup phase: send requests continuously for WARMUP_DURATION_MS // Benchmark requests using tinybench built-in warmup.
console.log(`\nWarming up for ${prettyMs(WARMUP_DURATION_MS)}...`) console.log(`\nPreparing tinybench benchmark...`)
let warmupCount = 0 const bench = new Bench({
const warmupEnd = performance.now() + WARMUP_DURATION_MS name: 'table-1000-items-client-component',
while (performance.now() < warmupEnd) { time: 0,
iterations: BENCH_REQUESTS,
warmup: true,
warmupTime: WARMUP_DURATION_MS,
retainSamples: true,
throws: true,
})
bench.add(
'request',
async () => {
await measureRequest(URL) await measureRequest(URL)
warmupCount++ },
{
async: true,
} }
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++) { console.log(
const duration = await measureRequest(URL) `\nRunning ${BENCH_REQUESTS} benchmark requests with tinybench (${prettyMs(WARMUP_DURATION_MS)} built-in warmup)...\n`
durations.push(duration) )
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)
const stats = statistics(durations)
console.log('Results:') console.log('Results:')
console.log('─'.repeat(40)) console.log('─'.repeat(40))
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/react": "^19.0.0", "@types/react": "^19.0.0",
"tinybench": "^6.0.0",
"typescript": "^5.0.0", "typescript": "^5.0.0",
"pretty-ms": "^7.0.1" "pretty-ms": "^7.0.1"
} }
......
...@@ -4,6 +4,7 @@ import { fileURLToPath } from 'url' ...@@ -4,6 +4,7 @@ import { fileURLToPath } from 'url'
import { promises as fs } from 'fs' import { promises as fs } from 'fs'
import prettyMs from 'pretty-ms' import prettyMs from 'pretty-ms'
import treeKill from 'tree-kill' import treeKill from 'tree-kill'
import { Bench } from 'tinybench'
const __dirname = dirname(fileURLToPath(import.meta.url)) const __dirname = dirname(fileURLToPath(import.meta.url))
const ROOT_DIR = join(__dirname, '..', '..') const ROOT_DIR = join(__dirname, '..', '..')
...@@ -126,26 +127,56 @@ async function main() { ...@@ -126,26 +127,56 @@ async function main() {
const instance = await startServer() const instance = await startServer()
try { try {
// Warmup phase: send requests continuously for WARMUP_DURATION_MS // Benchmark requests using tinybench built-in warmup.
console.log(`\nWarming up for ${prettyMs(WARMUP_DURATION_MS)}...`) console.log(`\nPreparing tinybench benchmark...`)
let warmupCount = 0 const bench = new Bench({
const warmupEnd = performance.now() + WARMUP_DURATION_MS name: 'table-1000-items-server-component',
while (performance.now() < warmupEnd) { time: 0,
iterations: BENCH_REQUESTS,
warmup: true,
warmupTime: WARMUP_DURATION_MS,
retainSamples: true,
throws: true,
})
bench.add(
'request',
async () => {
await measureRequest(URL) await measureRequest(URL)
warmupCount++ },
{
async: true,
} }
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++) { console.log(
const duration = await measureRequest(URL) `\nRunning ${BENCH_REQUESTS} benchmark requests with tinybench (${prettyMs(WARMUP_DURATION_MS)} built-in warmup)...\n`
durations.push(duration) )
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)
const stats = statistics(durations)
console.log('Results:') console.log('Results:')
console.log('─'.repeat(40)) console.log('─'.repeat(40))
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/react": "^19.0.0", "@types/react": "^19.0.0",
"tinybench": "^6.0.0",
"typescript": "^5.0.0", "typescript": "^5.0.0",
"pretty-ms": "^7.0.1" "pretty-ms": "^7.0.1"
} }
......
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