Browse files

Uses injected sendEvent function

  • Loading branch information...
1 parent c10d39e commit 0ba1bc81157a5146d764a14ddce3837131b0aaa8 @c4milo committed May 3, 2012
Showing with 19 additions and 16 deletions.
  1. +19 −16 lib/profiler.js
View
35 lib/profiler.js
@@ -4,19 +4,20 @@ var fs = require('fs');
var HeapProfileType = 'HEAP';
var CPUProfileType = 'CPU';
-function ProfilerAgent() {
+function ProfilerAgent(sendEvent) {
+ this.sendEvent = sendEvent;
this.profiles = {
HEAP: {},
CPU: {}
};
- this._enable = false;
+ this.enabled = false;
this.isProfilingCPU = false;
}
(function(){
this.enable = function(params, sendResult) {
- sendResult({result: this._enable});
+ sendResult({result: this.enabled});
};
this.causesRecompilation = function(params, sendResult) {
@@ -50,9 +51,10 @@ function ProfilerAgent() {
});
};
- this.takeHeapSnapshot = function(params, sendResult, sendEvent) {
+ this.takeHeapSnapshot = function(params, sendResult) {
+ var self = this;
var snapshot = profiler.takeSnapshot(function(done, total) {
- sendEvent({
+ self.sendEvent({
method: 'Profiler.reportHeapSnapshotProgress',
params:{
done: done,
@@ -63,7 +65,7 @@ function ProfilerAgent() {
this.profiles[HeapProfileType][snapshot.uid] = snapshot;
- sendEvent({
+ this.sendEvent({
method: 'Profiler.addProfileHeader',
params: {
header: {
@@ -77,14 +79,15 @@ function ProfilerAgent() {
sendResult({});
};
- this.getProfile = function(params, sendResult, sendEvent) {
+ this.getProfile = function(params, sendResult) {
+ var self = this;
if (params.type == HeapProfileType) {
var snapshot = this.profiles[params.type][params.uid];
snapshot.serialize({
onData: function(chunk, size) {
chunk = chunk + '';
- sendEvent({
+ self.sendEvent({
method: 'Profiler.addHeapSnapshotChunk',
params: {
uid: snapshot.uid,
@@ -94,7 +97,7 @@ function ProfilerAgent() {
},
onEnd: function() {
- sendEvent({
+ self.sendEvent({
method: 'Profiler.finishHeapSnapshot',
params: {uid: snapshot.uid}
});
@@ -124,22 +127,22 @@ function ProfilerAgent() {
}
};
- this.clearProfiles = function(params, sendResult, sendEvent) {
+ this.clearProfiles = function(params, sendResult) {
this.profiles.HEAP = {};
this.profiles.CPU = {};
profiler.deleteAllSnapshots();
profiler.deleteAllProfiles();
};
- this.start = function(params, sendResult, sendEvent) {
+ this.start = function(params, sendResult) {
/* TODO
{ "method":"Console.messageAdded",
"params":{"message":{"source":"javascript","level":"log","text":"Profile \"Profile 1" started.","type":"log","line":0,"url":"","repeatCount":1}}}
*/
profiler.startProfiling();
- sendEvent({
+ this.sendEvent({
method: 'Profiler.setRecordingProfile',
params: {
isProfiling: true
@@ -149,12 +152,12 @@ function ProfilerAgent() {
sendResult({});
};
- this.stop = function(params, sendResult, sendEvent) {
+ this.stop = function(params, sendResult) {
var profile = profiler.stopProfiling();
this.profiles[CPUProfileType][profile.uid] = profile;
- sendEvent({
+ this.sendEvent({
method: 'Profiler.addProfileHeader',
params: {
header: {
@@ -165,7 +168,7 @@ function ProfilerAgent() {
}
});
- sendEvent({
+ this.sendEvent({
method: 'Profiler.setRecordingProfile',
params: {
isProfiling: false
@@ -187,4 +190,4 @@ function ProfilerAgent() {
};
}).call(ProfilerAgent.prototype);
-module.exports = new ProfilerAgent();
+module.exports = ProfilerAgent;

0 comments on commit 0ba1bc8

Please sign in to comment.