rsnext/bench/vercel/bench.js
Jimmy Lai 2c23da077f
misc: fix benchmark script (#44592)
Fixing + doing some maintenance as I was using it for some things.

Changes:
- fixed a bug related to how React is injected
- added a progress tracker for the numbers of requests
- retry on 500
- added a param for crashing the lambda manually (useful for cold boots)
- added a param to skip build, useful if you want to retest the same variation
- don't crash when there's no data to compare

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
2023-01-05 13:46:41 +00:00

104 lines
2.7 KiB
JavaScript

import { Command } from 'commander'
import console from 'console'
import chalk from 'chalk'
import PQueue from 'p-queue'
import {
generateProjects,
cleanupProjectFolders,
TEST_PROJECT_NAME,
} from './project-utils.js'
import { printBenchmarkResults } from './chart.js'
import { genRetryableRequest } from './gen-request.js'
const program = new Command()
const queue = new PQueue({ concurrency: 50 })
const TTFB_OUTLIERS_THRESHOLD = 1500
let progress = 0
program.option('-p, --path <path>')
program.option('-s, --skip-build', 'Skip build step')
program.option('-f, --force-crash', 'Force function crash')
program.parse(process.argv)
const options = program.opts()
if (options.path) {
console.log('Running benchmark for path: ', options.path)
}
if (options.skipBuild) {
console.log('Skipping build step')
}
if (options.forceCrash) {
console.log('Forcing function crash')
}
export const forceCrash = options.forceCrash
try {
const [originDeploymentURL, headDeploymentURL] = options.skipBuild
? [
`https://${TEST_PROJECT_NAME}-origin.vercel.app`,
`https://${TEST_PROJECT_NAME}-head.vercel.app`,
]
: await generateProjects()
const originBenchmarkURL = `${originDeploymentURL}${options.path || ''}`
const headBenchmarkURL = `${headDeploymentURL}${options.path || ''}`
console.log(`Origin deployment URL: ${originBenchmarkURL}`)
console.log(`Head deployment URL: ${headBenchmarkURL}`)
console.log(`Running benchmark...`)
const benchResults = await runBenchmark(originBenchmarkURL)
const headBenchResults = await runBenchmark(headBenchmarkURL)
console.log(chalk.bold('Benchmark results for cold:'))
printBenchmarkResults(
{
origin: benchResults,
head: headBenchResults,
},
(r) => r.cold && r.firstByte <= TTFB_OUTLIERS_THRESHOLD && r.firstByte
)
console.log(chalk.bold('Benchmark results for hot:'))
printBenchmarkResults(
{
origin: benchResults,
head: headBenchResults,
},
(r) => !r.cold && r.firstByte <= TTFB_OUTLIERS_THRESHOLD && r.firstByte
)
} catch (err) {
console.log(chalk.red('Benchmark failed: ', err))
} finally {
await cleanupProjectFolders()
}
async function runBenchmark(url) {
progress = 0
process.stdout.write(`Sending requests to ${url} ...\n`)
process.stdout.write(`Progress: ${++progress}/500`)
return (
await Promise.all(
Array.from({ length: 500 }).map(async () => {
const p = await queue.add(() => genRetryableRequest(url))
refreshProgress()
return p
})
)
).filter(Boolean)
}
function refreshProgress() {
process.stdout.clearLine()
process.stdout.cursorTo(0)
process.stdout.write(`Requests sent: ${++progress}/500`)
}