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(lantern): Remove min task duration from lantern graphs #9910

Merged
merged 24 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
48 changes: 38 additions & 10 deletions lighthouse-core/computed/page-dependency-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ const NetworkRecords = require('./network-records.js');

/** @typedef {import('../lib/dependency-graph/base-node.js').Node} Node */

// Tasks smaller than 10 ms have minimal impact on simulation
const MINIMUM_TASK_DURATION_OF_INTEREST = 10;
// Shorter tasks have negligible impact on simulation results.
const SIGNIFICANT_DUR_THRESHOLD_MS = 10;

// TODO: video files tend to be enormous and throw off all graph traversals, move this ignore
// into estimation logic when we use the dependency graph for other purposes.
const IGNORED_MIME_TYPES_REGEX = /^video/;
Expand Down Expand Up @@ -111,24 +112,18 @@ class PageDependencyGraph {

TracingProcessor.assertHasToplevelEvents(traceOfTab.mainThreadEvents);

const minimumEvtDur = MINIMUM_TASK_DURATION_OF_INTEREST * 1000;
while (i < traceOfTab.mainThreadEvents.length) {
const evt = traceOfTab.mainThreadEvents[i];
i++;

// Skip all trace events that aren't schedulable tasks with sizable duration
if (
!TracingProcessor.isScheduleableTask(evt) ||
!evt.dur ||
evt.dur < minimumEvtDur
) {
i++;
if (!TracingProcessor.isScheduleableTask(evt) || !evt.dur) {
continue;
}

// Capture all events that occurred within the task
/** @type {Array<LH.TraceEvent>} */
const children = [];
i++; // Start examining events after this one
for (
const endTime = evt.ts + evt.dur;
i < traceOfTab.mainThreadEvents.length && traceOfTab.mainThreadEvents[i].ts < endTime;
Expand Down Expand Up @@ -308,6 +303,39 @@ class PageDependencyGraph {
node.addDependency(rootNode);
}
}

// Second pass to prune the graph of short tasks.
for (const node of cpuNodes) {
if (node.event.dur >= SIGNIFICANT_DUR_THRESHOLD_MS * 1000) {
// Don't prune this node. The task is long so it will impact simulation.
continue;
}
// Prune the node if it isn't highly connected to minimize graph size. Rewiring the graph
// here replaces O(M + N) edges with (M * N) edges, which is fine if either M or N is at
// most 1.
if (node.getNumberOfDependencies() === 1 || node.getNumberOfDependents() <= 1) {
PageDependencyGraph._pruneNode(node);
}
}
}

/**
* Removes the given node from the graph, but retains all paths between its dependencies and
* dependents.
* @param {Node} node
*/
static _pruneNode(node) {
const dependencies = node.getDependencies();
const dependents = node.getDependents();
for (const dependency of dependencies) {
node.removeDependency(dependency);
for (const dependent of dependents) {
dependency.addDependent(dependent);
}
}
for (const dependent of dependents) {
node.removeDependent(dependent);
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions lighthouse-core/lib/dependency-graph/base-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ class BaseNode {
return this._dependents.slice();
}

/**
* @return {number}
*/
getNumberOfDependents() {
return this._dependents.length;
}
warrengm marked this conversation as resolved.
Show resolved Hide resolved
/**
* @return {Node[]}
*/
Expand Down
86 changes: 86 additions & 0 deletions lighthouse-core/test/computed/page-dependency-graph-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ describe('PageDependencyGraph computed artifact:', () => {

const getDependencyIds = node => node.getDependencies().map(node => node.id);

assert.equal(nodes.length, 7);
assert.deepEqual(getDependencyIds(nodes[0]), []);
assert.deepEqual(getDependencyIds(nodes[1]), [1, '1.200000']);
assert.deepEqual(getDependencyIds(nodes[2]), [1]);
Expand All @@ -281,6 +282,91 @@ describe('PageDependencyGraph computed artifact:', () => {
assert.deepEqual(getDependencyIds(nodes[6]), [4]);
});

it('should prune short tasks', () => {
const request0 = createRequest(0, '0', 0);
const request1 = createRequest(1, '1', 100, null, NetworkRequest.TYPES.Script);
const request2 = createRequest(2, '2', 200, null, NetworkRequest.TYPES.XHR);
const request3 = createRequest(3, '3', 300, null, NetworkRequest.TYPES.Script);
const request4 = createRequest(4, '4', 400, null, NetworkRequest.TYPES.XHR);
const networkRecords = [request0, request1, request2, request3, request4];

// Long task, should be kept in the output.
addTaskEvents(120, 50, [
{name: 'EvaluateScript', data: {url: '1'}},
{name: 'ResourceSendRequest', data: {requestId: 2}},
{name: 'XHRReadyStateChange', data: {readyState: 4, url: '2'}},
]);

// Short task, should be pruned, but the 4->5 relationship should be retained
warrengm marked this conversation as resolved.
Show resolved Hide resolved
addTaskEvents(350, 5, [
{name: 'EvaluateScript', data: {url: '3'}},
{name: 'ResourceSendRequest', data: {requestId: 4}},
{name: 'XHRReadyStateChange', data: {readyState: 4, url: '4'}},
]);

const graph = PageDependencyGraph.createGraph(traceOfTab, networkRecords);
const nodes = [];
graph.traverse(node => nodes.push(node));

const getDependencyIds = node => node.getDependencies().map(node => node.id);

assert.equal(nodes.length, 6);

assert.deepEqual(getDependencyIds(nodes[0]), []);
assert.deepEqual(getDependencyIds(nodes[1]), [0]);
assert.deepEqual(getDependencyIds(nodes[2]), [0, '1.120000']);
assert.deepEqual(getDependencyIds(nodes[3]), [0]);
assert.deepEqual(getDependencyIds(nodes[4]), [0, 3]);

assert.equal('1.120000', nodes[5].id);
assert.deepEqual(getDependencyIds(nodes[5]), [1]);
});

it('should not prune highly-connected short tasks', () => {
const request0 = createRequest(0, '0', 0);
const request1 = {
...createRequest(1, '1', 100, null, NetworkRequest.TYPES.Document),
documentURL: '1',
frameId: 'frame1',
};
const request2 = {
...createRequest(1, '2', 200, null, NetworkRequest.TYPES.Script),
warrengm marked this conversation as resolved.
Show resolved Hide resolved
documentURL: '1',
frameId: 'frame1',
};
const request3 = createRequest(3, '3', 300, null, NetworkRequest.TYPES.XHR);
const request4 = createRequest(4, '4', 400, null, NetworkRequest.TYPES.XHR);
const networkRecords = [request0, request1, request2, request3, request4];

// Short task, evaluates script (2) and sends two XHRs.
addTaskEvents(220, 5, [
{name: 'EvaluateScript', data: {url: '2', frame: 'frame1'}},

{name: 'ResourceSendRequest', data: {requestId: 3}},
{name: 'XHRReadyStateChange', data: {readyState: 4, url: '3'}},

{name: 'ResourceSendRequest', data: {requestId: 4}},
{name: 'XHRReadyStateChange', data: {readyState: 4, url: '4'}},
]);

const graph = PageDependencyGraph.createGraph(traceOfTab, networkRecords);
const nodes = [];
graph.traverse(node => nodes.push(node));

const getDependencyIds = node => node.getDependencies().map(node => node.id);

assert.equal(nodes.length, 6);

assert.deepEqual(getDependencyIds(nodes[0]), []);
assert.deepEqual(getDependencyIds(nodes[1]), [0]);
assert.deepEqual(getDependencyIds(nodes[2]), [0]);
assert.deepEqual(getDependencyIds(nodes[3]), [0, '1.220000']);
assert.deepEqual(getDependencyIds(nodes[4]), [0, '1.220000']);

assert.equal('1.220000', nodes[5].id);
assert.deepEqual(getDependencyIds(nodes[5]), [1, '1:duplicate']);
});

it('should set isMainDocument on first document request', () => {
const request1 = createRequest(1, '1', 0, null, NetworkRequest.TYPES.Other);
const request2 = createRequest(2, '2', 5, null, NetworkRequest.TYPES.Document);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"idb-keyval": "2.2.0",
"intl-messageformat-parser": "^1.8.1",
"isomorphic-fetch": "^2.2.1",
"jest": "^24.3.0",
"jest": "^24.9.0",
warrengm marked this conversation as resolved.
Show resolved Hide resolved
"jsdom": "^12.2.0",
"npm-run-posix-or-windows": "^2.0.2",
"nyc": "^13.3.0",
Expand All @@ -134,6 +134,7 @@
"configstore": "^3.1.1",
"cssstyle": "1.2.1",
"details-element-polyfill": "^2.4.0",
"global": "^4.4.0",
Copy link
Collaborator

Choose a reason for hiding this comment

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

mistake?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reverted

"http-link-header": "^0.8.0",
"inquirer": "^3.3.0",
"intl": "^1.2.5",
Expand Down
Loading