Skip to content
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

Fix CPU profile generation #55365

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/next/src/cli/next-build.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env node

import '../server/lib/cpu-profile'
import { existsSync } from 'fs'
import * as Log from '../build/output/log'
import { CliCommand } from '../lib/commands'
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/cli/next-dev.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env node

import '../server/lib/cpu-profile'
import type { StartServerOptions } from '../server/lib/start-server'
import { getPort, printAndExit } from '../server/lib/utils'
import * as Log from '../build/output/log'
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/cli/next-start.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

import '../server/lib/cpu-profile'
import { startServer } from '../server/lib/start-server'
import { getPort, printAndExit } from '../server/lib/utils'
import { getProjectDir } from '../lib/get-project-dir'
Expand Down
8 changes: 6 additions & 2 deletions packages/next/src/server/lib/cpu-profile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
if (process.env.__NEXT_PRIVATE_CPU_PROFILE) {
const privateCpuProfileName = process.env.__NEXT_PRIVATE_CPU_PROFILE
const isCpuProfileEnabled = process.env.NEXT_CPU_PROF || privateCpuProfileName

if (isCpuProfileEnabled) {
const { Session } = require('inspector') as typeof import('inspector')
const fs = require('fs')

Expand All @@ -17,12 +20,13 @@ if (process.env.__NEXT_PRIVATE_CPU_PROFILE) {

// Write profile to disk
const filename = `${
process.env.__NEXT_PRIVATE_CPU_PROFILE
privateCpuProfileName || 'CPU.main'
}.${Date.now()}.cpuprofile`
fs.writeFileSync(`./${filename}`, JSON.stringify(param.profile))
process.exit(0)
})
}
process.on('SIGINT', saveProfile)
process.on('SIGTERM', saveProfile)
process.on('exit', saveProfile)
}
1 change: 0 additions & 1 deletion packages/next/src/server/lib/setup-server-worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import './cpu-profile'
import v8 from 'v8'
import http, { IncomingMessage, ServerResponse } from 'http'

Expand Down
5 changes: 0 additions & 5 deletions packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ import chalk from '../../lib/chalk'

const debug = setupDebug('next:start-server')

if (process.env.NEXT_CPU_PROF) {
process.env.__NEXT_PRIVATE_CPU_PROFILE = `CPU.router`
require('./cpu-profile')
}

export interface StartServerOptions {
dir: string
port: number
Expand Down