Skip to content

Commit 512f600

Browse files
committed
feat(profiler): correlate commands with profiles
1 parent ef04851 commit 512f600

File tree

6 files changed

+29
-19
lines changed

6 files changed

+29
-19
lines changed

lib/agent/control/control.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Control.prototype.getUpdates = function () {
3232

3333
result.commands = result.commands || []
3434
result.commands.forEach(function (command) {
35-
_this.controlBus.emit(command.command, command.data)
35+
_this.controlBus.emit(command.command, command)
3636
})
3737
})
3838
}

lib/agent/control/control.spec.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ var Timer = require('../timer')
55

66
describe('The Control module', function () {
77
it('gets memory-heapdump', function () {
8+
var command = {
9+
id: 123,
10+
command: 'memory-heapdump'
11+
}
812
var collectorApi = {
913
getUpdates: function (options, cb) {
1014
cb(null, {
11-
commands: [{
12-
id: 123,
13-
command: 'memory-heapdump'
14-
}]
15+
commands: [command]
1516
})
1617
}
1718
}
@@ -29,7 +30,7 @@ describe('The Control module', function () {
2930
}
3031
})
3132
profiler.getUpdates()
32-
expect(controlBus.emit).to.be.calledWith('memory-heapdump', undefined)
33+
expect(controlBus.emit).to.be.calledWith('memory-heapdump', command)
3334
})
3435
it('should have a timer', function () {
3536
var collectorApi = {

lib/agent/profiler/cpu/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ function CpuProfiler (options) {
99
this.controlBus = options.controlBus
1010
this.profileWindow = 10000
1111

12-
this.controlBus.on('cpu-profile', function (data) {
13-
_this.sendProfile()
12+
this.controlBus.on('cpu-profile', function (command) {
13+
_this.sendProfile(command)
1414
})
1515
}
1616

17-
CpuProfiler.prototype.sendProfile = function () {
17+
CpuProfiler.prototype.sendProfile = function (command) {
1818
var _this = this
1919

2020
v8profiler.startProfiling('trace-cpu-profile', true)
@@ -30,7 +30,8 @@ CpuProfiler.prototype.sendProfile = function () {
3030

3131
_this.collectorApi.sendCpuProfile({
3232
cpuProfile: result,
33-
time: Date.now()
33+
time: Date.now(),
34+
commandId: command.id
3435
})
3536
profile.delete()
3637
})

lib/agent/profiler/cpu/index.spec.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ describe('The Cpu Profiler module', function () {
1717
},
1818
delete: this.sandbox.spy()
1919
}
20+
var command = {
21+
id: 42
22+
}
2023

2124
var startProfilingStub = this.sandbox.stub(v8profiler, 'startProfiling', function () {})
2225
var stopProfilingStub = this.sandbox.stub(v8profiler, 'stopProfiling', function () {
@@ -36,14 +39,15 @@ describe('The Cpu Profiler module', function () {
3639
return now
3740
})
3841

39-
profiler.sendProfile()
42+
profiler.sendProfile(command)
4043

4144
setTimeout(function () {
4245
expect(startProfilingStub).to.be.calledWith('trace-cpu-profile', true)
4346
expect(stopProfilingStub).to.be.calledWith('trace-cpu-profile')
4447
expect(collectorApi.sendCpuProfile).to.be.calledWith({
4548
cpuProfile: profileData,
46-
time: now
49+
time: now,
50+
commandId: command.id
4751
})
4852
expect(profile.delete).to.be.called
4953
done()

lib/agent/profiler/memory/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ function MemoryProfiler (options) {
88
this.collectorApi = options.collectorApi
99
this.controlBus = options.controlBus
1010

11-
this.controlBus.on('memory-heapdump', function (data) {
12-
_this.sendSnapshot()
11+
this.controlBus.on('memory-heapdump', function (command) {
12+
_this.sendSnapshot(command)
1313
})
1414
}
1515

16-
MemoryProfiler.prototype.sendSnapshot = function () {
16+
MemoryProfiler.prototype.sendSnapshot = function (command) {
1717
var _this = this
1818
var now = Date.now()
1919
var snapshot = v8profiler.takeSnapshot()
@@ -25,7 +25,8 @@ MemoryProfiler.prototype.sendSnapshot = function () {
2525

2626
_this.collectorApi.sendMemorySnapshot({
2727
heapSnapshot: result,
28-
time: now
28+
time: now,
29+
commandId: command.id
2930
})
3031

3132
snapshot.delete()

lib/agent/profiler/memory/index.spec.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ describe('The Memory Profiler module', function () {
1414
var deleteSnapshotSpy = this.sandbox.spy()
1515
var snapshotContent = 'snapshot'
1616
var now = 1234
17-
17+
var command = {
18+
id: 42
19+
}
1820
var profiler = MemoryProfiler.create({
1921
collectorApi: collectorApi,
2022
controlBus: msgBus
@@ -33,11 +35,12 @@ describe('The Memory Profiler module', function () {
3335
return now
3436
})
3537

36-
profiler.sendSnapshot()
38+
profiler.sendSnapshot(command)
3739

3840
expect(collectorApi.sendMemorySnapshot).to.be.calledWith({
3941
heapSnapshot: snapshotContent,
40-
time: now
42+
time: now,
43+
commandId: command.id
4144
})
4245
expect(deleteSnapshotSpy).to.be.called
4346
})

0 commit comments

Comments
 (0)