Skip to content

Commit

Permalink
Rebased and use byte-efficiency audit
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Mar 1, 2017
1 parent dcb8c8b commit 250839b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
*/
'use strict';

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

const KB_IN_BYTES = 1024;
const TOTAL_WASTED_BYTES_THRESHOLD = 100 * KB_IN_BYTES;
const IGNORE_THRESHOLD_IN_BYTES = 1400;
const TOTAL_WASTED_BYTES_THRESHOLD = 1000 * 1024; // 1KB

class ResponsesAreCompressed extends Audit {
/**
Expand All @@ -42,24 +41,12 @@ class ResponsesAreCompressed extends Audit {
};
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
const networkRecords = artifacts.networkRecords[Audit.DEFAULT_PASS];

return artifacts.requestNetworkThroughput(networkRecords).then(networkThroughput => {
return ResponsesAreCompressed.audit_(artifacts, networkThroughput);
});
}

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

let totalWastedBytes = 0;
Expand All @@ -69,48 +56,37 @@ class ResponsesAreCompressed extends Audit {
const gzipSavings = originalSize - gzipSize;

// allow a pass if we don't get 10% savings or less than 1400 bytes
if (gzipSize / originalSize > 0.9 || gzipSavings < 1400) {
if (gzipSize / originalSize > 0.9 || gzipSavings < IGNORE_THRESHOLD_IN_BYTES) {
return results;
}

totalWastedBytes += gzipSavings;
const url = URL.getDisplayName(record.url);
const totalKb = originalSize / KB_IN_BYTES;
const gzipSavingsKb = gzipSavings / KB_IN_BYTES;
const totalBytes = originalSize;
const gzipSavingsBytes = gzipSavings;
const gzipSavingsPercent = 100 * gzipSavingsBytes / totalBytes;
results.push({
url,
total: `${totalKb.toLocaleString()} KB`,
gzipSavings: `${gzipSavingsKb.toLocaleString()} KB`,
totalBytes,
wastedBytes: gzipSavingsBytes,
wastedPercent: gzipSavingsPercent,
gzipSavings: this.toSavingsString(gzipSavingsBytes, gzipSavingsPercent),
});

return results;
}, []);

let debugString;
let displayValue = '';
if (totalWastedBytes > 1000) {
const totalWastedKb = Math.round(totalWastedBytes / KB_IN_BYTES);
// Only round to nearest 10ms since we're relatively hand-wavy
const totalWastedMs = Math.round(totalWastedBytes / networkThroughput * 100) * 10;
displayValue = `${totalWastedKb}KB (~${totalWastedMs}ms) potential savings`;
}

return ResponsesAreCompressed.generateAuditResult({
displayValue,
return {
passes: totalWastedBytes < TOTAL_WASTED_BYTES_THRESHOLD,
debugString,
rawValue: totalWastedBytes < TOTAL_WASTED_BYTES_THRESHOLD,
extendedInfo: {
formatter: Formatter.SUPPORTED_FORMATS.TABLE,
value: {
results,
tableHeadings: {
url: 'URL',
total: 'Original (KB)',
gzipSavings: 'GZIP Savings (KB)',
}
}
results,
tableHeadings: {
url: 'URL',
totalKb: 'Original',
gzipSavings: 'GZIP Savings',
}
});
};
}
}

Expand Down
4 changes: 2 additions & 2 deletions lighthouse-core/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"accessibility/tabindex",
"byte-efficiency/unused-css-rules",
"byte-efficiency/uses-optimized-images",
"byte-efficiency/uses-request-compression",
"byte-efficiency/uses-responsive-images",
"dobetterweb/external-anchors-use-rel-noopener",
"dobetterweb/appcache-manifest",
Expand All @@ -99,8 +100,7 @@
"dobetterweb/notification-on-start",
"dobetterweb/script-blocking-first-paint",
"dobetterweb/uses-http2",
"dobetterweb/uses-passive-event-listeners",
"dobetterweb/uses-request-compression"
"dobetterweb/uses-passive-event-listeners"
],

"aggregations": [{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/
'use strict';

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

function generateResponse(filename, type, originalSize, gzipSize) {
Expand All @@ -34,24 +35,24 @@ describe('Page uses optimized responses', () => {
it('fails when reponses are collectively unoptimized', () => {
const auditResult = ResponsesAreCompressedAudit.audit_({
ResponseCompression: [
generateResponse('index.js', 'text/javascript', 10000, 9000),
generateResponse('index.css', 'text/css', 5000, 3700),
generateResponse('index.json', 'application/json', 204800, 102400),
generateResponse('index.js', 'text/javascript', 100 * KB_BYTES, 90 * KB_BYTES),
generateResponse('index.css', 'text/css', 50 * KB_BYTES, 37 * KB_BYTES),
generateResponse('index.json', 'application/json', 2048 * KB_BYTES, 1024 * KB_BYTES),
],
});

assert.equal(auditResult.rawValue, false);
assert.equal(auditResult.passes, false);
});

it('passes when all reponses are sufficiently optimized', () => {
const auditResult = ResponsesAreCompressedAudit.audit_({
ResponseCompression: [
generateResponse('index.js', 'text/javascript', 100000, 91000),
generateResponse('index.css', 'text/css', 5000, 4000),
generateResponse('index.json', 'application/json', 1000, 500),
generateResponse('index.js', 'text/javascript', 1000 * KB_BYTES, 910 * KB_BYTES),
generateResponse('index.css', 'text/css', 50 * KB_BYTES, 40 * KB_BYTES),
generateResponse('index.json', 'application/json', 10 * KB_BYTES, 5 * KB_BYTES),
],
});

assert.equal(auditResult.rawValue, true);
assert.equal(auditResult.passes, true);
});
});

0 comments on commit 250839b

Please sign in to comment.