Skip to content

Commit

Permalink
core(simulator): start nodes in priority order
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed May 25, 2018
1 parent e410b34 commit f8f9776
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
15 changes: 15 additions & 0 deletions lighthouse-core/lib/dependency-graph/network-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
21 changes: 20 additions & 1 deletion lighthouse-core/lib/dependency-graph/simulator/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion typings/web-inspector.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f8f9776

Please sign in to comment.