Commit f1033c39 authored by Jimmy Lai's avatar Jimmy Lai

add debug timings

parent ec25a1de
...@@ -319,6 +319,7 @@ const nextDev = async ( ...@@ -319,6 +319,7 @@ const nextDev = async (
nodeOptions.inspect = formatDebugAddress(address) nodeOptions.inspect = formatDebugAddress(address)
} }
const forkStartTime = Date.now()
child = fork(startServerPath, { child = fork(startServerPath, {
stdio: 'inherit', stdio: 'inherit',
env: { env: {
...@@ -330,6 +331,8 @@ const nextDev = async ( ...@@ -330,6 +331,8 @@ const nextDev = async (
NEXT_PRIVATE_TRACE_ID: traceId, NEXT_PRIVATE_TRACE_ID: traceId,
// Pass server options via env to eliminate IPC handshake latency // Pass server options via env to eliminate IPC handshake latency
NEXT_PRIVATE_WORKER_OPTIONS: JSON.stringify(startServerOptions), 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 NODE_EXTRA_CA_CERTS: startServerOptions.selfSignedCertificate
? startServerOptions.selfSignedCertificate.rootCA ? startServerOptions.selfSignedCertificate.rootCA
: defaultEnv.NODE_EXTRA_CA_CERTS, : defaultEnv.NODE_EXTRA_CA_CERTS,
......
...@@ -94,16 +94,24 @@ export async function initialize(opts: { ...@@ -94,16 +94,24 @@ export async function initialize(opts: {
startServerSpan?: Span startServerSpan?: Span
quiet?: boolean quiet?: boolean
}): Promise<ServerInitResult> { }): Promise<ServerInitResult> {
const timings: Record<string, number> = {}
const mark = (name: string) => {
timings[name] = performance.now()
}
mark('init-start')
if (!process.env.NODE_ENV) { if (!process.env.NODE_ENV) {
// @ts-ignore not readonly // @ts-ignore not readonly
process.env.NODE_ENV = opts.dev ? 'development' : 'production' process.env.NODE_ENV = opts.dev ? 'development' : 'production'
} }
mark('before-loadConfig')
const config = await loadConfig( const config = await loadConfig(
opts.dev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_SERVER, opts.dev ? PHASE_DEVELOPMENT_SERVER : PHASE_PRODUCTION_SERVER,
opts.dir, opts.dir,
{ silent: false } { silent: false }
) )
mark('after-loadConfig')
let compress: ReturnType<typeof setupCompression> | undefined let compress: ReturnType<typeof setupCompression> | undefined
...@@ -111,12 +119,14 @@ export async function initialize(opts: { ...@@ -111,12 +119,14 @@ export async function initialize(opts: {
compress = setupCompression() compress = setupCompression()
} }
mark('before-fsCheck')
const fsChecker = await setupFsCheck({ const fsChecker = await setupFsCheck({
dev: opts.dev, dev: opts.dev,
dir: opts.dir, dir: opts.dir,
config, config,
minimalMode: opts.minimalMode, minimalMode: opts.minimalMode,
}) })
mark('after-fsCheck')
const renderServer: LazyRenderServerInstance = {} const renderServer: LazyRenderServerInstance = {}
...@@ -131,6 +141,7 @@ export async function initialize(opts: { ...@@ -131,6 +141,7 @@ export async function initialize(opts: {
let originalFetch = globalThis.fetch let originalFetch = globalThis.fetch
if (opts.dev) { if (opts.dev) {
mark('dev-block-start')
const { Telemetry } = const { Telemetry } =
require('../../telemetry/storage') as typeof import('../../telemetry/storage') require('../../telemetry/storage') as typeof import('../../telemetry/storage')
...@@ -138,11 +149,14 @@ export async function initialize(opts: { ...@@ -138,11 +149,14 @@ export async function initialize(opts: {
distDir: path.join(opts.dir, config.distDir), distDir: path.join(opts.dir, config.distDir),
}) })
traceGlobals.set('telemetry', telemetry) traceGlobals.set('telemetry', telemetry)
mark('after-telemetry')
const { pagesDir, appDir } = findPagesDir(opts.dir) const { pagesDir, appDir } = findPagesDir(opts.dir)
mark('after-findPagesDir')
const { setupDevBundler } = const { setupDevBundler } =
require('./router-utils/setup-dev-bundler') as typeof import('./router-utils/setup-dev-bundler') require('./router-utils/setup-dev-bundler') as typeof import('./router-utils/setup-dev-bundler')
mark('after-require-setupDevBundler')
const resetFetch = () => { const resetFetch = () => {
globalThis.fetch = originalFetch globalThis.fetch = originalFetch
...@@ -153,6 +167,7 @@ export async function initialize(opts: { ...@@ -153,6 +167,7 @@ export async function initialize(opts: {
? opts.startServerSpan.traceChild('setup-dev-bundler') ? opts.startServerSpan.traceChild('setup-dev-bundler')
: trace('setup-dev-bundler') : trace('setup-dev-bundler')
mark('before-setupDevBundler')
const developmentBundler = await setupDevBundlerSpan.traceAsyncFn(() => const developmentBundler = await setupDevBundlerSpan.traceAsyncFn(() =>
setupDevBundler({ setupDevBundler({
renderServer, renderServer,
...@@ -169,6 +184,7 @@ export async function initialize(opts: { ...@@ -169,6 +184,7 @@ export async function initialize(opts: {
resetFetch, resetFetch,
}) })
) )
mark('after-setupDevBundler')
const devBundlerService = new DevBundlerService( const devBundlerService = new DevBundlerService(
developmentBundler, developmentBundler,
...@@ -182,6 +198,7 @@ export async function initialize(opts: { ...@@ -182,6 +198,7 @@ export async function initialize(opts: {
service: devBundlerService, service: devBundlerService,
config: config as NextConfigComplete, config: config as NextConfigComplete,
} }
mark('dev-block-end')
} }
renderServer.instance = renderServer.instance =
...@@ -886,6 +903,23 @@ export async function initialize(opts: { ...@@ -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 { return {
requestHandler, requestHandler,
upgradeHandler, 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. // Start CPU profile if it wasn't already started.
import './cpu-profile' import './cpu-profile'
import { getNetworkHost } from '../../lib/get-network-host' import { getNetworkHost } from '../../lib/get-network-host'
...@@ -40,6 +46,12 @@ import type { ConfiguredExperimentalFeature } from '../config' ...@@ -40,6 +46,12 @@ import type { ConfiguredExperimentalFeature } from '../config'
const debug = setupDebug('next:start-server') const debug = setupDebug('next:start-server')
let startServerSpan: Span | undefined 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 * 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