MCPcopy
hub / github.com/vercel/next.js / saveCpuProfile

Function saveCpuProfile

packages/next/src/server/lib/cpu-profile.ts:29–67  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

27 * the same process (via session.connect()).
28 */
29export function saveCpuProfile(): void {
30 if (!session || profileSaved || !isCpuProfileEnabled) {
31 return
32 }
33 profileSaved = true
34
35 const fs = require('fs') as typeof import('fs')
36 const path = require('path') as typeof import('path')
37
38 session!.post('Profiler.stop', (error, param) => {
39 if (error) {
40 console.error('Cannot generate CPU profiling:', error)
41 return
42 }
43
44 const timestamp = new Date()
45 .toISOString()
46 .replace(/[:.]/g, '-')
47 .slice(0, 19)
48 const baseName = privateCpuProfileName || 'cpu-profile'
49 const filename = `${baseName}-${timestamp}.cpuprofile`
50
51 let outputPath: string
52 if (cpuProfileDir) {
53 if (!fs.existsSync(cpuProfileDir)) {
54 fs.mkdirSync(cpuProfileDir, { recursive: true })
55 }
56 outputPath = path.join(cpuProfileDir, filename)
57 } else {
58 outputPath = `./${filename}`
59 }
60
61 fs.writeFileSync(outputPath, JSON.stringify(param.profile))
62 const { green } =
63 require('../../lib/picocolors') as typeof import('../../lib/picocolors')
64 console.log(`\n${green('CPU profile saved:')} ${outputPath}`)
65 console.log('Open in Chrome DevTools → Performance tab → Load profile')
66 })
67}

Callers 5

handleSessionStopFunction · 0.90
nextBuildFunction · 0.90
nextStartFunction · 0.90
nextAnalyzeFunction · 0.90
cpu-profile.tsFile · 0.85

Calls 8

greenFunction · 0.85
errorMethod · 0.65
replaceMethod · 0.65
requireFunction · 0.50
sliceMethod · 0.45
joinMethod · 0.45
stringifyMethod · 0.45
logMethod · 0.45

Tested by

no test coverage detected