-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix critical chains & time to first byte errors
Showing
7 changed files
with
515 additions
and
441 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
441 changes: 172 additions & 269 deletions
441
lighthouse-core/test/gather/computed/critical-request-chains-test.js
Large diffs are not rendered by default.
Oops, something went wrong.
197 changes: 197 additions & 0 deletions
197
lighthouse-core/test/gather/computed/critical-requests-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
/** | ||
* Copyright 2016 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. | ||
*/ | ||
'use strict'; | ||
|
||
/* eslint-env mocha */ | ||
|
||
const GathererClass = require('../../../gather/computed/critical-requests'); | ||
const assert = require('assert'); | ||
const Gatherer = new GathererClass(); | ||
|
||
const HIGH = 'High'; | ||
const VERY_HIGH = 'VeryHigh'; | ||
const MEDIUM = 'Medium'; | ||
const LOW = 'Low'; | ||
const VERY_LOW = 'VeryLow'; | ||
|
||
function mockTracingData(prioritiesList, edges) { | ||
const networkRecords = prioritiesList.map((priority, index) => | ||
({_requestId: index.toString(), | ||
_resourceType: { | ||
_category: 'fake' | ||
}, | ||
priority: () => priority, | ||
initiatorRequest: () => null | ||
})); | ||
|
||
// add mock initiator information | ||
edges.forEach(edge => { | ||
const initiator = networkRecords[edge[0]]; | ||
networkRecords[edge[1]].initiatorRequest = () => initiator; | ||
}); | ||
|
||
return networkRecords; | ||
} | ||
|
||
function testGetCriticalRequests(data) { | ||
const networkRecords = mockTracingData(data.priorityList, data.edges); | ||
return Gatherer.request(networkRecords).then(criticalRequests => { | ||
assert.deepEqual(criticalRequests, data.expected); | ||
}); | ||
} | ||
|
||
let requests; | ||
function constructEmptyRequest(parent = null, id = null) { | ||
const request = { | ||
id, | ||
parent: requests[parent] || null, | ||
endTime: undefined, | ||
responseReceivedTime: undefined, | ||
startTime: undefined, | ||
url: undefined, | ||
transferSize: undefined | ||
}; | ||
|
||
requests[id] = request; | ||
|
||
return request; | ||
} | ||
|
||
describe('CriticalRequest gatherer: getCriticalRequests function', () => { | ||
beforeEach(() => { | ||
requests = {}; | ||
}); | ||
|
||
it('returns correct data for four critical requests', () => | ||
testGetCriticalRequests({ | ||
priorityList: [HIGH, MEDIUM, VERY_HIGH, HIGH], | ||
edges: [[0, 1], [1, 2], [2, 3]], | ||
expected: [ | ||
constructEmptyRequest(null, '0'), | ||
constructEmptyRequest('0', '1'), | ||
constructEmptyRequest('1', '2'), | ||
constructEmptyRequest('2', '3'), | ||
] | ||
})); | ||
|
||
it('returns correct data for chain interleaved with non-critical requests', | ||
() => testGetCriticalRequests({ | ||
priorityList: [MEDIUM, HIGH, LOW, MEDIUM, HIGH, VERY_LOW], | ||
edges: [[0, 1], [1, 2], [2, 3], [3, 4]], | ||
expected: [ | ||
constructEmptyRequest(null, '0'), | ||
constructEmptyRequest('0', '1'), | ||
] | ||
})); | ||
|
||
it('returns correct data for two parallel chains', () => | ||
testGetCriticalRequests({ | ||
priorityList: [HIGH, HIGH, HIGH, HIGH], | ||
edges: [[0, 2], [1, 3]], | ||
expected: [ | ||
constructEmptyRequest(null, '0'), | ||
constructEmptyRequest(null, '1'), | ||
constructEmptyRequest('0', '2'), | ||
constructEmptyRequest('1', '3'), | ||
] | ||
})); | ||
|
||
it('returns correct data for fork at root', () => | ||
testGetCriticalRequests({ | ||
priorityList: [HIGH, HIGH, HIGH], | ||
edges: [[0, 1], [0, 2]], | ||
expected: [ | ||
constructEmptyRequest(null, '0'), | ||
constructEmptyRequest('0', '1'), | ||
constructEmptyRequest('0', '2'), | ||
] | ||
})); | ||
|
||
it('returns correct data for fork at non root', () => | ||
testGetCriticalRequests({ | ||
priorityList: [HIGH, HIGH, HIGH, HIGH], | ||
edges: [[0, 1], [1, 2], [1, 3]], | ||
expected: [ | ||
constructEmptyRequest(null, '0'), | ||
constructEmptyRequest('0', '1'), | ||
constructEmptyRequest('1', '2'), | ||
constructEmptyRequest('1', '3'), | ||
] | ||
})); | ||
|
||
it('returns empty list when no critical request', () => | ||
testGetCriticalRequests({ | ||
priorityList: [LOW, LOW], | ||
edges: [[0, 1]], | ||
expected: {} | ||
})); | ||
|
||
it('returns empty list when no request whatsoever', () => | ||
testGetCriticalRequests({ | ||
priorityList: [], | ||
edges: [], | ||
expected: {} | ||
})); | ||
|
||
it('returns correct data on a random big graph', () => | ||
testGetCriticalRequests({ | ||
priorityList: Array(9).fill(HIGH), | ||
edges: [[0, 1], [1, 2], [1, 3], [4, 5], [5, 7], [7, 8], [5, 6]], | ||
expected: [ | ||
constructEmptyRequest(null, '0'), | ||
constructEmptyRequest('0', '1'), | ||
constructEmptyRequest('1', '2'), | ||
constructEmptyRequest('1', '3'), | ||
constructEmptyRequest(null, '4'), | ||
constructEmptyRequest('4', '5'), | ||
constructEmptyRequest('5', '6'), | ||
constructEmptyRequest('5', '7'), | ||
constructEmptyRequest('7', '8'), | ||
] | ||
})); | ||
|
||
it('handles redirects', () => { | ||
const networkRecords = mockTracingData([HIGH, HIGH, HIGH], [[0, 1], [1, 2]]); | ||
|
||
// Make a fake redirect | ||
networkRecords[1].requestId = '1:redirected.0'; | ||
networkRecords[2].requestId = '1'; | ||
return Gatherer.request(networkRecords).then(criticalRequests => { | ||
assert.deepEqual(criticalRequests, [ | ||
constructEmptyRequest(null, '0'), | ||
constructEmptyRequest('0', '1'), | ||
constructEmptyRequest('1', '2'), | ||
]); | ||
}); | ||
}); | ||
|
||
it('discards favicons as non-critical', () => { | ||
const networkRecords = mockTracingData([HIGH, HIGH, HIGH], [[0, 1], [0, 2]]); | ||
|
||
// 2nd record is a favicon | ||
networkRecords[1].url = 'https://example.com/favicon.ico'; | ||
networkRecords[1].parsedURL = { | ||
lastPathComponent: 'favicon.ico' | ||
}; | ||
// 3rd record is also a favicon | ||
networkRecords[2].mimeType = 'image/x-icon'; | ||
return Gatherer.request(networkRecords).then(criticalRequests => { | ||
assert.deepEqual(criticalRequests, [ | ||
constructEmptyRequest(null, '0'), | ||
]); | ||
}); | ||
}); | ||
}); |