Skip to content

Commit

Permalink
Support .cpuprofile of node --cpu-prof (#256)
Browse files Browse the repository at this point in the history
* Support `.cpuprofile` of `node --cpu-prof`

* `convert_cpu_prof` -> `convertCpuProf`

* Do separate test script for `node --cpu-prof`

* Fix linter `const` error

* Format code according to `standard`
  • Loading branch information
kazarmy authored Jun 14, 2022
1 parent 5c71dae commit 5621be1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/cpu-profiler/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ node_modules
win
todo
.vscode
flamegraph.html
flamegraph*.html
v8-profile.json
10 changes: 10 additions & 0 deletions examples/cpu-profiler/cpu-prof-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

node --cpu-prof --cpu-prof-name=samples-cpu-prof.cpuprofile index.js &

sleep 1
node_modules/.bin/autocannon -l -R 200000 -c 2000 -d 20 http://localhost:3000
curl -v http://localhost:3000/stop-server
wait

node node_modules/.bin/0x --name=flamegraph-cpu-prof --visualize-cpu-profile samples-cpu-prof.cpuprofile
6 changes: 5 additions & 1 deletion examples/cpu-profiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ router.get('/stop-profiling', async (ctx, next) => {
}
})

router.get('/stop-server', (ctx, next) => {
server.close()
})

app
.use(router.routes())
.use(router.allowedMethods())

app.listen(3000)
const server = app.listen(3000)
1 change: 1 addition & 0 deletions examples/cpu-profiler/samples-cpu-prof.cpuprofile

Large diffs are not rendered by default.

38 changes: 37 additions & 1 deletion lib/cpu-profile-to-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,44 @@ function convert (profile) {
return converted
}

function id2IndexMap (nodes) {
const map = new Array(nodes.length + 1)
for (let i = 0; i < nodes.length; i++) {
const node = nodes[i]
map[node.id] = i
}
return map
}

function convertCpuProf (nodes, id2Index, id) {
const node = nodes[id2Index[id]]
const name = node.callFrame.functionName
const url = node.callFrame.url
const lineNumber = node.callFrame.lineNumber
const childrenLength = node.children ? node.children.length : 0
const converted = {
children: new Array(childrenLength),
name: `${name}${url ? ' ' + url : ''}${lineNumber > 0 ? `:${lineNumber}` : ''}`,
top: node.hitCount,
value: node.hitCount,
S: 0
}
if (name[0] === '(' && childrenLength === 0) {
// filter out (garbage collector) and (idle)
converted.top = 0
converted.value = 0
}
for (let i = 0; i < childrenLength; i++) {
converted.children[i] = convertCpuProf(nodes, id2Index, node.children[i])
converted.value += converted.children[i].value
}
return converted
}

function cpuProfileToTree (profile) {
const converted = convert(profile.head)
const converted = profile.head
? convert(profile.head)
: convertCpuProf(profile.nodes, id2IndexMap(profile.nodes), 1)
return {
merged: converted,
unmerged: converted
Expand Down

0 comments on commit 5621be1

Please sign in to comment.