File size: 9,430 Bytes
b91e262
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env node
/**
 * Next.js CPU Profile Script
 *
 * Generates CPU profiles for Next.js startup and dev server boot.
 *
 * Usage:
 *   node scripts/profile-next-dev-boot.js [options]
 *
 * Options:
 *   --test-dir=PATH     Test project directory (default: /private/tmp/next-boot-test)
 *   --output-dir=PATH   Output directory for profiles (default: ./profiles)
 *   --turbopack         Use Turbopack (default)
 *   --webpack           Use Webpack
 *   --duration=MS       How long to profile after ready (default: 1000)
 *   --cli               Profile just the CLI entry point (runs next --help)
 *
 * Output files:
 *   - dev-turbopack-YYYY-MM-DDTHH-MM-SS.cpuprofile
 *   - cli-turbopack-YYYY-MM-DDTHH-MM-SS.cpuprofile
 *
 * The profile can be loaded in:
 *   - Chrome DevTools (Performance tab -> Load profile)
 *   - VS Code (JavaScript Profile Visualizer extension)
 *   - https://www.speedscope.app/
 *
 * Note: Currently profiles the parent process only. For child process profiling,
 * additional Next.js changes are needed (see future PRs).
 */

const { spawn, execSync } = require('child_process')
const path = require('path')
const fs = require('fs')

// Parse arguments
const args = process.argv.slice(2)
const getArg = (name, defaultValue) => {
  const arg = args.find((a) => a.startsWith(`--${name}=`))
  return arg ? arg.split('=')[1] : defaultValue
}
const hasFlag = (name) => args.includes(`--${name}`)

const testDir = getArg('test-dir', '/private/tmp/next-boot-test')
const baseOutputDir =
  getArg('output-dir', null) || path.join(process.cwd(), 'profiles')
const useWebpack = hasFlag('webpack')
const duration = parseInt(getArg('duration', '1000'), 10)
const profileCli = hasFlag('cli')
const bundlerFlag = useWebpack ? '--webpack' : '--turbopack'

// Generate meaningful profile names
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, 19)
const bundlerName = useWebpack ? 'webpack' : 'turbopack'
const profileType = profileCli ? 'cli' : 'dev'
const outputDir = baseOutputDir
const profileName = `${profileType}-${bundlerName}-${timestamp}`

const nextDir = path.join(__dirname, '..', 'packages', 'next')
const nextBin = path.join(nextDir, 'dist/bin/next')

if (profileCli) {
  console.log('\x1b[34m=== Next.js CLI Entry Point Profile ===\x1b[0m')
} else {
  console.log('\x1b[34m=== Next.js Dev Server CPU Profile ===\x1b[0m')
  console.log(`Test directory: ${testDir}`)
  console.log(`Bundler: ${useWebpack ? 'Webpack' : 'Turbopack'}`)
}
console.log(`Output directory: ${outputDir}`)
console.log('')

// Verify test directory (only for dev server profiling)
if (!profileCli && !fs.existsSync(testDir)) {
  console.error(
    `\x1b[31mError: Test directory does not exist: ${testDir}\x1b[0m`
  )
  process.exit(1)
}

// Create output directory
if (!fs.existsSync(outputDir)) {
  fs.mkdirSync(outputDir, { recursive: true })
}

// Kill existing processes
function killNextDev() {
  try {
    execSync('pkill -f "next dev"', { stdio: 'ignore' })
  } catch {}
}

