-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Gatherer for critical request chains #300
Closed
Closed
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0cdc88e
qweqwe
deepanjanroy c4964d8
WIP overdep
deepanjanroy 8d5b7c7
overdep WIP
deepanjanroy e9a4850
Merge branch 'master' into overdep
deepanjanroy f01db28
Add audit and tests
deepanjanroy 6916f5e
Merge branch 'master' into overdep
deepanjanroy d742c05
Actually add audit file. Remove old require
deepanjanroy ad660da
criticial chain more WIP
deepanjanroy b76cfde
Merge branch 'master' into overdep
deepanjanroy 123d133
even more WIP
deepanjanroy b3c7e46
critical chains: more more WIP
deepanjanroy c3f293b
Merge branch 'master' into overdep
deepanjanroy 93b7c2a
fix lints
deepanjanroy 72163c2
let -> const. Remove Duplicate test.
deepanjanroy 2a1be4a
Link to priority doc
deepanjanroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/** | ||
* @license | ||
* 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'; | ||
|
||
const Gather = require('./gather'); | ||
const childProcess = require('child_process'); | ||
const fs = require('fs'); | ||
const log = require('../lib/log.js'); | ||
|
||
const flatten = arr => arr.reduce((a, b) => a.concat(b), []); | ||
const contains = (arr, elm) => arr.indexOf(elm) > -1; | ||
|
||
class Node { | ||
get requestId() { | ||
return this.request.requestId; | ||
} | ||
constructor(request, parent) { | ||
this.children = []; | ||
this.parent = parent; | ||
this.request = request; | ||
} | ||
|
||
setParent(parentNode) { | ||
this.parent = parentNode; | ||
} | ||
|
||
addChild(childNode) { | ||
this.children.push(childNode); | ||
} | ||
|
||
} | ||
|
||
class CriticalNetworkChains extends Gather { | ||
|
||
get criticalPriorities() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a comment with link to https://docs.google.com/document/u/1/d/1bCDuq9H1ih9iNjgzyAL0gpwNFiEP4TZS-YLRp_RuMlc/edit to explain this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
return ['VeryHigh', 'High', 'Medium']; | ||
} | ||
|
||
postProfiling(options, tracingData) { | ||
const graph = this._getNetworkDependencyGraph(options.url, tracingData); | ||
const chains = this.getCriticalChains(tracingData.networkRecords, graph); | ||
|
||
// There logs are here so we can test this gatherer | ||
// Will be removed when we have a way to surface them in the report | ||
const nonTrivialChains = chains.filter(chain => chain.length > 1); | ||
|
||
// Note: Approximately, | ||
// startTime: time when request was dispatched | ||
// responseReceivedTime: either time to first byte, or time of receiving | ||
// the end of response headers | ||
// endTime: time when response loading finished | ||
const debuggingData = nonTrivialChains.map(chain => ({ | ||
urls: chain.map(request => request._url), | ||
totalRequests: chain.length, | ||
times: chain.map(request => ({ | ||
startTime: request.startTime, | ||
endTime: request.endTime, | ||
responseReceivedTime: request.responseReceivedTime | ||
})), | ||
totalTimeBetweenBeginAndEnd: | ||
(chain[chain.length - 1].endTime - chain[0].startTime), | ||
totalLoadingTime: chain.reduce((acc, req) => | ||
acc + (req.endTime - req.responseReceivedTime), 0) | ||
})); | ||
log.log('info', 'cricitalChains', JSON.stringify(debuggingData)); | ||
return {CriticalNetworkChains: chains}; | ||
} | ||
|
||
getCriticalChains(networkRecords, graph) { | ||
// TODO: Should we also throw out requests after DOMContentLoaded? | ||
const criticalRequests = networkRecords.filter( | ||
req => contains(this.criticalPriorities, req._initialPriority)); | ||
|
||
// Build a map of requestID -> Node. | ||
const requestIdToNodes = new Map(); | ||
for (let request of criticalRequests) { | ||
const requestNode = new Node(request, null); | ||
requestIdToNodes.set(requestNode.requestId, requestNode); | ||
} | ||
|
||
// Connect the parents and children. | ||
for (let edge of graph.edges) { | ||
const fromNode = graph.nodes[edge.__from_node_index]; | ||
const toNode = graph.nodes[edge.__to_node_index]; | ||
const fromRequestId = fromNode.request.request_id; | ||
const toRequestId = toNode.request.request_id; | ||
|
||
if (requestIdToNodes.has(fromRequestId) && | ||
requestIdToNodes.has(toRequestId)) { | ||
const fromRequestNode = requestIdToNodes.get(fromRequestId); | ||
const toRequestNode = requestIdToNodes.get(toRequestId); | ||
|
||
fromRequestNode.addChild(toRequestNode); | ||
toRequestNode.setParent(fromRequestNode); | ||
} | ||
} | ||
|
||
const nodesList = [...requestIdToNodes.values()]; | ||
const orphanNodes = nodesList.filter(node => node.parent === null); | ||
const nodeChains = flatten(orphanNodes.map( | ||
node => this._getChainsDFS(node))); | ||
const requestChains = nodeChains.map(chain => chain.map( | ||
node => node.request)); | ||
return requestChains; | ||
} | ||
|
||
_getChainsDFS(startNode) { | ||
if (startNode.children.length === 0) { | ||
return [[startNode]]; | ||
} | ||
|
||
const childrenChains = flatten(startNode.children.map(child => | ||
this._getChainsDFS(child))); | ||
return childrenChains.map(chain => [startNode].concat(chain)); | ||
} | ||
|
||
_saveClovisData(url, tracingData, filename) { | ||
const clovisData = { | ||
url: url, | ||
traceContents: tracingData.traceContents, | ||
frameLoadEvents: tracingData.frameLoadEvents, | ||
rawNetworkEvents: tracingData.rawNetworkEvents | ||
}; | ||
fs.writeFileSync(filename, JSON.stringify(clovisData)); | ||
} | ||
|
||
_getNetworkDependencyGraph(url, tracingData) { | ||
const clovisDataFilename = 'clovisData.json'; | ||
const clovisGraphFilename = 'dependency-graph.json'; | ||
|
||
// These will go away once we implement initiator graph ourselves | ||
this._saveClovisData(url, tracingData, clovisDataFilename); | ||
childProcess.execSync('python scripts/process_artifacts.py'); | ||
childProcess.execSync('python scripts/netdep_graph_json.py', {stdio: [0, 1, 2]}); | ||
const depGraphString = fs.readFileSync(clovisGraphFilename); | ||
|
||
try { | ||
fs.unlinkSync(clovisDataFilename); | ||
fs.unlinkSync(clovisGraphFilename); | ||
} catch (e) { | ||
console.error(e); | ||
// Should not halt lighthouse for a file delete error | ||
} | ||
|
||
return JSON.parse(depGraphString).graph; | ||
} | ||
} | ||
|
||
module.exports = CriticalNetworkChains; |
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
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,126 @@ | ||
/** | ||
* 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'; | ||
|
||
const GathererClass = require('../../../src/gatherers/critical-network-chains'); | ||
const assert = require('assert'); | ||
|
||
const Gatherer = new GathererClass(); | ||
|
||
function mockTracingData(prioritiesList, edges) { | ||
const networkRecords = prioritiesList.map((priority, index) => | ||
({requestId: index, initialPriority: priority})); | ||
|
||
/* eslint-disable camelcase */ | ||
const nodes = networkRecords.map(record => | ||
({request: {request_id: record._requestId}})); | ||
|
||
const graphEdges = edges.map(edge => | ||
({__from_node_index: edge[0], __to_node_index: edge[1]})); | ||
/* eslint-enable camelcase */ | ||
|
||
return { | ||
networkRecords: networkRecords, | ||
graph: { | ||
nodes: nodes, | ||
edges: graphEdges | ||
} | ||
}; | ||
} | ||
|
||
function testGetCriticalChain(data) { | ||
const mockData = mockTracingData(data.priorityList, data.edges); | ||
const criticalChains = Gatherer.getCriticalChains( | ||
mockData.networkRecords, mockData.graph); | ||
// It is sufficient to only check the requestIds are correct in the chain | ||
const requestIdChains = criticalChains.map(chain => | ||
chain.map(node => node.requestId)); | ||
// Ordering of the chains do not matter | ||
assert.deepEqual(new Set(requestIdChains), new Set(data.expectedChains)); | ||
} | ||
|
||
const HIGH = 'High'; | ||
const VERY_HIGH = 'VeryHigh'; | ||
const MEDIUM = 'Medium'; | ||
const LOW = 'Low'; | ||
const VERY_LOW = 'VeryLow'; | ||
|
||
/* global describe, it*/ | ||
describe('CriticalNetworkChain gatherer: getCriticalChain function', () => { | ||
it('returns correct data for chain of four critical requests', () => | ||
testGetCriticalChain({ | ||
priorityList: [HIGH, MEDIUM, VERY_HIGH, HIGH], | ||
edges: [[0, 1], [1, 2], [2, 3]], | ||
expectedChains: [[0, 1, 2, 3]] | ||
})); | ||
|
||
it('returns correct data for chain interleaved with non-critical requests', | ||
() => testGetCriticalChain({ | ||
priorityList: [MEDIUM, HIGH, LOW, MEDIUM, HIGH, VERY_LOW], | ||
edges: [[0, 1], [1, 2], [2, 3], [3, 4]], | ||
expectedChains: [[0, 1], [3, 4]] | ||
})); | ||
|
||
it('returns correct data for two parallel chains', () => | ||
testGetCriticalChain({ | ||
priorityList: [HIGH, HIGH, HIGH, HIGH], | ||
edges: [[0, 2], [1, 3]], | ||
expectedChains: [[1, 3], [0, 2]] | ||
})); | ||
|
||
it('returns correct data for fork at root', () => | ||
testGetCriticalChain({ | ||
priorityList: [HIGH, HIGH, HIGH], | ||
edges: [[0, 1], [0, 2]], | ||
expectedChains: [[0, 1], [0, 2]] | ||
})); | ||
|
||
it('returns correct data for fork at non root', () => | ||
testGetCriticalChain({ | ||
priorityList: [HIGH, HIGH, HIGH, HIGH], | ||
edges: [[0, 1], [1, 2], [1, 3]], | ||
expectedChains: [[0, 1, 2], [0, 1, 3]] | ||
})); | ||
|
||
it('returns empty chain list when no critical request', () => | ||
testGetCriticalChain({ | ||
priorityList: [LOW, LOW], | ||
edges: [[0, 1]], | ||
expectedChains: [] | ||
})); | ||
|
||
it('returns empty chain list when no request whatsoever', () => | ||
testGetCriticalChain({ | ||
priorityList: [], | ||
edges: [], | ||
expectedChains: [] | ||
})); | ||
|
||
it('returns two single node chains for two independent requests', () => | ||
testGetCriticalChain({ | ||
priorityList: [HIGH, HIGH], | ||
edges: [], | ||
expectedChains: [[0], [1]] | ||
})); | ||
|
||
it('returns correct data on a random big graph', () => | ||
testGetCriticalChain({ | ||
priorityList: Array(9).fill(HIGH), | ||
edges: [[0, 1], [1, 2], [1, 3], [4, 5], [5, 7], [7, 8], [5, 6]], | ||
expectedChains: [ | ||
[0, 1, 2], [0, 1, 3], [4, 5, 7, 8], [4, 5, 6] | ||
]})); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arr.includes(elm)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't work in node :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's in 6.1.0, it would appear. I guess at this point since most people are bypassing 5 for 6 we could specify it as a minimum?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works on 6.0 too. Once we drop support for node 5.0 we can change this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm +1 on supporting 5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just FYI for the future: v8 syntax changes only ever happen in the major node version bumps.
As much as I want destructuring and rest parameters, I'd also like to hold off on requiring 6 for a bit longer.