-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
36 lines (32 loc) · 1.07 KB
/
index.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
const path = require('path');
const memwatch = require('node-memwatch');
const heapProfile = require('heap-profile');
const heapDump = require('heapdump');
module.exports = function(dir = __dirname) {
memwatch.on('leak', function(info) {
console.error('memory leak detected!');
console.table(info);
writeProfile(dir, 'leak');
if (process.env.NODE_ENV === 'dev') writeSnapshot(dir, 'leak');
});
heapProfile.start();
if (process.env.NODE_ENV === 'dev') writeSnapshot(dir, 'init');
};
const writeProfile = function(dir, type) {
heapProfile.write(getPath(dir, 'heapprofile', type), function(err, filename) {
if (err) return console.error(err);
console.info('heapProfile written to', filename);
});
};
const writeSnapshot = function(dir, type) {
heapDump.writeSnapshot(getPath(dir, 'heapsnapshot', type), function(
err,
filename
) {
if (err) return console.error(err);
console.info('heapSnapshot written to', filename);
});
};
const getPath = function(dir, type, info) {
return `${dir}${path.sep}${type}-${Date.now()}-${info}.${type}`;
};