Skip to content

Commit

Permalink
Merge pull request #241 from macbre/milestone-events
Browse files Browse the repository at this point in the history
Emit events via npm module for certain page loading milestones
  • Loading branch information
macbre committed Feb 27, 2014
2 parents 3ff362e + ba3add3 commit 653482e
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 15 deletions.
4 changes: 2 additions & 2 deletions core/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ function ipc(event) {
this.event = event;
}

ipc.prototype.push = function(data) {
ipc.prototype.push = function() {
stderr.writeLine(JSON.stringify({
event: this.event,
data: data
data: Array.prototype.slice.apply(arguments)
}));
};

Expand Down
6 changes: 3 additions & 3 deletions core/modules/requestsMonitor/requestsMonitor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Simple HTTP requests monitor and analyzer
*/
exports.version = '1.1';
exports.version = '1.2';

exports.module = function(phantomas) {
// imports
Expand Down Expand Up @@ -280,8 +280,8 @@ exports.module = function(phantomas) {
phantomas.on('recv', function(entry, res) {
// check the first response which is not a redirect (issue #74)
if (!ttfbMeasured && !entry.isRedirect) {
phantomas.setMetric('timeToFirstByte', entry.timeToFirstByte);
phantomas.setMetric('timeToLastByte', entry.timeToLastByte);
phantomas.setMetric('timeToFirstByte', entry.timeToFirstByte, true);
phantomas.setMetric('timeToLastByte', entry.timeToLastByte, true);

ttfbMeasured = true;

Expand Down
17 changes: 12 additions & 5 deletions core/phantomas.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ phantomas.prototype = {
break;

case 'setMetric':
this.setMetric(data.name, data.value);
this.setMetric(data.name, data.value, data.isFinal);
break;

case 'incrMetric':
Expand All @@ -718,13 +718,20 @@ phantomas.prototype = {
},

// metrics reporting
setMetric: function(name, value) {
setMetric: function(name, value, isFinal) {
var ipc = new (require('./ipc'))('metric');

value = typeof value === 'string' ? value : (value || 0); // set to zero if undefined / null is provided
this.results.setMetric(name, value);

// trigger an event when the metric value is said to be final (isse #240)
if (isFinal === true) {
ipc.push(name, value);
}
},

setMetricEvaluate: function(name, fn) {
this.setMetric(name, this.page.evaluate(fn));
this.setMetric(name, this.page.evaluate(fn), true /* isFinal */);
},

setMarkerMetric: function(name) {
Expand All @@ -735,7 +742,7 @@ phantomas.prototype = {
throw 'setMarkerMetric() called before responseEnd event!';
}

this.results.setMetric(name, value);
this.setMetric(name, value, true /* isFinal */);
return value;
},

Expand All @@ -746,7 +753,7 @@ phantomas.prototype = {
// @ee https://github.com/ariya/phantomjs/wiki/API-Reference-WebPage#evaluatefunction-arg1-arg2--object
this.setMetric(name, this.page.evaluate(function(key) {
return window.__phantomas.get(key) || 0;
}, key));
}, key), true /* isFinal */);
},

// get a value set using window.__phantomas browser scope
Expand Down
4 changes: 2 additions & 2 deletions core/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@
sendMsg('log', msg);
}

function setMetric(name, value) {
sendMsg('setMetric', {name: name, value: (typeof value !== 'undefined') ? value : 0});
function setMetric(name, value, isFinal) {
sendMsg('setMetric', {name: name, value: (typeof value !== 'undefined') ? value : 0, isFinal: isFinal === true});
}

function incrMetric(name, incr /* =1 */) {
Expand Down
36 changes: 36 additions & 0 deletions examples/npm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env node

/**
* Example script that uses phantomas npm module
*/
var phantomas = require('../'),
run;

console.log('phantomas v%s loaded from %s', phantomas.version, phantomas.path);

run = phantomas('http://google.is', {
'analyze-css': true,
'assert-requests': 1
});

console.log('Running phantomas: pid %d', run.pid);

// errors handling
run.on('error', function(code) {
console.log('Exit code #%d', code);
});

// handle results
run.on('results', function(results) {
console.log('Number of requests: %d', results.getMetric('requests'));
console.log('Failed asserts: %j', results.getFailedAsserts());
});

// events handling
run.on('progress', function(progress) {
console.log('Loading progress: %d%', progress);
});

run.on('milestone', function(milestone, timing) {
console.log('%s at %d ms', milestone, timing);
});
15 changes: 15 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,21 @@ function phantomas(url, options, callback) {
events.emit('progress', progress);
});

// handle page loading milestones (#240)
var milestoneDebug = require('debug')('phantomas:milestone');

ipc.on('metric', function(metric, value) {
switch(metric) {
case 'timeToFirstByte':
case 'timeToLastByte':
case 'onDOMReadyTime':
case 'windowOnLoadTime':
milestoneDebug('%s: %d ms', metric, value);
events.emit('milestone', metric, value);
break;
}
});

// process results
proc.on('close', function(code) {
var debug = require('debug')('phantomas:results'),
Expand Down
8 changes: 6 additions & 2 deletions lib/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ ipc.prototype.init = function() {
messages.forEach(function(msg) {
if (msg === '') return;

debug('%s', msg);
msg = JSON.parse(msg);
debug('%s: %j', msg.event, msg.data);

self.events.emit(msg.event, msg.data);
// send event name and the rest of the data
var args = msg.data;
args.unshift(msg.event);

self.events.emit.apply(self.events, args);
});
});
};
Expand Down
5 changes: 4 additions & 1 deletion test/public-api-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ mockery.registerMock('fs', {
list: function() {}
});
mockery.registerMock('system', {
os: {}
os: {},
stderr: {
writeLine: function() {}
}
});
mockery.registerMock('webpage', {
create: function() {
Expand Down

0 comments on commit 653482e

Please sign in to comment.