File size: 1,291 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
#!/usr/bin/env node
/**
 * Analyze a CPU profile to identify hot modules
 */

const fs = require('fs')

const profilePath = process.argv[2]
if (!profilePath) {
  console.error('Usage: node analyze-profile.js <profile.cpuprofile>')
  process.exit(1)
}

const profile = JSON.parse(fs.readFileSync(profilePath, 'utf-8'))

// Extract nodes with their hit counts
const nodes = profile.nodes || []

// Group by file/module
const moduleHits = {}
nodes.forEach((node) => {
  const fn = node.callFrame
  if (fn && fn.url) {
    const url = fn.url
    // Extract module name from path
    let moduleName = url
    if (url.includes('next/dist/')) {
      moduleName = url.split('next/dist/')[1]
    } else if (url.includes('node_modules/')) {
      moduleName = 'node_modules/' + url.split('node_modules/').pop()
    }
    if (!moduleHits[moduleName]) {
      moduleHits[moduleName] = { hits: 0 }
    }
    moduleHits[moduleName].hits += node.hitCount || 0
  }
})

// Sort by hits
const sorted = Object.entries(moduleHits)
  .filter(([_, v]) => v.hits > 0)
  .sort((a, b) => b[1].hits - a[1].hits)
  .slice(0, 40)

console.log('Top 40 modules by CPU time:')
console.log('='.repeat(70))
sorted.forEach(([name, data], i) => {
  console.log(`${String(i + 1).padStart(2)}. ${name} (${data.hits} hits)`)
})