-
Notifications
You must be signed in to change notification settings - Fork 107
feat(build,dev): add --profile support
#1243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+255
−52
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
914cac7
feat(build,dev): add `--profile` support
danielroe 2680bb6
fix: don't set _startTime
danielroe 338aebf
fix: use `undefined` as default `profile` value
danielroe 39aad90
feat: move profiler entirely to cli
danielroe 72559f9
fix: merge debug overrides
danielroe cd04391
chore: lint
danielroe 74916dd
fix: stop profiler synchronously
danielroe 26b08e5
fix: handle session cleanup
danielroe b6dc053
fix: improve types for `__nuxt_cli__`
danielroe 4acc72c
fix: stop cpu profile before calling process.exit
danielroe 0317fb9
fix: improve logging + await profiler when exiting intentionally
danielroe 63c2ddb
feat: use box
danielroe 61d3b93
fix: disable forking when profiling
danielroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,91 @@ | ||
| import type { Session } from 'node:inspector' | ||
| import { mkdirSync, writeFileSync } from 'node:fs' | ||
| import process from 'node:process' | ||
| import { box } from '@clack/prompts' | ||
| import { colors } from 'consola/utils' | ||
| import { join, relative } from 'pathe' | ||
| import { themeColor } from './ascii' | ||
|
|
||
| let session: Session | undefined | ||
| let profileCount = 0 | ||
|
|
||
| export async function startCpuProfile(): Promise<void> { | ||
| const cli = globalThis.__nuxt_cli__ | ||
| if (cli?.cpuProfileSession) { | ||
| session = cli.cpuProfileSession | ||
| delete cli.cpuProfileSession | ||
| return | ||
| } | ||
| const inspector = await import('node:inspector') | ||
| session = new inspector.Session() | ||
| session.connect() | ||
| try { | ||
| await new Promise<void>((res, rej) => { | ||
| session!.post('Profiler.enable', (err) => { | ||
| if (err) { | ||
| return rej(err) | ||
| } | ||
| session!.post('Profiler.start', (err) => { | ||
| if (err) { | ||
| return rej(err) | ||
| } | ||
| res() | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
| catch (err) { | ||
| session.disconnect() | ||
| session = undefined | ||
| throw err | ||
| } | ||
| } | ||
|
|
||
| export async function stopCpuProfile(outDir: string, command: string): Promise<string | undefined> { | ||
| if (!session) { | ||
| return | ||
| } | ||
| const s = session | ||
| session = undefined | ||
| const count = profileCount++ | ||
| const outPath = join(outDir, `nuxt-${command}${count ? `-${count}` : ''}.cpuprofile`) | ||
| const relativeOutPath = relative(process.cwd(), outPath).replace(/^(?![^.]{1,2}\/)/, './') | ||
| try { | ||
| await new Promise<any>((resolve, reject) => { | ||
| s.post('Profiler.stop', (err, params) => { | ||
| if (err) { | ||
| return reject(err) | ||
| } | ||
|
|
||
| if (!params?.profile) { | ||
| return resolve(params) | ||
| } | ||
|
|
||
| try { | ||
| mkdirSync(outDir, { recursive: true }) | ||
| writeFileSync(outPath, JSON.stringify(params.profile)) | ||
| const nextSteps = [ | ||
| `CPU profile written to ${colors.cyan(relativeOutPath)}.`, | ||
| `Open it in a CPU profile viewer like your IDE, or ${colors.cyan('https://discoveryjs.github.io/cpupro')}.`, | ||
| ] | ||
| box(`\n${nextSteps.map(step => ` › ${step}`).join('\n')}\n`, '', { | ||
| contentAlign: 'left', | ||
| titleAlign: 'left', | ||
| width: 'auto', | ||
| titlePadding: 2, | ||
| contentPadding: 2, | ||
| rounded: true, | ||
| withGuide: false, | ||
| formatBorder: (text: string) => `${themeColor + text}\x1B[0m`, | ||
| }) | ||
| } | ||
| catch {} | ||
|
|
||
| resolve(params) | ||
| }) | ||
| }) | ||
| } | ||
| finally { | ||
| s.disconnect() | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.