()
| 27 | * the same process (via session.connect()). |
| 28 | */ |
| 29 | export 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 | } |
no test coverage detected