Skip to content

Commit

Permalink
feat: new cpuprofile format (#113)
Browse files Browse the repository at this point in the history
PR-URL: #113
Reviewed-BY: hyj1991 <[email protected]>
  • Loading branch information
hyj1991 authored Dec 13, 2021
1 parent 395a678 commit 1e3ddb7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
17 changes: 10 additions & 7 deletions src/commands/cpuprofiler/cpu_profile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ void Profile::Serialize(const CpuProfile *node, std::string filename) {
Utf8String title(node->GetTitle());
writer.json_keyvalue("title", *title);

// set head
writer.json_objectstart("head");
// set nodes
writer.json_arraystart("nodes");
ProfileNode::SerializeNode(node->GetTopDownRoot(), &writer);
writer.json_objectend();
writer.json_arrayend();

// set start/end time
writer.json_keyvalue("startTime", node->GetStartTime() / 1000000);
writer.json_keyvalue("endTime", node->GetEndTime() / 1000000);
writer.json_keyvalue("startTime", node->GetStartTime());
writer.json_keyvalue("endTime", node->GetEndTime());

// set samples
uint32_t count = node->GetSamplesCount();
Expand All @@ -46,9 +46,12 @@ void Profile::Serialize(const CpuProfile *node, std::string filename) {
writer.json_arrayend();

// set timestamps
writer.json_arraystart("timestamps");
writer.json_arraystart("timeDeltas");
for (uint32_t index = 0; index < count; ++index) {
writer.json_element(node->GetSampleTimestamp(index));
uint32_t prev =
index == 0 ? node->GetStartTime() : node->GetSampleTimestamp(index - 1);
uint32_t delta = node->GetSampleTimestamp(index) - prev;
writer.json_element(delta);
}
writer.json_arrayend();

Expand Down
25 changes: 16 additions & 9 deletions src/commands/cpuprofiler/cpu_profile_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,32 @@ void ProfileNode::SerializeNode(const CpuProfileNode *node,
Utf8String funcion_name(node->GetFunctionName());
Utf8String url(node->GetScriptResourceName());

// set parent
// set node
writer->json_start();
writer->json_keyvalue("id", node->GetNodeId());
writer->json_keyvalue("hitCount", node->GetHitCount());
// set call frame
writer->json_objectstart("callFrame");
writer->json_keyvalue("functionName", *funcion_name);
writer->json_keyvalue("scriptId", node->GetScriptId());
writer->json_keyvalue("bailoutReason", node->GetBailoutReason());
writer->json_keyvalue("url", *url);
writer->json_keyvalue("lineNumber", node->GetLineNumber());
writer->json_keyvalue("columnNumber", node->GetColumnNumber());
writer->json_keyvalue("bailoutReason", node->GetBailoutReason());
writer->json_keyvalue("id", node->GetNodeId());
writer->json_keyvalue("scriptId", node->GetScriptId());
writer->json_keyvalue("hitCount", node->GetHitCount());
writer->json_objectend();

// set children recursively
// set children
int32_t count = node->GetChildrenCount();
writer->json_arraystart("children");
for (int32_t index = 0; index < count; index++) {
writer->json_start();
ProfileNode::SerializeNode(node->GetChild(index), writer);
writer->json_end();
writer->json_element(node->GetChild(index)->GetNodeId());
}
writer->json_arrayend();
writer->json_end();

for (int32_t index = 0; index < count; index++) {
ProfileNode::SerializeNode(node->GetChild(index), writer);
}
}

} // namespace xprofiler
24 changes: 13 additions & 11 deletions test/fixtures/command.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,23 @@ const isArray = value => Array.isArray(value);
const cpuprofile = {
typeId: /^xprofiler-cpu-profile$/,
title: /^xprofiler$/,
head: {
functionName: /^([\w\s()]+|)$/,
url: /^([.\w()/\\:]+|)$/,
lineNumber: /^\d+$/,
columnNumber: /^\d+$/,
bailoutReason: /^([\w\s]+|)$/,
nodes: [{
id: /^\d+$/,
scriptId: /^\d+$/,
hitCount: /^\d+$/,
callFrame: {
functionName: /^([.\w\s()-_]+|)$/,
scriptId: /^\d+$/,
bailoutReason: /^([\w\s]+|)$/,
url: /^([@.\w()/\\:_-]+|)$/,
lineNumber: /^\d+$/,
columnNumber: /^\d+$/,
},
children: isArray
},
}],
startTime: /^\d+$/,
endTime: /^\d+$/,
samples: isArray,
timestamps: isArray
timeDeltas: isArray
};

const heapsnapshot = {
Expand Down Expand Up @@ -94,9 +96,9 @@ const heapsnapshot = {
const heapprofile = {
head: {
callFrame: {
functionName: /^([\w\s()]+|)$/,
functionName: /^([.\w\s()-_]+|)$/,
scriptId: /^\d+$/,
url: /^([.\w()/\\:]+|)$/,
url: /^([@.\w()/\\:_-]+|)$/,
lineNumber: /^\d+$/,
columnNumber: /^\d+$/
},
Expand Down

0 comments on commit 1e3ddb7

Please sign in to comment.