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 1 commit
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
63 changes: 47 additions & 16 deletions lighthouse-core/audits/predictive-perf.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,49 @@ 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 getOptimisticFMPGraph(dependencyGraph, traceOfTab) {
const fmp = traceOfTab.timestamps.firstMeaningfulPaint;
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 > fmp || node.type === Node.TYPES.CPU) return false;
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';
});
Expand All @@ -53,12 +80,16 @@ class PredictivePerf extends Audit {
* @param {!TraceOfTabArtifact} traceOfTab
* @return {!Node}
*/
static getPessimisticFMPGraph(dependencyGraph, traceOfTab) {
const fmp = traceOfTab.timestamps.firstMeaningfulPaint;
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 > fmp) return false;
// Include CPU tasks that performed a layout
if (node.type === Node.TYPES.CPU) return node.didPerformLayout();
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();
});
Expand Down Expand Up @@ -116,8 +147,8 @@ class PredictivePerf extends Audit {
artifacts.requestTraceOfTab(trace),
]).then(([graph, traceOfTab]) => {
const graphs = {
optimisticFMP: PredictivePerf.getOptimisticFMPGraph(graph, traceOfTab),
pessimisticFMP: PredictivePerf.getPessimisticFMPGraph(graph, traceOfTab),
optimisticFCP: PredictivePerf.getOptimisticFCPGraph(graph, traceOfTab),
pessimisticFCP: PredictivePerf.getPessimisticFCPGraph(graph, traceOfTab),
optimisticTTCI: PredictivePerf.getOptimisticTTCIGraph(graph, traceOfTab),
pessimisticTTCI: PredictivePerf.getPessimisticTTCIGraph(graph, traceOfTab),
};
Expand All @@ -129,15 +160,15 @@ class PredictivePerf extends Audit {
const lastLongTaskEnd = PredictivePerf.getLastLongTaskEndTime(estimate.nodeTiming);

switch (key) {
case 'optimisticFMP':
case 'pessimisticFMP':
case 'optimisticFCP':
case 'pessimisticFCP':
values[key] = estimate.timeInMs;
break;
case 'optimisticTTCI':
values[key] = Math.max(values.optimisticFMP, lastLongTaskEnd);
values[key] = Math.max(values.optimisticFCP, lastLongTaskEnd);
break;
case 'pessimisticTTCI':
values[key] = Math.max(values.pessimisticFMP, lastLongTaskEnd);
values[key] = Math.max(values.pessimisticFCP, lastLongTaskEnd);
break;
}

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
8 changes: 4 additions & 4 deletions lighthouse-core/test/audits/predictive-perf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +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), 1513);
assert.equal(output.displayValue, '1,510\xa0ms');

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