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

feat(predictive-perf): add CPU estimation #3162

Merged
merged 2 commits into from
Sep 16, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
49 changes: 44 additions & 5 deletions lighthouse-core/audits/predictive-perf.js
Original file line number Diff line number Diff line change
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 = 20;

class PredictivePerf extends Audit {
/**
* @return {!AuditMeta}
Expand All @@ -40,8 +43,7 @@ class PredictivePerf extends Audit {
const fmp = traceOfTab.timestamps.firstMeaningfulPaint;
return dependencyGraph.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 @@ -53,7 +55,11 @@ class PredictivePerf extends Audit {
static getPessimisticFMPGraph(dependencyGraph, traceOfTab) {
const fmp = traceOfTab.timestamps.firstMeaningfulPaint;
return dependencyGraph.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 @@ -62,8 +68,14 @@ class PredictivePerf extends Audit {
* @return {!Node}
*/
static getOptimisticTTCIGraph(dependencyGraph) {
// Adjust the critical long task threshold for microseconds
const minimumCpuTaskDuration = CRITICAL_LONG_TASK_THRESHOLD * 1000;

return dependencyGraph.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 > minimumCpuTaskDuration;
// Include all scripts and high priority requests
return node.resourceType === 'script' ||
node.record.priority() === 'High' ||
node.record.priority() === 'VeryHigh';
});
Expand All @@ -77,6 +89,18 @@ class PredictivePerf extends Audit {
return dependencyGraph;
}

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

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
Expand All @@ -98,7 +122,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.nodeTiming);

switch (key) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would personally prefer if/else if here since it's not really a loop (though really the Object.keys may not be needed at all since it's four fixed, known cases that have to be enumerated above and below anyways :P)

Copy link
Collaborator Author

@patrickhulce patrickhulce Sep 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah but those duplicated 3 lines at the top would really look pretty bad 😆

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
73 changes: 73 additions & 0 deletions lighthouse-core/gather/computed/dependency-graph/cpu-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @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>=} childEvents
*/
constructor(parentEvent, childEvents = []) {
const nodeId = `${parentEvent.tid}.${parentEvent.ts}`;
super(nodeId);

this._event = parentEvent;
this._childEvents = childEvents;
}

/**
* @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 childEvents() {
return this._childEvents;
}

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

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

module.exports = CPUNode;
Loading