Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option for debugging CPU usage (#51174)
Add a `NEXT_CPU_PROF` environment variable to programmatically profile CPU for the router worker and two render workers. This is helpful because the `--cpu-prof` isn't reliably writing the result (#27406) especially with our multi-process architecture. ![CleanShot-2023-06-12-GS9p3SbN@2x](https://github.com/vercel/next.js/assets/3676859/b7cf80d2-a35a-4e90-978d-dec70be89f90)
- Loading branch information
Showing
6 changed files
with
77 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
if (process.env.__NEXT_PRIVATE_CPU_PROFILE) { | ||
const { Session } = require('inspector') as typeof import('inspector') | ||
const fs = require('fs') | ||
|
||
const session = new Session() | ||
session.connect() | ||
|
||
session.post('Profiler.enable') | ||
session.post('Profiler.start') | ||
|
||
function saveProfile() { | ||
session.post('Profiler.stop', (error, param) => { | ||
if (error) { | ||
console.error('Cannot generate CPU profiling:', error) | ||
return | ||
} | ||
|
||
// Write profile to disk | ||
const filename = `${ | ||
process.env.__NEXT_PRIVATE_CPU_PROFILE | ||
}.${Date.now()}.cpuprofile` | ||
fs.writeFileSync(`./${filename}`, JSON.stringify(param.profile)) | ||
process.exit(0) | ||
}) | ||
} | ||
process.on('SIGINT', saveProfile) | ||
process.on('SIGTERM', saveProfile) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters