Skip to content

Instantly share code, notes, and snippets.

@pflannery
Last active August 29, 2015 14:17
Show Gist options
  • Save pflannery/8e38a06a844a7fc362dc to your computer and use it in GitHub Desktop.
Save pflannery/8e38a06a844a7fc362dc to your computer and use it in GitHub Desktop.
Generate CPU profile for node
var fs = require('fs'),
profiler = require('v8-profiler');
function saveHeapSnapshot(fileName) {
var buffer = '';
var snapshot = profiler.takeSnapshot("test");
snapshot.serialize(
function iterator(data, length) {
buffer += data;
},function complete(){
fs.writeFileSync(fileName, buffer)
}
);
}
function saveCpuProfile(outputPath, profile) {
var profileJSON;
profileJSON = JSON.stringify(profile);
// lets us see the deopt reason in latest chrome
profileJSON = profileJSON.replace(/"bailoutReason":/g, '"deoptReason":');
fs.writeFileSync(outputPath, profileJSON);
}
var recordSamples = true; // generate flame data
profiler.startProfiling("taskgroup", recordSamples);
// code to profile
var TaskGroup = require("taskgroup").TaskGroup;
var testArray = Array(10000).join().split(',');
var grp = TaskGroup.create();
testArray.forEach(function(value, index) {
grp.addTask(function() {
// console.log("task"+index);
});
});
grp.on("completed", function(){
// saveSnapshot("completed.heapsnapshot"); // genereates heap snapshot
var cpuProfile = profiler.stopProfiling("taskgroup");
saveCpuProfile("complete.cpuprofile", cpuProfile);
// clean up
cpuProfile.delete()
// profiler.deleteAllProfiles();
// profiler.deleteAllSnapshots();
});
grp.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment