Skip to content

Commit d977835

Browse files
authored
fix(profiling): use the same v8-profiler module for memory (#49)
1 parent 8fed6cd commit d977835

6 files changed

Lines changed: 27 additions & 50 deletions

File tree

lib/agent/profiler/cpu/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ var debug = require('debug')('risingstack/trace')
22

33
var v8profiler = require('../../../optionalDependencies/v8-profiler')
44

5-
65
function CpuProfiler (options) {
76
var _this = this
87
this.collectorApi = options.collectorApi
@@ -23,7 +22,7 @@ CpuProfiler.prototype.sendProfile = function () {
2322
var profile = v8profiler.stopProfiling('trace-cpu-profile')
2423

2524
// if the v8-profiler cannot be compiled, we won't have a profile
26-
profile && profile.export(function(err, result) {
25+
profile && profile.export(function (err, result) {
2726
if (err) {
2827
return debug(err)
2928
}
@@ -33,7 +32,7 @@ CpuProfiler.prototype.sendProfile = function () {
3332
time: Date.now()
3433
})
3534
profile.delete()
36-
});
35+
})
3736
}, this.profileWindow)
3837
}
3938

lib/agent/profiler/memory/index.js

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
var os = require('os')
2-
var fs = require('fs')
3-
41
var debug = require('debug')('risingstack/trace')
52

6-
var heapdump = require('../../../optionalDependencies/heapdump')
3+
var v8profiler = require('../../../optionalDependencies/v8-profiler')
74

85
function MemoryProfiler (options) {
96
var _this = this
@@ -17,25 +14,20 @@ function MemoryProfiler (options) {
1714

1815
MemoryProfiler.prototype.sendSnapshot = function () {
1916
var _this = this
20-
var tempdir = os.tmpdir()
2117
var now = Date.now()
22-
var heapSnapshotName = now + '.heapsnapshot'
23-
var snapshotPath = tempdir + '/' + heapSnapshotName
24-
heapdump.writeSnapshot(snapshotPath, function (err) {
25-
if (err) {
26-
return debug('Error writing the heapsnapshot', err)
27-
}
18+
var snapshot = v8profiler.takeSnapshot()
2819

29-
fs.readFile(snapshotPath, 'utf-8', function (err, data) {
30-
if (err) {
31-
return debug('Error reading the heapsnapshot', err)
32-
}
20+
snapshot && snapshot.export(function (error, result) {
21+
if (error) {
22+
return debug(error)
23+
}
3324

34-
_this.collectorApi.sendMemorySnapshot({
35-
heapSnapshot: data,
36-
time: now
37-
})
25+
_this.collectorApi.sendMemorySnapshot({
26+
heapSnapshot: result,
27+
time: now
3828
})
29+
30+
snapshot.delete()
3931
})
4032
}
4133

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
var expect = require('chai').expect
2-
var os = require('os')
3-
var fs = require('fs')
42
var EventEmitter = require('events').EventEmitter
53

6-
var heapdump = require('../../../optionalDependencies/heapdump')
4+
var v8profiler = require('../../../optionalDependencies/v8-profiler')
75

86
var MemoryProfiler = require('./')
97

@@ -13,32 +11,34 @@ describe('The Memory Profiler module', function () {
1311
var collectorApi = {
1412
sendMemorySnapshot: this.sandbox.spy()
1513
}
16-
17-
var snapShotContent = 'very-snapshot'
14+
var deleteSnapshotSpy = this.sandbox.spy()
15+
var snapshotContent = 'snapshot'
1816
var now = 1234
1917

2018
var profiler = MemoryProfiler.create({
2119
collectorApi: collectorApi,
2220
controlBus: msgBus
2321
})
2422

25-
this.sandbox.stub(heapdump, 'writeSnapshot', function (path, cb) {
26-
cb()
23+
this.sandbox.stub(v8profiler, 'takeSnapshot', function () {
24+
return {
25+
export: function (cb) {
26+
cb(null, snapshotContent)
27+
},
28+
delete: deleteSnapshotSpy
29+
}
2730
})
2831

2932
this.sandbox.stub(Date, 'now', function () {
3033
return now
3134
})
3235

33-
this.sandbox.stub(fs, 'readFile', function (path, encoding, cb) {
34-
cb(null, snapShotContent)
35-
})
36-
3736
profiler.sendSnapshot()
3837

3938
expect(collectorApi.sendMemorySnapshot).to.be.calledWith({
40-
heapSnapshot: snapShotContent,
39+
heapSnapshot: snapshotContent,
4140
time: now
4241
})
42+
expect(deleteSnapshotSpy).to.be.called
4343
})
4444
})

lib/optionalDependencies/heapdump.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/optionalDependencies/v8-profiler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ try {
66
console.error('error: [trace]', 'v8-profiler couldn\'t be required, possibly a compiler issue - continuing')
77
v8profiler = {
88
startProfiling: function () {},
9-
stopProfiling: function () {}
9+
stopProfiling: function () {},
10+
takeSnapshot: function () {}
1011
}
1112
}
1213

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
"optionalDependencies": {
5858
"microtime": "2.1.1",
5959
"event-loop-stats": "1.0.0",
60-
"heapdump": "0.3.7",
6160
"gc-stats": "1.0.0",
6261
"v8-profiler": "5.6.5"
6362
},

0 commit comments

Comments
 (0)