Last active
August 29, 2015 14:17
-
-
Save pflannery/8e38a06a844a7fc362dc to your computer and use it in GitHub Desktop.
Generate CPU profile for node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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