Skip to content

Commit

Permalink
core(uses-text-compression): ignore percent threshold for large savin…
Browse files Browse the repository at this point in the history
…gs (#16165)
  • Loading branch information
connorjclark authored Aug 27, 2024
1 parent 4b36705 commit 7e64d56
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
18 changes: 12 additions & 6 deletions core/audits/byte-efficiency/uses-text-compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,18 @@ class ResponsesAreCompressed extends ByteEfficiencyAudit {
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 ||
record.transferSize < gzipSize
) {
// Not every resource is smaller when compressed.
if (record.transferSize < gzipSize) {
return;
}

// If savings is small, let's be generous and not surface the minor savings.
if (gzipSavings < IGNORE_THRESHOLD_IN_BYTES) {
return;
}

// Require at least 20kb of savings ... or some percentage of total resource size.
if (gzipSavings < 20_000 && 1 - gzipSize / originalSize < IGNORE_THRESHOLD_IN_PERCENT) {
return;
}

Expand Down
38 changes: 15 additions & 23 deletions core/test/audits/byte-efficiency/uses-text-compression-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,36 @@
* SPDX-License-Identifier: Apache-2.0
*/

const KB = 1024;
import assert from 'assert/strict';

import ResponsesAreCompressedAudit from '../../../audits/byte-efficiency/uses-text-compression.js';

const KB = 1024;
const MB = 1024 * KB;

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

describe('Page uses optimized responses', () => {
it('fails when responses are collectively unoptimized', () => {
const auditResult = ResponsesAreCompressedAudit.audit_({
ResponseCompression: [
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)
],
});

assert.equal(auditResult.items.length, 2);
});

it('passes when all responses are sufficiently optimized', () => {
it('applies a threshold', () => {
const auditResult = ResponsesAreCompressedAudit.audit_({
ResponseCompression: [
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: 'index.js', resourceSize: 1000 * KB, gzipSize: 910 * KB}), // 90kb (hit)
generateResponse({file: 'index.css', resourceSize: 6 * KB, gzipSize: 4.8 * KB}), // 1.2kb
generateResponse({file: 'index2.css', resourceSize: 50 * KB, gzipSize: 37 * KB}), // 13kb (hit)
generateResponse({file: 'index.json', resourceSize: 10 * KB, gzipSize: 10 * KB}), // 0kb
generateResponse({file: 'uncompressed.xcustom', resourceSize: 11 * MB, gzipSize: 10 * MB}), // 1mb (hit)
generateResponse({file: 'compressed.json', resourceSize: 10 * KB, transferSize: 3 * KB,
gzipSize: 6 * KB}), // 0kb & 0%
gzipSize: 6 * KB}), // 0kb
],
});

assert.equal(auditResult.items.length, 1);
expect(auditResult.items.map(item => item.url)).toEqual(
['http://google.com/index.js', 'http://google.com/index2.css', 'http://google.com/uncompressed.xcustom']);
});
});

0 comments on commit 7e64d56

Please sign in to comment.