Skip to content

Commit

Permalink
misc: add minimal server bench setup (#41328)
Browse files Browse the repository at this point in the history
This PR adds a script to run a minimal Next server in order to reproduce more easily the cold boot conditions in production.

You can use it by using `pnpm start` after having built your app with `pnpm next-react-exp build bench/minimal-server/benchmar-app` from the root of this repo.

<img width="338" alt="image" src="https://user-images.githubusercontent.com/11064311/195102966-6997b957-1e6b-433f-b611-e0eaec9462fa.png">

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `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`
- [ ] Integration 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`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
  • Loading branch information
feedthejim committed Oct 11, 2022
1 parent ba08576 commit c6a8675
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 0 deletions.
10 changes: 10 additions & 0 deletions bench/minimal-server/benchmark-app/app/layout.js
@@ -0,0 +1,10 @@
import * as React from 'react'

export default function Root({ children }) {
return (
<html>
<head></head>
<body>{children}</body>
</html>
)
}
5 changes: 5 additions & 0 deletions bench/minimal-server/benchmark-app/app/rsc/page.js
@@ -0,0 +1,5 @@
import * as React from 'react'

export default function page() {
return <div>hello</div>
}
5 changes: 5 additions & 0 deletions bench/minimal-server/benchmark-app/next.config.js
@@ -0,0 +1,5 @@
module.exports = {
experimental: {
appDir: true,
},
}
14 changes: 14 additions & 0 deletions bench/minimal-server/benchmark-app/package.json
@@ -0,0 +1,14 @@
{
"name": "stats-app",
"private": true,
"license": "MIT",
"dependencies": {
"webpack-bundle-analyzer": "^4.6.1",
"webpack-stats-plugin": "^1.1.0"
},
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
}
9 changes: 9 additions & 0 deletions bench/minimal-server/benchmark-app/pages/index.js
@@ -0,0 +1,9 @@
import * as React from 'react'

export default function page() {
return <div> hello world </div>
}

export async function getServerSideProps() {
return {}
}
7 changes: 7 additions & 0 deletions bench/minimal-server/package.json
@@ -0,0 +1,7 @@
{
"name": "next-minimal-server",
"description": "Minimal server for Next.js for benchmarking/perf analysis purposes.",
"scripts": {
"start": "node start.js"
}
}
40 changes: 40 additions & 0 deletions bench/minimal-server/start.js
@@ -0,0 +1,40 @@
process.env.__NEXT_REACT_CHANNEL = 'exp'
process.env.NODE_ENV = 'production'

require('../../test/lib/react-channel-require-hook')

console.time('next-cold-start')
const NextServer = require('next/dist/server/next-server').default
const path = require('path')

const appDir = path.join(__dirname, 'benchmark-app')
const distDir = '.next'

const compiledConfig = require(path.join(
appDir,
distDir,
'required-server-files.json'
)).config

process.chdir(appDir)

const nextServer = new NextServer({
conf: compiledConfig,
dir: appDir,
distDir,
minimalMode: true,
customServer: false,
})

const requestHandler = nextServer.getRequestHandler()

require('http')
.createServer((req, res) => {
console.time('next-request')
return requestHandler(req, res).finally(() => {
console.timeEnd('next-request')
})
})
.listen(3000, () => {
console.timeEnd('next-cold-start')
})

0 comments on commit c6a8675

Please sign in to comment.