Commit f1033c39 authored by Jimmy Lai's avatar Jimmy Lai

add debug timings

parent ec25a1de
......@@ -319,6 +319,7 @@ const nextDev = async (
nodeOptions.inspect = formatDebugAddress(address)
}
const forkStartTime = Date.now()
child = fork(startServerPath, {
stdio: 'inherit',
env: {
......@@ -330,6 +331,8 @@ const nextDev = async (
NEXT_PRIVATE_TRACE_ID: traceId,
// Pass server options via env to eliminate IPC handshake latency
NEXT_PRIVATE_WORKER_OPTIONS: JSON.stringify(startServerOptions),
// Pass fork start time for timing measurement
NEXT_PRIVATE_FORK_TIME: String(forkStartTime),
NODE_EXTRA_CA_CERTS: startServerOptions.selfSignedCertificate
? startServerOptions.selfSignedCertificate.rootCA
: defaultEnv.NODE_EXTRA_CA_CERTS,
......
......@@ -94,16 +94,24 @@ export async function initialize(opts: {
startServerSpan?: Span
quiet?: boolean
}): Promise<ServerInitResult> {
const timings: Record<string, number> = {}
const mark = (name: string) => {
timings[name] = performance.now()
}
mark('init-start')
if (!process.env.NODE_ENV) {
// @ts-ignore not readonly
process.env.NODE_ENV = opts.dev ? 'development' : 'production'
}
mark('before-loadConfig')
const config = await loadConfig(
opts.dev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_SERVER,
opts.dir,
{ silent: false }
)
mark('after-loadConfig')
let compress: ReturnType<typeof setupCompression> | undefined
......@@ -111,12 +119,14 @@ export async function initialize(opts: {
compress = setupCompression()
}
mark('before-fsCheck')
const fsChecker = await setupFsCheck({
dev: opts.dev,
dir: opts.dir,
config,
minimalMode: opts.minimalMode,
})
mark('after-fsCheck')
const renderServer: LazyRenderServerInstance = {}
......@@ -131,6 +141,7 @@ export async function initialize(opts: {
let originalFetch = globalThis.fetch
if (opts.dev) {
mark('dev-block-start')
const { Telemetry } =
require('../../telemetry/storage') as typeof import('../../telemetry/storage')
......@@ -138,11 +149,14 @@ export async function initialize(opts: {
distDir: path.join(opts.dir, config.distDir),
})
traceGlobals.set('telemetry', telemetry)
mark('after-telemetry')
const { pagesDir, appDir } = findPagesDir(opts.dir)
mark('after-findPagesDir')
const { setupDevBundler } =
require('./router-utils/setup-dev-bundler') as typeof import('./router-utils/setup-dev-bundler')
mark('after-require-setupDevBundler')
const resetFetch = () => {
globalThis.fetch = originalFetch
......@@ -153,6 +167,7 @@ export async function initialize(opts: {
? opts.startServerSpan.traceChild('setup-dev-bundler')
: trace('setup-dev-bundler')
mark('before-setupDevBundler')
const developmentBundler = await setupDevBundlerSpan.traceAsyncFn(() =>
setupDevBundler({
renderServer,
......@@ -169,6 +184,7 @@ export async function initialize(opts: {
resetFetch,
})
)
mark('after-setupDevBundler')
const devBundlerService = new DevBundlerService(
developmentBundler,
......@@ -182,6 +198,7 @@ export async function initialize(opts: {
service: devBundlerService,
config: config as NextConfigComplete,
}
mark('dev-block-end')
}
renderServer.instance =
......@@ -886,6 +903,23 @@ export async function initialize(opts: {
}
}
mark('init-end')
// Log timing breakdown
if (debug.enabled) {
const entries = Object.entries(timings)
const start = timings['init-start']
debug('timing breakdown:')
for (let i = 1; i < entries.length; i++) {
const [prevName] = entries[i - 1]
const [currName, currTime] = entries[i]
debug(
` ${prevName} → ${currName}: ${(currTime - entries[i - 1][1]).toFixed(1)}ms`
)
}
debug(` TOTAL: ${(timings['init-end'] - start).toFixed(1)}ms`)
}
return {
requestHandler,
upgradeHandler,
......
// Measure fork/spawn overhead - must be at the very top
const childStartTime = Date.now()
const forkTime = process.env.NEXT_PRIVATE_FORK_TIME
? childStartTime - parseInt(process.env.NEXT_PRIVATE_FORK_TIME, 10)
: null
// Start CPU profile if it wasn't already started.
import './cpu-profile'
import { getNetworkHost } from '../../lib/get-network-host'
......@@ -40,6 +46,12 @@ import type { ConfiguredExperimentalFeature } from '../config'
const debug = setupDebug('next:start-server')
let startServerSpan: Span | undefined
// Log fork/spawn timing
const moduleLoadTime = Date.now() - childStartTime
if (forkTime !== null) {
debug(`fork/spawn overhead: ${forkTime}ms, module load: ${moduleLoadTime}ms`)
}
/**
* Get the process ID (PID) of the process using the specified port
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment