-
Notifications
You must be signed in to change notification settings - Fork 8
/
profile.js
41 lines (34 loc) · 1.08 KB
/
profile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
getProfiler = function(v8Profiler, fs) {
var currentProfiles = {},
colorize = function(s) {
return '\x1b[36m' + s + '\x1b[0m';
}
;
var startProfile = function(profileName, options) {
if (currentProfiles[profileName]) {
return;
}
currentProfiles[profileName] = options;
console.log(colorize('Profiling "' + profileName + '"'));
v8Profiler.startProfiling(profileName);
};
var stopProfile = function(profileName) {
if (!currentProfiles[profileName] || currentProfiles[profileName].complete) {
return;
}
currentProfiles[profileName].complete = true;
var profile = v8Profiler.stopProfiling(profileName),
exportPath = currentProfiles[profileName].exportPath
;
profile.export(function(error, result) {
fs.writeFileSync(exportPath, result);
console.log(colorize('Profile "' + profileName + '" has been written to ' + exportPath));
profile.delete();
delete currentProfiles[profileName];
});
};
return {
startProfile: startProfile,
stopProfile: stopProfile,
};
};