Skip to content

Commit

Permalink
cpu-profile-to-tree: Refactor out the filtering of parenthesized nodes (
Browse files Browse the repository at this point in the history
#258)

* cpu-profile-to-tree: Refactor out the filtering of parenthesized nodes

* Support Node 12.x
  • Loading branch information
kazarmy authored Jun 19, 2022
1 parent 9996d45 commit 0ba1e2b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
40 changes: 23 additions & 17 deletions lib/cpu-profile-to-tree.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
'use strict'

function convert (profile) {
// Filter out all parenthesized nodes with no children i.e. (idle), (garbage
// collector) and (program)
function filterParenthesizedNoChild (profileNode, convertedNode) {
let name = profileNode.functionName
if (!name) {
name = profileNode.callFrame.functionName
}
const childrenLength = profileNode.children ? profileNode.children.length : 0
if (name[0] === '(' && childrenLength === 0) {
convertedNode.top = 0
convertedNode.value = 0
}
}

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

Expand Down Expand Up @@ -43,11 +53,7 @@ function convertCpuProf (nodes, id2Index, id) {
value: node.hitCount,
S: 0
}
if (name[0] === '(' && childrenLength === 0) {
// filter out (garbage collector) and (idle)
converted.top = 0
converted.value = 0
}
filterParenthesizedNoChild(node, converted)
for (let i = 0; i < childrenLength; i++) {
converted.children[i] = convertCpuProf(nodes, id2Index, node.children[i])
converted.value += converted.children[i].value
Expand Down
38 changes: 37 additions & 1 deletion test/cpu-profile-to-tree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ const exampleProfile = {
children: []
}
]
},
{
functionName: '(idle)',
url: '',
lineNumber: 0,
callUID: 249,
bailoutReason: '',
id: 39,
scriptId: 0,
hitCount: 3678,
children: []
}
]
}
Expand Down Expand Up @@ -75,6 +86,13 @@ const _expected = {
top: 0,
value: 10,
S: 0
},
{
children: [],
name: '(idle)',
top: 0,
value: 0,
S: 0
}
],
name: '(root)',
Expand Down Expand Up @@ -118,7 +136,7 @@ const exampleCpuProfProfile = {
columnNumber: -1
},
hitCount: 0,
children: [2]
children: [2, 5]
},
{
id: 3,
Expand All @@ -142,6 +160,17 @@ const exampleCpuProfProfile = {
},
hitCount: 2,
children: [3, 4]
},
{
id: 5,
callFrame: {
functionName: '(garbage collector)',
scriptId: 0,
url: '',
lineNumber: -1,
columnNumber: -1
},
hitCount: 766
}
]
}
Expand Down Expand Up @@ -169,6 +198,13 @@ const _expectedCpuProf = {
top: 2,
value: 12,
S: 0
},
{
children: [],
name: '(garbage collector)',
top: 0,
value: 0,
S: 0
}
],
name: '(root)',
Expand Down

0 comments on commit 0ba1e2b

Please sign in to comment.