Skip to content

Commit

Permalink
src: fix the base case in the heap profiler JSON generation
Browse files Browse the repository at this point in the history
In the base case, the last node needs to have an empty array with
no childrens, this commit handles that by creating an empty array for
it.

Fixes: #124
Signed-off-by: Juan José Arboleda <soyjuanarbol@gmail.com>
  • Loading branch information
juanarbol committed Apr 25, 2024
1 parent e098801 commit 64b040c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/nsolid/nsolid_heap_snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ nlohmann::ordered_json build_sampling_heap_profile_node(
result["id"] = node->node_id;

// Add children if there are any
if (node->children.empty())
if (node->children.empty()) {
result["children"] = nlohmann::ordered_json::array();
return result;
}

// Recursively build children
nlohmann::ordered_json children;
Expand Down
35 changes: 35 additions & 0 deletions test/parallel/test-nsolid-heap-sampling-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,45 @@ const {
{
let profile = '';
const stream = nsolid.heapSamplingStream(0, 1200);
// Do some allocations to make sure we have some data to sample
const arr = [];
for (let i = 0; i < 1000; i++) {
arr.push(new Array(1000));
}
stream.on('data', (data) => {
profile += data;
});
stream.on('end', common.mustCall(() => {
assert(JSON.parse(profile));
testProfileSchema(JSON.parse(profile));
}));
}

function testProfileSchema (profile) {

Check failure on line 119 in test/parallel/test-nsolid-heap-sampling-stream.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected space before function parentheses
// Basic object schema test
assert(profile.head);
assert(profile.samples);

testCallFrame(profile.head);

// Recursive schema test per sample callframe
function testCallFrame (head) {

Check failure on line 127 in test/parallel/test-nsolid-heap-sampling-stream.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected space before function parentheses
assert(Number.isInteger(head.callFrame.columnNumber));
assert(Number.isInteger(head.callFrame.lineNumber));
assert(Number.isInteger(head.selfSize));
assert(Number.isInteger(head.callFrame.scriptId));
assert(Number.isInteger(head.id));
assert(head.callFrame.functionName === '' ||
typeof head.callFrame.functionName === 'string');
assert(head.callFrame.url === '' || typeof head.callFrame.url === 'string');
assert(Array.isArray(head.children));
testChildren(head.children);
}

// Recursive children schema test
function testChildren (children) {

Check failure on line 141 in test/parallel/test-nsolid-heap-sampling-stream.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Unexpected space before function parentheses
let isTestDone = children.length === 0;

Check failure on line 142 in test/parallel/test-nsolid-heap-sampling-stream.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

'isTestDone' is never reassigned. Use 'const' instead
if (!isTestDone) return testCallFrame(children[0]);
assert(isTestDone);
}
}

0 comments on commit 64b040c

Please sign in to comment.