Skip to content

Commit

Permalink
feature: add inspector for node 10 and heap snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
wallet77 committed Jul 20, 2018
1 parent efbcb02 commit dc61bca
Showing 1 changed file with 34 additions and 8 deletions.
42 changes: 34 additions & 8 deletions lib/Daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var fs = require('fs');
var God = require('./God');
var async = require('async');
var fmt = require('./tools/fmt.js');
var semver = require('semver');

var Daemon = module.exports = function(opts) {
if (!opts) opts = {};
Expand Down Expand Up @@ -153,7 +154,7 @@ Daemon.prototype.innerStart = function(cb) {
var profiler;

try {
profiler = require('v8-profiler-node8');
profiler = semver.satisfies(process.version, '<= 10.0.0') ? require('v8-profiler-node8') : require('inspector');
} catch(e) {
profiler = null;
}
Expand All @@ -167,14 +168,39 @@ Daemon.prototype.innerStart = function(cb) {
return cb(new Error('v8-profiler is not available'));
}

var snapshot1 = profiler.takeSnapshot();
var path = require('path');
snapshot1.export(function(error, result) {
fs.writeFile(msg.pwd, result, function() {
snapshot1.delete();
return cb(null, {file : msg.pwd});
if (semver.satisfies(process.version, '>= 10.0.0')) {
const session = new profiler.Session()
session.connect()
session.post('HeapProfiler.enable')

const chunks = []
session.on('HeapProfiler.addHeapSnapshotChunk', (data) => {
chunks.push(data.params.chunk)
})

session.post('HeapProfiler.takeHeapSnapshot', {
reportProgress: false
}, (err, data) => {
if (err) return cb(err)

fs.writeFile(msg.pwd, chunks.join(''), function() {
session.post('Profiler.disable')
session.disconnect()

return cb(null, {file : msg.pwd});
});
})

} else {
var snapshot1 = profiler.takeSnapshot();
var path = require('path');
snapshot1.export(function(error, result) {
fs.writeFile(msg.pwd, result, function() {
snapshot1.delete();
return cb(null, {file : msg.pwd});
});
});
});
}
}

function startProfilingPM2(msg, cb) {
Expand Down

0 comments on commit dc61bca

Please sign in to comment.