Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests(smokehouse): gzip test to assert transfer and resource sizes #7286

Merged
merged 4 commits into from
Feb 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lighthouse-cli/test/fixtures/byte-efficiency/gzip.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<!--
* Copyright 2019 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.
-->
<html>
<head>
<title>Gzip</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<script src="script.js?gzip=1"></script>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
20 changes: 17 additions & 3 deletions lighthouse-cli/test/fixtures/static-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -71,6 +72,7 @@ function requestHandler(request, response) {
}

let delay = 0;
let gzip = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useGzip

if (queryString) {
const params = new URLSearchParams(queryString);
// set document status-code
Expand All @@ -93,20 +95,28 @@ 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
if (delay > 0) {
return setTimeout(finishResponse, delay, data);
}

finishResponse(data);
finishResponse(data, gzip);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if passing it this way, would need to pass into the delay path above as well (to getdelay and gzip to work together).

Could also doing the gzip encoding when doing the header? e.g.

if (gzip) {
  headers['Content-Encoding'] = 'gzip';
  data = zlib.gzipSync(data);
}

response.writeHead(statusCode, headers);

// ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}

function sendRedirect(url) {
Expand All @@ -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();
}
}
Expand Down
1 change: 1 addition & 0 deletions lighthouse-cli/test/smokehouse/byte-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
extends: 'lighthouse:full',
settings: {
onlyAudits: [
'network-requests',
'offscreen-images',
'uses-webp-images',
'uses-optimized-images',
Expand Down
25 changes: 25 additions & 0 deletions lighthouse-cli/test/smokehouse/byte-efficiency/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these stable across platforms, like the native zlib mac/linux is the same byte-for-byte as js and transfer size is always the same and all that?

seems like we could loosen the assertion a bit

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question, no idea. For the purposes of the regression a ballpark is good enough (less than 1500 i guess).

Looks like travis / appveyor got the same thing I did.

This SO answer perhaps suggests that we can rely on zlib being the same? Interestingly there's a byte that represents the OS used, but that wouldn't change the length. You've got me curious now so I'm gonna dig in way deeper than necessary :P

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any findings from the 🐰 🕳 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just that it may change when Node upgrades zlib, but they haven't update it in years. Seems fine to punt.

resourceSize: 52997,
},
{
url: 'http://localhost:10200/byte-efficiency/script.js',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this is just for exact comparison with the gzip?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just the ?gzip=1 one is important for the regression test. This is just for comparing. And maybe it'd catch some other future issue? I'm open to removing.

transferSize: 53181,
resourceSize: 52997,
},
],
},
},
},
},
];