Skip to content

Commit

Permalink
timeToFirstByte: fix #298
Browse files Browse the repository at this point in the history
* move to a separate core module
  • Loading branch information
macbre committed May 10, 2014
1 parent 91de7ae commit a7e1780
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 23 deletions.
30 changes: 7 additions & 23 deletions core/modules/requestsMonitor/requestsMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ exports.module = function(phantomas) {
phantomas.setMetric('postRequests'); // @desc number of POST requests
phantomas.setMetric('httpsRequests'); // @desc number of HTTPS requests
phantomas.setMetric('notFound'); // @desc number of HTTP 404 responses
phantomas.setMetric('timeToFirstByte'); // @desc time it took to receive the first byte of the first response (that was not a redirect)
phantomas.setMetric('timeToLastByte'); // @desc time it took to receive the last byte of the first response (that was not a redirect)
phantomas.setMetric('bodySize'); // @desc size of the uncompressed content of all responses @unreliable
phantomas.setMetric('contentLength'); // @desc size of the content of all responses (based on Content-Length header) @unreliable
phantomas.setMetric('httpTrafficCompleted'); // @desc time it took to receive the last byte of the last HTTP response
Expand Down Expand Up @@ -48,12 +46,6 @@ exports.module = function(phantomas) {
}
}

// when the monitoring started?
var loadStartedTime;
phantomas.on('loadStarted', function(res) {
loadStartedTime = Date.now();
});

phantomas.on('onResourceRequested', function(res, request) {
// store current request data
var entry = requests[res.id] = {
Expand Down Expand Up @@ -300,22 +292,14 @@ exports.module = function(phantomas) {
}
});

// TTFB / TTLB metrics
var ttfbMeasured = false;

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, true);
phantomas.setMetric('timeToLastByte', entry.timeToLastByte, true);

ttfbMeasured = true;

phantomas.log('Time to first byte: set to %d ms for <%s> (HTTP %d)', entry.timeToFirstByte, entry.url, entry.status);
phantomas.emit('responseEnd', entry, res); // @desc the first response (that was not a redirect) fully received
}
// completion of the last HTTP request
var loadStartedTime;
phantomas.on('loadStarted', function(res) {
// when the monitoring started?
loadStartedTime = Date.now();
});

// completion of the last HTTP request
phantomas.on('recv', function(entry, res) {
phantomas.setMetric('httpTrafficCompleted', entry.recvEndTime - loadStartedTime);
});
};
42 changes: 42 additions & 0 deletions core/modules/timeToFirstByte/timeToFirstByte.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* TTFB / TTLB metrics
*/
'use strict';

exports.version = '1.1';

exports.module = function(phantomas) {
var measured = false,
reqId = 1; // request ID to consider when calculating TTFB / TTLB

phantomas.setMetric('timeToFirstByte'); // @desc time it took to receive the first byte of the first response (that was not a redirect)
phantomas.setMetric('timeToLastByte'); // @desc time it took to receive the last byte of the first response (that was not a redirect)

phantomas.on('recv', function(entry, res) {
// metrics already calculated
if (measured) {
return;
}

if (entry.isRedirect) {
// wait for the next request
reqId = entry.id + 1;

phantomas.log('Time to first byte: response #%d <%s> is a redirect (waiting for response #%d)', entry.id, entry.url, reqId);
return;
}

// check the first response which is not a redirect (issue #74)
if (entry.id === reqId) {
phantomas.setMetric('timeToFirstByte', entry.timeToFirstByte, true);
phantomas.setMetric('timeToLastByte', entry.timeToLastByte, true);

measured = true;

phantomas.log('Time to first byte: set to %d ms for #%d request to <%s> (HTTP %d)', entry.timeToFirstByte, entry.id, entry.url, entry.status);
phantomas.log('Time to last byte: set to %d ms', entry.timeToLastByte);

phantomas.emit('responseEnd', entry, res); // @desc the first response (that was not a redirect) fully received
}
});
};

0 comments on commit a7e1780

Please sign in to comment.