-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added audit for request compression (gzip & br) (#1513)
* 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
Showing
11 changed files
with
857 additions
and
7 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
lighthouse-core/audits/byte-efficiency/uses-request-compression.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.