Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Emit events via npm module for certain page loading milestones #241

Merged
merged 8 commits into from

1 participant

Commits on Feb 26, 2014
  1. Handle "milestone" event in npm module

    authored
    Bind to IPC event and propagate it via npm module
Commits on Feb 27, 2014
  1. Typo

    authored
This page is out of date. Refresh to see the latest.
View
4 core/ipc.js
@@ -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)
}));
};
View
6 core/modules/requestsMonitor/requestsMonitor.js
@@ -1,7 +1,7 @@
/**
* Simple HTTP requests monitor and analyzer
*/
-exports.version = '1.1';
+exports.version = '1.2';
exports.module = function(phantomas) {
// imports
@@ -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;
View
17 core/phantomas.js
@@ -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':
@@ -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) {
@@ -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;
},
@@ -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
View
4 core/scope.js
@@ -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 */) {
View
36 examples/npm.js
@@ -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);
+});
View
15 lib/index.js
@@ -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'),
View
8 lib/ipc.js
@@ -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);
});
});
};
View
5 test/public-api-test.js
@@ -14,7 +14,10 @@ mockery.registerMock('fs', {
list: function() {}
});
mockery.registerMock('system', {
- os: {}
+ os: {},
+ stderr: {
+ writeLine: function() {}
+ }
});
mockery.registerMock('webpage', {
create: function() {
Something went wrong with that request. Please try again.