async function runProfile() {
  killNextDev()
  await new Promise((r) => setTimeout(r, 500))

  // Clean .next directory
  const nextCache = path.join(testDir, '.next')
  if (fs.existsSync(nextCache)) {
    fs.rmSync(nextCache, { recursive: true, force: true })
  }

  console.log('Starting dev server with CPU profiling...')
  console.log('(Profile will be saved after server is ready)')
  console.log('')

  return new Promise((resolve, reject) => {
    let resolved = false

    // Profile the parent process with --cpu-prof
    const spawnArgs = [
      process.execPath,
      [
        '--cpu-prof',
        `--cpu-prof-dir=${outputDir}`,
        `--cpu-prof-name=${profileName}`,
        nextBin,
        'dev',
        bundlerFlag,
      ],
    ]

    const child = spawn(spawnArgs[0], spawnArgs[1], {
      cwd: testDir,
      stdio: ['ignore', 'pipe', 'pipe'],
      env: { ...process.env, FORCE_COLOR: '0' },
    })

    let output = ''

    const onData = (data) => {
      const text = data.toString()
      output += text
      process.stdout.write(text)

      // Wait for "Ready in Xms"
      if (output.includes('Ready in') && !resolved) {
        resolved = true
        console.log('')
        console.log(
          `\x1b[33mServer ready, profiling for ${duration}ms more...\x1b[0m`
        )

        // Wait a bit then stop
        setTimeout(() => {
          console.log('Stopping server and saving profile...')
          child.kill('SIGINT')
        }, duration)
      }
    }

    child.stdout.on('data', onData)
    child.stderr.on('data', onData)

    child.on('close', (code) => {
      killNextDev()

      // Wait a moment for profile files to be written
      setTimeout(() => {
        // Find and rename profiles matching our name pattern
        // --cpu-prof-name creates files without extension
        const files = fs.readdirSync(outputDir)
        const rawFiles = files.filter(
          (f) => f.startsWith(profileName) && !f.endsWith('.cpuprofile')
        )

        // Rename raw files to have .cpuprofile extension
        rawFiles.forEach((f) => {
          const oldPath = path.join(outputDir, f)
          const newPath = path.join(outputDir, `${f}.cpuprofile`)
          fs.renameSync(oldPath, newPath)
        })

        // Now find all .cpuprofile files
        const profileFiles = fs
          .readdirSync(outputDir)
          .filter((f) => f.startsWith(profileName) && f.endsWith('.cpuprofile'))
        const profiles = profileFiles
          .map((f) => ({
            name: f,
            path: path.join(outputDir, f),
            size: fs.statSync(path.join(outputDir, f)).size,
          }))
          .filter((p) => p.size > 0)
          .sort((a, b) => b.size - a.size)

        if (profiles.length > 0) {
          console.log('')
          console.log(`\x1b[32mProfile(s) saved:\x1b[0m`)
          profiles.forEach((p, i) => {
            const sizeKB = Math.round(p.size / 1024)
            console.log(`  ${i + 1}. ${p.path} (${sizeKB} KB)`)
          })
          console.log('')
          console.log('To view the profile:')
          console.log('  1. Open Chrome DevTools -> Performance tab')
          console.log('  2. Click "Load profile" and select the file')
          console.log('  3. Or use https://www.speedscope.app/')
          console.log('')
          console.log(
            '\x1b[33mTip:\x1b[0m The largest profile is usually the child process (server worker)'
          )
          resolve(profiles[0].path)
        } else {
          console.log('')
          console.log(
            '\x1b[33mNo profiles found. Trying alternative method...\x1b[0m'
          )
          console.log('')
          console.log(
            'To profile the child process, modify next-dev.ts to add profiling flags.'
          )
          console.log(
            'Or use: node --cpu-prof --cpu-prof-dir=./profiles ./dist/bin/next dev'
          )
          reject(new Error('Profile file not found'))
        }
      }, 500)
    })

    child.on('error', reject)

    // Timeout
    setTimeout(() => {
      if (!resolved) {
        child.kill('SIGKILL')
        reject(new Error('Timeout waiting for server'))
      }
    }, 120000)
  })
}

async function runCliProfile() {
  console.log('Profiling CLI entry point (next --help)...')
  console.log('')

  return new Promise((resolve, reject) => {
    const child = spawn(
      process.execPath,
      [
        '--cpu-prof',
        `--cpu-prof-dir=${outputDir}`,
        `--cpu-prof-name=${profileName}`,
        nextBin,
        '--help',
      ],
      {
        cwd: process.cwd(),
        stdio: ['ignore', 'pipe', 'pipe'],
        env: { ...process.env, FORCE_COLOR: '0' },
      }
    )

    child.stdout.on('data', () => {})
    child.stderr.on('data', () => {})

    child.on('close', (code) => {
      // Wait for profile to be written
      setTimeout(() => {
        // --cpu-prof-name creates files without extension, rename to .cpuprofile
        const rawFile = path.join(outputDir, profileName)
        const finalFile = path.join(outputDir, `${profileName}.cpuprofile`)

        if (fs.existsSync(rawFile)) {
          fs.renameSync(rawFile, finalFile)
          const size = fs.statSync(finalFile).size
          console.log(`\x1b[32mProfile saved:\x1b[0m`)
          console.log(`  ${finalFile} (${Math.round(size / 1024)} KB)`)
          console.log('')
          console.log('To view the profile:')
          console.log('  1. Open Chrome DevTools -> Performance tab')
          console.log('  2. Click "Load profile" and select the file')
          console.log('  3. Or use https://www.speedscope.app/')
          console.log('')
          console.log(
            '\x1b[33mTip:\x1b[0m Look for heavy modules loaded at startup'
          )
          resolve(finalFile)
        } else if (fs.existsSync(finalFile)) {
          const size = fs.statSync(finalFile).size
          console.log(`\x1b[32mProfile saved:\x1b[0m`)
          console.log(`  ${finalFile} (${Math.round(size / 1024)} KB)`)
          resolve(finalFile)
        } else {
          reject(new Error('Profile file not found'))
        }
      }, 500)
    })

    child.on('error', reject)
  })
}

// Main execution
const main = profileCli ? runCliProfile : runProfile

main().catch((err) => {
  console.error('\x1b[31mError:\x1b[0m', err.message)
  if (!profileCli) killNextDev()
  process.exit(1)
})