From bd0665c4b7eca67b5c2b99d3955aa677029f1cea Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Wed, 20 Feb 2019 16:36:04 -0800 Subject: [PATCH 1/4] tests(smokehouse): gzip test to assert transfer and resource sizes --- .../test/fixtures/byte-efficiency/gzip.html | 17 +++++++++++++ lighthouse-cli/test/fixtures/static-server.js | 20 ++++++++++++--- lighthouse-cli/test/smokehouse/byte-config.js | 1 + .../byte-efficiency/expectations.js | 25 +++++++++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 lighthouse-cli/test/fixtures/byte-efficiency/gzip.html diff --git a/lighthouse-cli/test/fixtures/byte-efficiency/gzip.html b/lighthouse-cli/test/fixtures/byte-efficiency/gzip.html new file mode 100644 index 000000000000..d6ba29c443ab --- /dev/null +++ b/lighthouse-cli/test/fixtures/byte-efficiency/gzip.html @@ -0,0 +1,17 @@ + + + + +Gzip + + + + + + + + diff --git a/lighthouse-cli/test/fixtures/static-server.js b/lighthouse-cli/test/fixtures/static-server.js index 2e6604b5de3f..9857cceedb88 100644 --- a/lighthouse-cli/test/fixtures/static-server.js +++ b/lighthouse-cli/test/fixtures/static-server.js @@ -9,6 +9,7 @@ /* eslint-disable no-console */ const http = require('http'); +const zlib = require('zlib'); const path = require('path'); const fs = require('fs'); const parseQueryString = require('querystring').parse; @@ -71,6 +72,7 @@ function requestHandler(request, response) { } let delay = 0; + let gzip = false; if (queryString) { const params = new URLSearchParams(queryString); // set document status-code @@ -93,12 +95,20 @@ function requestHandler(request, response) { } } + if (params.has('gzip')) { + gzip = Boolean(params.get('gzip')); + } + // redirect url to new url if present if (params.has('redirect')) { return setTimeout(sendRedirect, delay, params.get('redirect')); } } + if (gzip) { + headers['Content-Encoding'] = 'gzip'; + } + response.writeHead(statusCode, headers); // Delay the response @@ -106,7 +116,7 @@ function requestHandler(request, response) { return setTimeout(finishResponse, delay, data); } - finishResponse(data); + finishResponse(data, gzip); } function sendRedirect(url) { @@ -117,8 +127,12 @@ function requestHandler(request, response) { response.end(); } - function finishResponse(data) { - response.write(data, 'binary'); + function finishResponse(data, gzip) { + if (gzip) { + response.write(zlib.gzipSync(data), 'binary'); + } else { + response.write(data, 'binary'); + } response.end(); } } diff --git a/lighthouse-cli/test/smokehouse/byte-config.js b/lighthouse-cli/test/smokehouse/byte-config.js index 6718ab5263ee..cb901fa93513 100644 --- a/lighthouse-cli/test/smokehouse/byte-config.js +++ b/lighthouse-cli/test/smokehouse/byte-config.js @@ -12,6 +12,7 @@ module.exports = { extends: 'lighthouse:full', settings: { onlyAudits: [ + 'network-requests', 'offscreen-images', 'uses-webp-images', 'uses-optimized-images', diff --git a/lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js b/lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js index d26e7cf1546d..2156f2e9f03e 100644 --- a/lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js +++ b/lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js @@ -103,4 +103,29 @@ module.exports = [ }, }, }, + { + requestedUrl: 'http://localhost:10200/byte-efficiency/gzip.html', + finalUrl: 'http://localhost:10200/byte-efficiency/gzip.html', + audits: { + 'network-requests': { + details: { + items: [ + { + url: 'http://localhost:10200/byte-efficiency/gzip.html', + }, + { + url: 'http://localhost:10200/byte-efficiency/script.js?gzip=1', + transferSize: 1136, + resourceSize: 52997, + }, + { + url: 'http://localhost:10200/byte-efficiency/script.js', + transferSize: 53181, + resourceSize: 52997, + }, + ], + }, + }, + }, + }, ]; From b878ab3e751e81a89fd4f6fdf1441e278d785859 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Mon, 25 Feb 2019 11:33:57 -0800 Subject: [PATCH 2/4] useGzip --- lighthouse-cli/test/fixtures/static-server.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lighthouse-cli/test/fixtures/static-server.js b/lighthouse-cli/test/fixtures/static-server.js index 9857cceedb88..33072518983d 100644 --- a/lighthouse-cli/test/fixtures/static-server.js +++ b/lighthouse-cli/test/fixtures/static-server.js @@ -72,7 +72,7 @@ function requestHandler(request, response) { } let delay = 0; - let gzip = false; + let useGzip = false; if (queryString) { const params = new URLSearchParams(queryString); // set document status-code @@ -96,7 +96,7 @@ function requestHandler(request, response) { } if (params.has('gzip')) { - gzip = Boolean(params.get('gzip')); + useGzip = Boolean(params.get('gzip')); } // redirect url to new url if present @@ -105,7 +105,7 @@ function requestHandler(request, response) { } } - if (gzip) { + if (useGzip) { headers['Content-Encoding'] = 'gzip'; } @@ -116,7 +116,7 @@ function requestHandler(request, response) { return setTimeout(finishResponse, delay, data); } - finishResponse(data, gzip); + finishResponse(data, useGzip); } function sendRedirect(url) { @@ -127,8 +127,8 @@ function requestHandler(request, response) { response.end(); } - function finishResponse(data, gzip) { - if (gzip) { + function finishResponse(data, useGzip) { + if (useGzip) { response.write(zlib.gzipSync(data), 'binary'); } else { response.write(data, 'binary'); From 5cea47fac6d4d2494a807962a803418fd36c5df4 Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Mon, 25 Feb 2019 11:40:05 -0800 Subject: [PATCH 3/4] pass useGzip in timeout --- lighthouse-cli/test/fixtures/static-server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lighthouse-cli/test/fixtures/static-server.js b/lighthouse-cli/test/fixtures/static-server.js index 33072518983d..f24e65ba1385 100644 --- a/lighthouse-cli/test/fixtures/static-server.js +++ b/lighthouse-cli/test/fixtures/static-server.js @@ -113,7 +113,7 @@ function requestHandler(request, response) { // Delay the response if (delay > 0) { - return setTimeout(finishResponse, delay, data); + return setTimeout(finishResponse, delay, data, useGzip); } finishResponse(data, useGzip); From 903e89d2fce2b89a5b4ce22d425735b7abea291f Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Mon, 25 Feb 2019 11:43:14 -0800 Subject: [PATCH 4/4] change data inline for gzip --- lighthouse-cli/test/fixtures/static-server.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lighthouse-cli/test/fixtures/static-server.js b/lighthouse-cli/test/fixtures/static-server.js index f24e65ba1385..9e5b98bca363 100644 --- a/lighthouse-cli/test/fixtures/static-server.js +++ b/lighthouse-cli/test/fixtures/static-server.js @@ -106,6 +106,7 @@ function requestHandler(request, response) { } if (useGzip) { + data = zlib.gzipSync(data); headers['Content-Encoding'] = 'gzip'; } @@ -113,10 +114,10 @@ function requestHandler(request, response) { // Delay the response if (delay > 0) { - return setTimeout(finishResponse, delay, data, useGzip); + return setTimeout(finishResponse, delay, data); } - finishResponse(data, useGzip); + finishResponse(data); } function sendRedirect(url) { @@ -127,12 +128,8 @@ function requestHandler(request, response) { response.end(); } - function finishResponse(data, useGzip) { - if (useGzip) { - response.write(zlib.gzipSync(data), 'binary'); - } else { - response.write(data, 'binary'); - } + function finishResponse(data) { + response.write(data, 'binary'); response.end(); } }