Skip to content
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

core(predictive-perf): predict FCP #3730

Merged
merged 3 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions lighthouse-core/audits/predictive-perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,74 @@ class PredictivePerf extends Audit {
return {
name: 'predictive-perf',
description: 'Predicted Performance (beta)',
helpText: 'Predicted performance evaluates how your site will perform under ' +
'a 3G connection on a mobile device.',
helpText:
'Predicted performance evaluates how your site will perform under ' +
'a 3G connection on a mobile device.',
scoringMode: Audit.SCORING_MODES.NUMERIC,
requiredArtifacts: ['traces', 'devtoolsLogs'],
};
}

/**
* @param {!Node} dependencyGraph
* @param {function()=} condition
* @return {!Set<string>}
*/
static getScriptUrls(dependencyGraph, condition) {
const scriptUrls = new Set();

dependencyGraph.traverse(node => {
if (node.type === Node.TYPES.CPU) return;
if (node.record._resourceType !== WebInspector.resourceTypes.Script) return;
if (condition && !condition(node)) return;
scriptUrls.add(node.record.url);
});

return scriptUrls;
}

/**
* @param {!Node} dependencyGraph
* @param {!TraceOfTabArtifact} traceOfTab
* @return {!Node}
*/
static getOptimisticFCPGraph(dependencyGraph, traceOfTab) {
const fcp = traceOfTab.timestamps.firstContentfulPaint;
const blockingScriptUrls = PredictivePerf.getScriptUrls(dependencyGraph, node => {
return (
node.endTime <= fcp && node.hasRenderBlockingPriority() && node.initiatorType !== 'script'
);
});

return dependencyGraph.cloneWithRelationships(node => {
if (node.endTime > fcp) return false;
// Include EvaluateScript tasks for blocking scripts
if (node.type === Node.TYPES.CPU) return node.isEvaluateScriptFor(blockingScriptUrls);
// Include non-script-initiated network requests with a render-blocking priority
return node.hasRenderBlockingPriority() && node.initiatorType !== 'script';
});
}

/**
* @param {!Node} dependencyGraph
* @param {!TraceOfTabArtifact} traceOfTab
* @return {!Node}
*/
static getPessimisticFCPGraph(dependencyGraph, traceOfTab) {
const fcp = traceOfTab.timestamps.firstContentfulPaint;
const blockingScriptUrls = PredictivePerf.getScriptUrls(dependencyGraph, node => {
return node.endTime <= fcp && node.hasRenderBlockingPriority();
});

return dependencyGraph.cloneWithRelationships(node => {
if (node.endTime > fcp) return false;
// Include EvaluateScript tasks for blocking scripts
if (node.type === Node.TYPES.CPU) return node.isEvaluateScriptFor(blockingScriptUrls);
// Include all network requests that had render-blocking priority (even script-initiated)
return node.hasRenderBlockingPriority();
});
}

/**
* @param {!Node} dependencyGraph
* @param {!TraceOfTabArtifact} traceOfTab
Expand Down Expand Up @@ -116,6 +177,8 @@ class PredictivePerf extends Audit {
artifacts.requestTraceOfTab(trace),
]).then(([graph, traceOfTab]) => {
const graphs = {
optimisticFCP: PredictivePerf.getOptimisticFCPGraph(graph, traceOfTab),
pessimisticFCP: PredictivePerf.getPessimisticFCPGraph(graph, traceOfTab),
optimisticFMP: PredictivePerf.getOptimisticFMPGraph(graph, traceOfTab),
pessimisticFMP: PredictivePerf.getPessimisticFMPGraph(graph, traceOfTab),
optimisticTTCI: PredictivePerf.getOptimisticTTCIGraph(graph, traceOfTab),
Expand All @@ -129,6 +192,8 @@ class PredictivePerf extends Audit {
const lastLongTaskEnd = PredictivePerf.getLastLongTaskEndTime(estimate.nodeTiming);

switch (key) {
case 'optimisticFCP':
case 'pessimisticFCP':
case 'optimisticFMP':
case 'pessimisticFMP':
values[key] = estimate.timeInMs;
Expand Down
9 changes: 9 additions & 0 deletions lighthouse-core/gather/computed/page-dependency-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ class PageDependencyGraphArtifact extends ComputedArtifact {
} else if (node !== rootNode) {
rootNode.addDependent(node);
}

const redirects = Array.from(node.record.redirects || []);
redirects.push(node.record);

for (let i = 1; i < redirects.length; i++) {
const redirectNode = networkNodeOutput.idToNodeMap.get(redirects[i - 1].requestId);
const actualNode = networkNodeOutput.idToNodeMap.get(redirects[i].requestId);
actualNode.addDependency(redirectNode);
}
});
}

Expand Down
14 changes: 14 additions & 0 deletions lighthouse-core/lib/dependency-graph/cpu-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,26 @@ class CPUNode extends Node {
}

/**
* Returns true if this node contains a Layout task.
* @return {boolean}
*/
didPerformLayout() {
return this._childEvents.some(evt => evt.name === 'Layout');
}

/**
* Returns true if this node contains the EvaluateScript task for a URL in the given set.
* @param {!Set<string>} urls
* @return {boolean}
*/
isEvaluateScriptFor(urls) {
return this._childEvents.some(evt => {
return evt.name === 'EvaluateScript' &&
evt.args.data &&
urls.has(evt.args.data.url);
});
}

/**
* @return {!CPUNode}
*/
Expand Down
6 changes: 4 additions & 2 deletions lighthouse-core/test/audits/predictive-perf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ describe('Performance: predictive performance audit', () => {

return PredictivePerf.audit(artifacts).then(output => {
assert.equal(output.score, 99);
assert.equal(Math.round(output.rawValue), 1696);
assert.equal(output.displayValue, '1,700\xa0ms');
assert.equal(Math.round(output.rawValue), 1333);
assert.equal(output.displayValue, '1,330\xa0ms');

const valueOf = name => Math.round(output.extendedInfo.value[name]);
assert.equal(valueOf('optimisticFCP'), 607);
assert.equal(valueOf('pessimisticFCP'), 607);
assert.equal(valueOf('optimisticFMP'), 754);
assert.equal(valueOf('pessimisticFMP'), 1191);
assert.equal(valueOf('optimisticTTCI'), 2438);
Expand Down