Skip to content

Commit

Permalink
feat(predictive-perf): add CPU estimation
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Aug 21, 2017
1 parent 00e1a7e commit 6e90985
Show file tree
Hide file tree
Showing 10 changed files with 688 additions and 116 deletions.
46 changes: 41 additions & 5 deletions lighthouse-core/audits/predictive-perf.js
Expand Up @@ -15,6 +15,9 @@ const Node = require('../gather/computed/dependency-graph/node.js');
const SCORING_POINT_OF_DIMINISHING_RETURNS = 1700;
const SCORING_MEDIAN = 10000;

// Any CPU task of 20 ms or more will end up being a critical long task on mobile
const CRITICAL_LONG_TASK_THRESHOLD = 20000;

class PredictivePerf extends Audit {
/**
* @return {!AuditMeta}
Expand All @@ -38,8 +41,7 @@ class PredictivePerf extends Audit {
const fmp = traceOfTab.timestamps.firstMeaningfulPaint;
return graph.cloneWithRelationships(node => {
if (node.endTime > fmp) return false;
if (node.type !== Node.TYPES.NETWORK) return true;
return node.record.priority() === 'VeryHigh'; // proxy for render-blocking
return node.isRenderBlocking();
});
}

Expand All @@ -51,7 +53,11 @@ class PredictivePerf extends Audit {
static getPessimisticFMPGraph(graph, traceOfTab) {
const fmp = traceOfTab.timestamps.firstMeaningfulPaint;
return graph.cloneWithRelationships(node => {
return node.endTime <= fmp;
if (node.endTime > fmp) return false;
// Include CPU tasks that performed a layout
if (node.type === Node.TYPES.CPU) return node.didPerformLayout();
// Include every request before FMP that wasn't an image
return node.resourceType !== 'image';
});
}

Expand All @@ -61,7 +67,10 @@ class PredictivePerf extends Audit {
*/
static getOptimisticTTCIGraph(graph) {
return graph.cloneWithRelationships(node => {
return node.record._resourceType && node.record._resourceType._name === 'script' ||
// Include everything that might be a long task
if (node.type === Node.TYPES.CPU) return node.event.dur > CRITICAL_LONG_TASK_THRESHOLD;
// Include all scripts and high priority requests
return node.resourceType === 'script' ||
node.record.priority() === 'High' ||
node.record.priority() === 'VeryHigh';
});
Expand All @@ -75,6 +84,18 @@ class PredictivePerf extends Audit {
return graph;
}

/**
* @param {!Map<!Node, {startTime, endTime}>} nodeAuxData
* @return {number}
*/
static getLastLongTaskEndTime(nodeAuxData) {
return Array.from(nodeAuxData.entries())
.filter(([node, auxData]) => node.type === Node.TYPES.CPU &&
auxData.endTime - auxData.startTime > 50)
.map(([_, auxData]) => auxData.endTime)
.reduce((max, x) => Math.max(max, x), 0);
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
Expand All @@ -96,7 +117,22 @@ class PredictivePerf extends Audit {
let sum = 0;
const values = {};
Object.keys(graphs).forEach(key => {
values[key] = PageDependencyGraph.computeGraphDuration(graphs[key]);
const estimate = PageDependencyGraph.estimateGraph(graphs[key]);
const lastLongTaskEnd = PredictivePerf.getLastLongTaskEndTime(estimate.nodeAuxiliaryData);

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

sum += values[key];
});

Expand Down
71 changes: 71 additions & 0 deletions lighthouse-core/gather/computed/dependency-graph/cpu-node.js
@@ -0,0 +1,71 @@
/**
* @license Copyright 2017 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 Node = require('./node');

class CPUNode extends Node {
/**
* @param {!TraceEvent} parentEvent
* @param {!Array<TraceEvent>=} children
*/
constructor(parentEvent, children = []) {
super(`${parentEvent.tid}.${parentEvent.ts}`);
this._event = parentEvent;
this._children = children;
}

/**
* @return {string}
*/
get type() {
return Node.TYPES.CPU;
}

/**
* @return {number}
*/
get startTime() {
return this._event.ts;
}

/**
* @return {number}
*/
get endTime() {
return this._event.ts + this._event.dur;
}

/**
* @return {!TraceEvent}
*/
get event() {
return this._event;
}

/**
* @return {!TraceEvent}
*/
get children() {
return this._children;
}

/**
* @return {boolean}
*/
didPerformLayout() {
return this._children.some(evt => evt.name === 'Layout');
}

/**
* @return {!CPUNode}
*/
cloneWithoutRelationships() {
return new CPUNode(this._event, this._children);
}
}

module.exports = CPUNode;

0 comments on commit 6e90985

Please sign in to comment.