Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(response-compression): add transferSize sanity check #3606

Merged
merged 3 commits into from
Oct 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class ResponsesAreCompressed extends ByteEfficiencyAudit {
// we require at least 10% savings off the original size AND at least 1400 bytes
// if the savings is smaller than either, we don't care
if (1 - gzipSize / originalSize < IGNORE_THRESHOLD_IN_PERCENT ||
gzipSavings < IGNORE_THRESHOLD_IN_BYTES
gzipSavings < IGNORE_THRESHOLD_IN_BYTES ||
record.transferSize < gzipSize

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be a <= ?
If transferSize is exactly equal to the gzipSize then the resource should still be ignored, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, if we want to get nitpicky about it though it should really just be

const gzipSavings = Math.min(originalSize - gzipSize, record.transferSize - gzipSize)

above but realistically this is just a sanity check that shouldn't be hit given the new mitigation in the gatherer

feel free to open a PR, we're always open to contributions :)

) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ResponseCompression extends Gatherer {
const isChromeExtensionResource = record.url.startsWith(CHROME_EXTENSION_PROTOCOL);

if (!isTextBasedResource || !record.resourceSize || !record.finished ||
isChromeExtensionResource) {
isChromeExtensionResource || !record.transferSize || record.statusCode === 304) {
return;
}

Expand All @@ -43,6 +43,7 @@ class ResponseCompression extends Gatherer {
requestId: record.requestId,
url: record.url,
mimeType: record.mimeType,
transferSize: record.transferSize,
resourceSize: record.resourceSize,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
*/
'use strict';

const KB_BYTES = 1024;
const KB = 1024;
const ResponsesAreCompressedAudit =
require('../../../audits/byte-efficiency/uses-request-compression.js');
const assert = require('assert');

function generateResponse(filename, type, originalSize, gzipSize) {
return {
url: `http://google.com/${filename}`,
mimeType: `${type}`,
resourceSize: originalSize,
gzipSize,
};
function generateResponse(options) {
return Object.assign({
url: `http://google.com/${options.file}`,
transferSize: options.resourceSize || 0,
resourceSize: 0,
gzipSize: 0,
}, options);
}

/* eslint-env mocha */
Expand All @@ -25,9 +25,9 @@ describe('Page uses optimized responses', () => {
it('fails when responses are collectively unoptimized', () => {
const auditResult = ResponsesAreCompressedAudit.audit_({
ResponseCompression: [
generateResponse('index.js', 'text/javascript', 100 * KB_BYTES, 90 * KB_BYTES), // 10kb & 10%
generateResponse('index.css', 'text/css', 50 * KB_BYTES, 37 * KB_BYTES), // 13kb & 26% (hit)
generateResponse('index.json', 'application/json', 2048 * KB_BYTES, 1024 * KB_BYTES), // 1024kb & 50% (hit)
generateResponse({file: 'index.js', resourceSize: 100 * KB, gzipSize: 90 * KB}), // 10kb & 10%
generateResponse({file: 'index.css', resourceSize: 50 * KB, gzipSize: 37 * KB}), // 13kb & 26% (hit)
generateResponse({file: 'index.json', resourceSize: 2048 * KB, gzipSize: 1024 * KB}), // 1024kb & 50% (hit)
],
});

Expand All @@ -37,9 +37,11 @@ describe('Page uses optimized responses', () => {
it('passes when all responses are sufficiently optimized', () => {
const auditResult = ResponsesAreCompressedAudit.audit_({
ResponseCompression: [
generateResponse('index.js', 'text/javascript', 1000 * KB_BYTES, 910 * KB_BYTES), // 90kb & 9%
generateResponse('index.css', 'text/css', 6 * KB_BYTES, 4.5 * KB_BYTES), // 1,5kb & 25% (hit)
generateResponse('index.json', 'application/json', 10 * KB_BYTES, 10 * KB_BYTES), // 0kb & 0%
generateResponse({file: 'index.js', resourceSize: 1000 * KB, gzipSize: 910 * KB}), // 90kb & 9%
generateResponse({file: 'index.css', resourceSize: 6 * KB, gzipSize: 4.5 * KB}), // 1,5kb & 25% (hit)
generateResponse({file: 'index.json', resourceSize: 10 * KB, gzipSize: 10 * KB}), // 0kb & 0%
generateResponse({file: 'compressed.json', resourceSize: 10 * KB, transferSize: 3 * KB,
gzipSize: 6 * KB}), // 0kb & 0%
],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ const traceData = {
networkRecords: [
{
_url: 'http://google.com/index.js',
_statusCode: 200,
_mimeType: 'text/javascript',
_requestId: 0,
_resourceSize: 9,
_transferSize: 10,
_resourceType: {
_isTextType: true,
},
Expand All @@ -33,9 +35,11 @@ const traceData = {
},
{
_url: 'http://google.com/index.css',
_statusCode: 200,
_mimeType: 'text/css',
_requestId: 1,
_resourceSize: 6,
_transferSize: 7,
_resourceType: {
_isTextType: true,
},
Expand All @@ -45,9 +49,25 @@ const traceData = {
},
{
_url: 'http://google.com/index.json',
_statusCode: 200,
_mimeType: 'application/json',
_requestId: 2,
_resourceSize: 7,
_transferSize: 8,
_resourceType: {
_isTextType: true,
},
_responseHeaders: [],
content: '1234567',
finished: true,
},
{
_url: 'http://google.com/index.json',
_statusCode: 304, // ignore for being a cache not modified response
_mimeType: 'application/json',
_requestId: 2,
_resourceSize: 7,
_transferSize: 7,
_resourceType: {
_isTextType: true,
},
Expand All @@ -57,9 +77,11 @@ const traceData = {
},
{
_url: 'http://google.com/other.json',
_statusCode: 200,
_mimeType: 'application/json',
_requestId: 2,
_resourceSize: 7,
_transferSize: 8,
_resourceType: {
_isTextType: true,
},
Expand All @@ -69,9 +91,11 @@ const traceData = {
},
{
_url: 'http://google.com/index.jpg',
_statusCode: 200,
_mimeType: 'images/jpg',
_requestId: 3,
_resourceSize: 10,
_transferSize: 10,
_resourceType: {
_isTextType: false,
},
Expand Down Expand Up @@ -124,6 +148,7 @@ describe('Optimized responses', () => {
_mimeType: 'text/css',
_requestId: 1,
_resourceSize: 10,
_transferSize: 10,
_resourceType: {
_isTextType: true,
},
Expand All @@ -136,6 +161,7 @@ describe('Optimized responses', () => {
_mimeType: 'text/css',
_requestId: 1,
_resourceSize: 123,
_transferSize: 123,
_resourceType: {
_isTextType: true,
},
Expand All @@ -157,8 +183,10 @@ describe('Optimized responses', () => {
function createNetworkRequests(traceData) {
traceData.networkRecords = traceData.networkRecords.map(record => {
record.url = record._url;
record.statusCode = record._statusCode;
record.mimeType = record._mimeType;
record.resourceSize = record._resourceSize;
record.transferSize = record._transferSize;
record.responseHeaders = record._responseHeaders;
record.requestId = record._requestId;
record.resourceType = () => {
Expand Down