From 7e64d56e444d46c644dadc3b0716df0771f117c7 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Tue, 27 Aug 2024 12:30:03 -0700 Subject: [PATCH] core(uses-text-compression): ignore percent threshold for large savings (#16165) --- .../byte-efficiency/uses-text-compression.js | 18 ++++++--- .../uses-text-compression-test.js | 38 ++++++++----------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/core/audits/byte-efficiency/uses-text-compression.js b/core/audits/byte-efficiency/uses-text-compression.js index 6821453d6e54..f4a360737f2e 100644 --- a/core/audits/byte-efficiency/uses-text-compression.js +++ b/core/audits/byte-efficiency/uses-text-compression.js @@ -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; } diff --git a/core/test/audits/byte-efficiency/uses-text-compression-test.js b/core/test/audits/byte-efficiency/uses-text-compression-test.js index 3fffa1b32336..961ec3135c90 100644 --- a/core/test/audits/byte-efficiency/uses-text-compression-test.js +++ b/core/test/audits/byte-efficiency/uses-text-compression-test.js @@ -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']); }); });