Skip to content

Commit

Permalink
Enable idle notifier in node.js when profiling
Browse files Browse the repository at this point in the history
Now idle time is marked as "(idle)" instead of just "(program)".
  • Loading branch information
Oleksandr Chekhovskyi committed Aug 31, 2015
1 parent 49461b2 commit 422f53d
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions v8-profiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ CpuProfile.prototype.getHeader = function() {
}

var startTime, endTime;
var activeProfiles = [];

var profiler = {
/*HEAP PROFILER API*/
Expand Down Expand Up @@ -111,16 +112,37 @@ var profiler = {
get profiles() { return binding.cpu.profiles; },

startProfiling: function(name, recsamples) {
if (activeProfiles.length == 0 && typeof process._startProfilerIdleNotifier == "function")
process._startProfilerIdleNotifier();

name = name || "";

if (activeProfiles.indexOf(name) < 0)
activeProfiles.push(name)

startTime = Date.now();
binding.cpu.startProfiling(name, recsamples);
},

stopProfiling: function(name) {
var index = activeProfiles.indexOf(name);
if (name && index < 0)
return;

var profile = binding.cpu.stopProfiling(name);
endTime = Date.now();
profile.__proto__ = CpuProfile.prototype;
if (!profile.startTime) profile.startTime = startTime;
if (!profile.endTime) profile.endTime = endTime;

if (name)
activeProfiles.splice(index, 1);
else
activeProfiles.length = activeProfiles.length - 1;

if (activeProfiles.length == 0 && typeof process._stopProfilerIdleNotifier == "function")
process._stopProfilerIdleNotifier();

return profile;
},

Expand Down

0 comments on commit 422f53d

Please sign in to comment.