Skip to content

Commit

Permalink
Fix preload, tests and smoketest
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Jan 16, 2018
1 parent 8194adc commit 31c838d
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 9 deletions.
2 changes: 2 additions & 0 deletions lighthouse-cli/test/fixtures/online-only.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<html>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<link rel="stylesheet" href="/perf/preload_style.css" />
<body>
There was nothing special about this site,
nothing was left to be seen,
not even a kite.
<script src="/perf/preload_tester.js"></script>
</body>
</html>
9 changes: 9 additions & 0 deletions lighthouse-cli/test/fixtures/perf/level-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license Copyright 2017 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.
*/

/* eslint-disable */

document.write('<script src="level-3.js?delay=500"></script>');
12 changes: 12 additions & 0 deletions lighthouse-cli/test/fixtures/perf/level-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @license Copyright 2017 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.
*/

/* eslint-disable */

const dummyDiv = document.createElement('div');
dummyDiv.innerHTML = 'Hello!';

document.body.appendChild(dummyDiv);
Binary file not shown.
8 changes: 8 additions & 0 deletions lighthouse-cli/test/fixtures/perf/preload_style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@font-face {
font-family: "Lobster";
src: url("./lobster-v20-latin-regular.woff2") format("woff2");
}

body {
font-family: "Lobster";
}
9 changes: 9 additions & 0 deletions lighthouse-cli/test/fixtures/perf/preload_tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license Copyright 2017 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.
*/

/* eslint-disable */

document.write('<script src="level-2.js"></script>')
9 changes: 9 additions & 0 deletions lighthouse-cli/test/smokehouse/perf/expectations.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ module.exports = [
// Can be flaky, so test float rawValue instead of boolean score
rawValue: '<1000',
},
'uses-rel-preload': {
score: 0,
rawValue: '>500',
details: {
items: {
length: 1,
},
},
},
},
},
{
Expand Down
3 changes: 2 additions & 1 deletion lighthouse-core/audits/uses-rel-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ class UsesRelPreloadAudit extends Audit {
let maxWasted = 0;
// get all critical requests 2 + mainResourceIndex levels deep
const mainResourceIndex = mainResource.redirects ? mainResource.redirects.length : 0;

const criticalRequests = UsesRelPreloadAudit._flattenRequests(critChains,
3 + mainResourceIndex,2 + mainResourceIndex);
3 + mainResourceIndex, 2 + mainResourceIndex);
criticalRequests.forEach(request => {
const networkRecord = request;
if (!networkRecord._isLinkPreload && networkRecord.protocol !== 'data') {
Expand Down
35 changes: 27 additions & 8 deletions lighthouse-core/test/audits/uses-rel-preload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

const UsesRelPreload = require('../../audits/uses-rel-preload.js');
const assert = require('assert');
const defaultMainResource = {
_endTime: 1,
};

const mockArtifacts = (networkRecords, mockChain) => {
const mockArtifacts = (networkRecords, mockChain, mainResource = defaultMainResource) => {
return {
devtoolsLogs: {
[UsesRelPreload.DEFAULT_PASS]: [],
Expand All @@ -21,16 +24,23 @@ const mockArtifacts = (networkRecords, mockChain) => {
},
requestNetworkRecords: () => networkRecords,
requestMainResource: () => {
return Promise.resolve({
_endTime: 1,
});
return Promise.resolve(mainResource);
},
};
};

describe('Performance: uses-rel-preload audit', () => {
it(`should suggest preload resource`, () => {
const mainResource = Object.assign({}, defaultMainResource, {
redirects: [''],
});
const networkRecords = [
{
requestId: '2',
_endTime: 1,
_isLinkPreload: false,
_url: 'http://www.example.com',
},
{
requestId: '3',
_startTime: 10,
Expand All @@ -45,17 +55,24 @@ describe('Performance: uses-rel-preload audit', () => {
children: {
'3': {
request: networkRecords[0],
children: {
'4': {
request: networkRecords[1],
children: {},
},
},
},
},
},
},
},
};

return UsesRelPreload.audit(mockArtifacts(networkRecords, chains)).then(output => {
assert.equal(output.rawValue, 9000);
assert.equal(output.details.items.length, 1);
});
return UsesRelPreload.audit(mockArtifacts(networkRecords, chains, mainResource))
.then(output => {
assert.equal(output.rawValue, 9000);
assert.equal(output.details.items.length, 1);
});
});

it(`shouldn't suggest preload for already preloaded records`, () => {
Expand All @@ -74,6 +91,7 @@ describe('Performance: uses-rel-preload audit', () => {
children: {
'3': {
request: networkRecords[0],
children: {},
},
},
},
Expand Down Expand Up @@ -103,6 +121,7 @@ describe('Performance: uses-rel-preload audit', () => {
children: {
'3': {
request: networkRecords[0],
children: {},
},
},
},
Expand Down

0 comments on commit 31c838d

Please sign in to comment.