-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathcpu-profile.ts
32 lines (27 loc) · 934 Bytes
/
cpu-profile.ts
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
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')
const session = new Session()
session.connect()
session.post('Profiler.enable')
session.post('Profiler.start')
function saveProfile() {
session.post('Profiler.stop', (error, param) => {
if (error) {
console.error('Cannot generate CPU profiling:', error)
return
}
// Write profile to disk
const filename = `${
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)
}