Skip to content

Commit

Permalink
Merge pull request #3811 from Unitech/memory_inspector
Browse files Browse the repository at this point in the history
feature: add inspector for node 10 and heap snapshot
  • Loading branch information
wallet77 authored Jul 23, 2018
2 parents ebffb60 + d064750 commit 6201804
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 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('inspector') : null;
} catch(e) {
profiler = null;
}
Expand All @@ -163,18 +164,31 @@ Daemon.prototype.innerStart = function(cb) {
*/
function snapshotPM2(msg, cb) {
if (profiler == null) {
console.log('v8-profiler is not available');
return cb(new Error('v8-profiler is not available'));
console.log('Heap snapshot is not available (node 10+)');
return cb(new Error('Heap snapshot is not available (node 10+)'));
}

var snapshot1 = profiler.takeSnapshot();
var path = require('path');
snapshot1.export(function(error, result) {
fs.writeFile(msg.pwd, result, function() {
snapshot1.delete();
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});
});
});
})
}

function startProfilingPM2(msg, cb) {
Expand Down

0 comments on commit 6201804

Please sign in to comment.