From f8f9776959863a70e2b776e9be05b7516042aeed Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Fri, 25 May 2018 14:11:53 -0700 Subject: [PATCH] core(simulator): start nodes in priority order --- .../lib/dependency-graph/network-node.js | 15 +++++++++++++ .../dependency-graph/simulator/simulator.js | 21 ++++++++++++++++++- typings/web-inspector.d.ts | 4 +++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lighthouse-core/lib/dependency-graph/network-node.js b/lighthouse-core/lib/dependency-graph/network-node.js index 1a21f2e6d57a..a8525ea33d54 100644 --- a/lighthouse-core/lib/dependency-graph/network-node.js +++ b/lighthouse-core/lib/dependency-graph/network-node.js @@ -8,6 +8,13 @@ const Node = require('./node'); const WebInspector = require('../web-inspector'); +const NETWORK_PRIORITY_NUMBERS = { + VeryHigh: 0, + High: 1, + Medium: 2, + Low: 3, +}; + class NetworkNode extends Node { /** * @param {LH.WebInspector.NetworkRequest} networkRecord @@ -79,6 +86,14 @@ class NetworkNode extends Node { node.setIsMainDocument(this._isMainDocument); return node; } + + /** + * @param {LH.WebInspector.NetworkPriority} priority + * @return {number} + */ + static priorityToP0Number(priority) { + return NETWORK_PRIORITY_NUMBERS[priority] || 10; + } } module.exports = NetworkNode; diff --git a/lighthouse-core/lib/dependency-graph/simulator/simulator.js b/lighthouse-core/lib/dependency-graph/simulator/simulator.js index 0c89a76240d9..d93fcd8622d9 100644 --- a/lighthouse-core/lib/dependency-graph/simulator/simulator.js +++ b/lighthouse-core/lib/dependency-graph/simulator/simulator.js @@ -173,6 +173,25 @@ class Simulator { }); } + /** + * @param {Node[]} nodes + * @return {Node[]} + */ + _sortNodesByPriority(nodes) { + return nodes.slice().sort((nodeA, nodeB) => { + // If nodes are different type it doesn't matter + if (nodeA.type !== nodeB.type) return 0; + // If nodes are CPU nodes, just prioritize the one that actually executed first + if (nodeA.type === Node.TYPES.CPU) return nodeA.startTime - nodeB.startTime; + // Else the nodes are network nodes, prioritize by priority, then actual startTime + const networkA = /** @type {NetworkNode} */ (nodeA); + const networkB = /** @type {NetworkNode} */ (nodeB); + const priorityA = NetworkNode.priorityToP0Number(networkA.record.priority()); + const priorityB = NetworkNode.priorityToP0Number(networkB.record.priority()); + return priorityA === priorityB ? nodeA.startTime - nodeB.startTime : priorityA - priorityB; + }); + } + /** * @param {Node} node * @param {number} totalElapsedTime @@ -398,7 +417,7 @@ class Simulator { // loop as long as we have nodes in the queue or currently in progress while (nodesReadyToStart.size || nodesInProgress.size) { // move all possible queued nodes to in progress - for (const node of nodesReadyToStart) { + for (const node of this._sortNodesByPriority(Array.from(nodesReadyToStart))) { this._startNodeIfPossible(node, totalElapsedTime); } diff --git a/typings/web-inspector.d.ts b/typings/web-inspector.d.ts index ce845066a879..32e874865b90 100644 --- a/typings/web-inspector.d.ts +++ b/typings/web-inspector.d.ts @@ -6,6 +6,8 @@ declare global { module LH.WebInspector { + export type NetworkPriority = 'VeryHigh' | 'High' | 'Medium' | 'Low'; + // TODO(bckenny): standardize on underscored internal API // externs for chrome-devtools-frontend/front_end/sdk/NetworkRequest.js export interface NetworkRequest { @@ -45,7 +47,7 @@ declare global { _timing: Crdp.Network.ResourceTiming; _resourceType: ResourceType; _mimeType: string; - priority(): 'VeryHigh' | 'High' | 'Medium' | 'Low'; + priority(): NetworkPriority; _responseHeaders?: {name: string, value: string}[]; _fetchedViaServiceWorker?: boolean;