Skip to content

Commit

Permalink
Added audit for request compression (gzip & br) (#1513)
Browse files Browse the repository at this point in the history
* Added audit for request compression (gzip & br)

* Review fixes ^^

* Review updates

- Moved to wastedBytes approach and wastedTime

* Added unit tests

* Added toLocalString for better number formatting

* Add zlib to do gzip compression

* Rebased and use byte-efficiency audit

* Review changes

* Remove duplicates

* Change waste threshold to 10kb

* Review changes

* Ignore pako inflate from browserify

* Put debug flag back on browserify

* Update nit on web-inspector

* Rebase stuff
  • Loading branch information
wardpeet committed Apr 14, 2017
1 parent bd60d6d commit ba01e2a
Show file tree
Hide file tree
Showing 11 changed files with 857 additions and 7 deletions.
105 changes: 105 additions & 0 deletions lighthouse-core/audits/byte-efficiency/uses-request-compression.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* @license
* Copyright 2017 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* @fileoverview Audit a page to ensure that resources loaded with
* gzip/br/deflate compression.
*/
'use strict';

const Audit = require('./byte-efficiency-audit');
const URL = require('../../lib/url-shim');

const IGNORE_THRESHOLD_IN_BYTES = 1400;
const IGNORE_THRESHOLD_IN_PERCENT = 0.1;
const TOTAL_WASTED_BYTES_THRESHOLD = 10 * 1024; // 10KB

class ResponsesAreCompressed extends Audit {
/**
* @return {!AuditMeta}
*/
static get meta() {
return {
category: 'Performance',
name: 'uses-request-compression',
description: 'Compression enabled for server responses',
helpText: 'Text-based responses should be served with compression (gzip, deflate or brotli)' +
' to minimize total network bytes.' +
' [Learn more](https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/optimize-encoding-and-transfer).',
requiredArtifacts: ['ResponseCompression', 'networkRecords']
};
}

/**
* @param {!Artifacts} artifacts
* @param {number} networkThroughput
* @return {!AuditResult}
*/
static audit_(artifacts) {
const uncompressedResponses = artifacts.ResponseCompression;

let totalWastedBytes = 0;
const results = [];
uncompressedResponses.forEach(record => {
const originalSize = record.resourceSize;
const gzipSize = record.gzipSize;
const gzipSavings = originalSize - gzipSize;

// 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
) {
return;
}

// remove duplicates
const url = URL.getDisplayName(record.url);
const isDuplicate = results.find(res => res.url === url &&
res.totalBytes === record.resourceSize);
if (isDuplicate) {
return;
}

totalWastedBytes += gzipSavings;
const totalBytes = originalSize;
const gzipSavingsBytes = gzipSavings;
const gzipSavingsPercent = 100 * gzipSavingsBytes / totalBytes;
results.push({
url,
totalBytes,
wastedBytes: gzipSavingsBytes,
wastedPercent: gzipSavingsPercent,
potentialSavings: this.toSavingsString(gzipSavingsBytes, gzipSavingsPercent),
});
});

let debugString;
return {
passes: totalWastedBytes < TOTAL_WASTED_BYTES_THRESHOLD,
debugString,
results,
tableHeadings: {
url: 'Uncompressed resource URL',
totalKb: 'Original',
potentialSavings: 'GZIP Savings',
}
};
}
}

module.exports = ResponsesAreCompressed;
7 changes: 7 additions & 0 deletions lighthouse-core/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = {
"dobetterweb/notification-on-start",
"dobetterweb/domstats",
"dobetterweb/optimized-images",
"dobetterweb/response-compression",
"dobetterweb/tags-blocking-first-paint",
"dobetterweb/websql"
]
Expand Down Expand Up @@ -115,6 +116,7 @@ module.exports = {
// "byte-efficiency/unused-css-rules",
"byte-efficiency/offscreen-images",
"byte-efficiency/uses-optimized-images",
"byte-efficiency/uses-request-compression",
"byte-efficiency/uses-responsive-images",
"dobetterweb/appcache-manifest",
"dobetterweb/dom-size",
Expand Down Expand Up @@ -532,6 +534,10 @@ module.exports = {
"expectedValue": true,
"weight": 1
},
"uses-request-compression": {
"expectedValue": true,
"weight": 1
},
"uses-responsive-images": {
"expectedValue": true,
"weight": 1
Expand Down Expand Up @@ -614,6 +620,7 @@ module.exports = {
{"id": "script-blocking-first-paint", "weight": 0},
// {"id": "unused-css-rules", "weight": 0},
{"id": "uses-optimized-images", "weight": 0},
{"id": "uses-request-compression", "weight": 0},
{"id": "uses-responsive-images", "weight": 0},
{"id": "total-byte-weight", "weight": 0},
{"id": "dom-size", "weight": 0},
Expand Down
Loading

0 comments on commit ba01e2a

Please sign in to comment.