Skip to content

Commit

Permalink
Merge pull request #277 from macbre/contentLength-fixes
Browse files Browse the repository at this point in the history
Content Length fixes
  • Loading branch information
macbre committed Apr 4, 2014
2 parents b3fe15d + 97c7ee7 commit 7807334
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
19 changes: 16 additions & 3 deletions core/modules/requestsMonitor/requestsMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ exports.module = function(phantomas) {

// FIXME: buggy
// @see https://github.com/ariya/phantomjs/issues/10169
entry.bodySize += res.bodySize || 0;
entry.bodySize = res.bodySize || 0;
break;

// the end of response
Expand Down Expand Up @@ -157,6 +157,12 @@ exports.module = function(phantomas) {
// because: https://github.com/ariya/phantomjs/issues/10156
case 'content-length':
entry.contentLength = parseInt(header.value, 10);

/**
if (entry.bodySize !== entry.contentLength) {
phantomas.log('%s: %j', 'bodySize vs contentLength', {url: entry.url, bodySize: entry.bodySize, contentLength: entry.contentLength});
}
**/
break;

// detect content type
Expand Down Expand Up @@ -254,12 +260,19 @@ exports.module = function(phantomas) {
break;
}

// default value (if Content-Length header is not present in the response or it's base64-encoded)
// see issue #137
if (typeof entry.contentLength === 'undefined') {
entry.contentLength = entry.bodySize;
phantomas.log('%s: %j', 'contentLength missing', {url: entry.url, bodySize: entry.bodySize});
}

// requests stats
if (!entry.isBase64) {
phantomas.incrMetric('requests');

phantomas.incrMetric('bodySize', entry.bodySize); // content only
phantomas.incrMetric('contentLength', entry.contentLength || entry.bodySize); // content only
phantomas.incrMetric('bodySize', entry.bodySize);
phantomas.incrMetric('contentLength', entry.contentLength);
}

if (entry.gzip) {
Expand Down
8 changes: 5 additions & 3 deletions modules/assetsTypes/assetsTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ exports.module = function(phantomas) {
});

phantomas.on('recv', function(entry, res) {
var size = entry.contentLength;

phantomas.incrMetric(entry.type + 'Count');
phantomas.incrMetric(entry.type + 'Size', entry.bodySize);
phantomas.incrMetric(entry.type + 'Size', size);

phantomas.addOffender(entry.type + 'Count', entry.url + ' (' + (entry.bodySize / 1024).toFixed(2) + ' kB)');
phantomas.addOffender(entry.type + 'Count', entry.url + ' (' + (size / 1024).toFixed(2) + ' kB)');
});

phantomas.on('base64recv', function(entry, res) {
phantomas.incrMetric('base64Count');
phantomas.incrMetric('base64Size', entry.bodySize);
phantomas.incrMetric('base64Size', entry.contentLength);
});
};
6 changes: 3 additions & 3 deletions modules/har/har.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function createHAR(page, creator) {
request: {
// Accurate bodySize blocked on https://github.com/ariya/phantomjs/pull/11484
// bodySize: -1,
bodySize: startReply.bodySize,
bodySize: startReply.contentLength,
cookies: [],
headers: request.headers,
// Accurate headersSize blocked on https://github.com/ariya/phantomjs/pull/11484
Expand All @@ -91,7 +91,7 @@ function createHAR(page, creator) {
url: request.url,
},
response: {
bodySize: startReply.bodySize,
bodySize: startReply.contentLength,
cookies: [],
headers: endReply.headers,
headersSize: -1,
Expand All @@ -101,7 +101,7 @@ function createHAR(page, creator) {
statusText: endReply.statusText,
content: {
mimeType: endReply.contentType || '',
size: startReply.bodySize,
size: startReply.bodySize, // uncompressed
text: startReply.content || ''
}
},
Expand Down
8 changes: 4 additions & 4 deletions modules/requestsStats/requestsStats.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ exports.module = function(phantomas) {

// size
pushToStack('smallestResponse', entry, function(stack, entry) {
return stack.bodySize > entry.bodySize;
return stack.contentLength > entry.contentLength;
});

pushToStack('biggestResponse', entry, function(stack, entry) {
return stack.bodySize < entry.bodySize;
return stack.contentLength < entry.contentLength;
});

// time (from sent to last byte)
Expand Down Expand Up @@ -98,8 +98,8 @@ exports.module = function(phantomas) {
switch (metric) {
case 'smallestResponse':
case 'biggestResponse':
phantomas.setMetric(metric, entry.bodySize);
details = (entry.bodySize/1024).toFixed(2) + ' kB';
phantomas.setMetric(metric, entry.contentLength);
details = (entry.contentLength/1024).toFixed(2) + ' kB';
break;

case 'fastestResponse':
Expand Down
4 changes: 2 additions & 2 deletions modules/staticAssets/staticAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ exports.module = function(phantomas) {

// check small images that can be base64 encoded
if (entry.isImage) {
if (entry.bodySize < BASE64_SIZE_THRESHOLD) {
if (entry.contentLength < BASE64_SIZE_THRESHOLD) {
phantomas.incrMetric('smallImages');
phantomas.addOffender('smallImages', entry.url + ' (' + (entry.bodySize/1024).toFixed(2) + ' kB)');
phantomas.addOffender('smallImages', entry.url + ' (' + (entry.contentLength/1024).toFixed(2) + ' kB)');
}
}

Expand Down
6 changes: 3 additions & 3 deletions test/modules/requestsStats-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ vows.describe('requestsStats').addBatch({
var requests = [
{
status: 200,
bodySize: 25,
contentLength: 25,
timeToFirstByte: 3,
timeToLastByte: 5,
},
{
status: 200,
bodySize: 2245,
contentLength: 2245,
timeToFirstByte: 1,
timeToLastByte: 11,
},
{
status: 200,
bodySize: 205,
contentLength: 205,
timeToFirstByte: 2,
timeToLastByte: 2,
}
Expand Down

0 comments on commit 7807334

Please sign in to comment.