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

new_audit(bootup-time): Bootup time per script #3563

Merged
merged 7 commits into from
Nov 17, 2017
Merged
Show file tree
Hide file tree
Changes from 6 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
203 changes: 203 additions & 0 deletions lighthouse-core/audits/bootup-time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/**
* @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 Audit = require('./audit');
const DevtoolsTimelineModel = require('../lib/traces/devtools-timeline-model');
const WebInspector = require('../lib/web-inspector');
const Util = require('../report/v2/renderer/util.js');

const group = {
Copy link
Member

Choose a reason for hiding this comment

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

could pull these two objects into a lib/traces/trace-event-groups.js or something so that they can be used across audits

loading: 'Network request loading',
parseHTML: 'Parsing DOM',
styleLayout: 'Style & Layout',
compositing: 'Compositing',
painting: 'Paint',
gpu: 'GPU',
scripting: 'Script Evaluation',
scriptParseCompile: 'Script Parsing & Compile',
scriptGC: 'Garbage collection',
other: 'Other',
images: 'Images',
};
const taskToGroup = {
'Animation': group.painting,
'Async Task': group.other,
'Frame Start': group.painting,
'Frame Start (main thread)': group.painting,
'Cancel Animation Frame': group.scripting,
'Cancel Idle Callback': group.scripting,
'Compile Script': group.scriptParseCompile,
'Composite Layers': group.compositing,
'Console Time': group.scripting,
'Image Decode': group.images,
'Draw Frame': group.painting,
'Embedder Callback': group.scripting,
'Evaluate Script': group.scripting,
'Event': group.scripting,
'Animation Frame Fired': group.scripting,
'Fire Idle Callback': group.scripting,
'Function Call': group.scripting,
'DOM GC': group.scriptGC,
'GC Event': group.scriptGC,
'GPU': group.gpu,
'Hit Test': group.compositing,
'Invalidate Layout': group.styleLayout,
'JS Frame': group.scripting,
'Input Latency': group.scripting,
'Layout': group.styleLayout,
'Major GC': group.scriptGC,
'DOMContentLoaded event': group.scripting,
'First paint': group.painting,
'FMP': group.painting,
'FMP candidate': group.painting,
'Load event': group.scripting,
'Minor GC': group.scriptGC,
'Paint': group.painting,
'Paint Image': group.images,
'Paint Setup': group.painting,
'Parse Stylesheet': group.parseHTML,
'Parse HTML': group.parseHTML,
'Parse Script': group.scriptParseCompile,
'Other': group.other,
'Rasterize Paint': group.painting,
'Recalculate Style': group.styleLayout,
'Request Animation Frame': group.scripting,
'Request Idle Callback': group.scripting,
'Request Main Thread Frame': group.painting,
'Image Resize': group.images,
'Finish Loading': group.loading,
'Receive Data': group.loading,
'Receive Response': group.loading,
'Send Request': group.loading,
'Run Microtasks': group.scripting,
'Schedule Style Recalculation': group.styleLayout,
'Scroll': group.compositing,
'Task': group.other,
'Timer Fired': group.scripting,
'Install Timer': group.scripting,
'Remove Timer': group.scripting,
'Timestamp': group.scripting,
'Update Layer': group.compositing,
'Update Layer Tree': group.compositing,
'User Timing': group.scripting,
'Create WebSocket': group.scripting,
'Destroy WebSocket': group.scripting,
'Receive WebSocket Handshake': group.scripting,
'Send WebSocket Handshake': group.scripting,
'XHR Load': group.scripting,
'XHR Ready State Change': group.scripting,
};

class BootupTime extends Audit {
/**
* @return {!AuditMeta}
*/
static get meta() {
return {
category: 'Performance',
name: 'bootup-time',
description: 'JavaScript boot-up time is high (> 4s)',
failureDescription: 'JavaScript boot-up time is too high.',
helpText: 'Consider reducing the time spent parsing, compiling and executing JS. ' +
'You may find delivering smaller JS payloads helps with this.',
requiredArtifacts: ['traces'],
};
}

/**
* @param {!Array<TraceEvent>=} trace
* @return {!Map<string, Number>}
*/
static getExecutionTimingsByURL(trace) {
const timelineModel = new DevtoolsTimelineModel(trace);
Copy link
Collaborator

Choose a reason for hiding this comment

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

are we doing this in a lot of places/should it be a computed artifact?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Are you referring to make a computed artifact requestExecutionTimingsByUrl(trace) or rather have a computed artifact requestExecutionTimings(trace, 'URL')

Copy link
Member

@paulirish paulirish Oct 20, 2017

Choose a reason for hiding this comment

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

the idea is to make DTM a computed artifact.

we were also discussing doing a deepFreeze (from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) on the trace data before sending it into devtools.

so it wont mutate.

if there are problems with this we could just deepFreeze each event in the traceEvents array. should be fine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

should i do it in this PR? or just a followup?

Copy link
Member

Choose a reason for hiding this comment

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

filed here #3702

const bottomUpByName = timelineModel.bottomUpGroupBy('URL');
Copy link
Member

Choose a reason for hiding this comment

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

bottomUpByURL

const result = new Map();

bottomUpByName.children.forEach((perUrlNode, url) => {
// when url is "" or about:blank, we skip it
if (!url || url === 'about:blank') {
return;
}

const tasks = {};
perUrlNode.children.forEach((perTaskPerUrlNode) => {
const taskGroup = WebInspector.TimelineUIUtils.eventStyle(perTaskPerUrlNode.event);
tasks[taskGroup.title] = tasks[taskGroup.title] || 0;
tasks[taskGroup.title] += Number((perTaskPerUrlNode.selfTime || 0).toFixed(1));
Copy link
Member

Choose a reason for hiding this comment

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

i felt weird about this rounding here as we're summing, so i moved it to outside of the loop.

});
result.set(url, tasks);
});

return result;
}

/**
* @param {!Artifacts} artifacts
* @return {!AuditResult}
*/
static audit(artifacts) {
const trace = artifacts.traces[BootupTime.DEFAULT_PASS];
const bootupTimings = BootupTime.getExecutionTimingsByURL(trace);

let totalBootupTime = 0;
const extendedInfo = {};
const headings = [
{key: 'url', itemType: 'url', text: 'URL'},
];

// Group tasks per url
const groupsPerUrl = Array.from(bootupTimings).map(([url, durations]) => {
Copy link
Member

Choose a reason for hiding this comment

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

this loop groups individual tasks into taskGroups. I figured this could be a lot simpler if we just do the same thing up inside the inner getExecutionTimingsByURL loop. that's what you'll see in #3703

extendedInfo[url] = durations;

const groups = [];
Object.keys(durations).forEach(task => {
totalBootupTime += durations[task];
const group = taskToGroup[task];

groups[group] = groups[group] || 0;
groups[group] += durations[task];

if (!headings.find(heading => heading.key === group)) {
headings.push(
{key: group, itemType: 'text', text: group}
);
}
});

return {
url: url,
groups,
};
});

// map data in correct format to create a table
const results = groupsPerUrl.map(({url, groups}) => {
const res = {};
headings.forEach(heading => {
res[heading.key] = Util.formatMilliseconds(groups[heading.key] || 0, 1);
});

res.url = url;

return res;
});

const tableDetails = BootupTime.makeTableDetails(headings, results);

return {
score: totalBootupTime < 4000,
Copy link
Collaborator

Choose a reason for hiding this comment

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

4s is quite generous, did we consider using a numeric instead of binary score to allow nuance?

rawValue: totalBootupTime,
displayValue: Util.formatMilliseconds(totalBootupTime),
details: tableDetails,
extendedInfo: {
value: extendedInfo,
},
};
}
}

module.exports = BootupTime;
2 changes: 2 additions & 0 deletions lighthouse-core/config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module.exports = {
'content-width',
'image-aspect-ratio',
'deprecations',
'bootup-time',
'manual/pwa-cross-browser',
'manual/pwa-page-transitions',
'manual/pwa-each-page-has-url',
Expand Down Expand Up @@ -242,6 +243,7 @@ module.exports = {
{id: 'dom-size', weight: 0, group: 'perf-info'},
{id: 'critical-request-chains', weight: 0, group: 'perf-info'},
{id: 'user-timings', weight: 0, group: 'perf-info'},
{id: 'bootup-time', weight: 0, group: 'perf-info'},
{id: 'screenshot-thumbnails', weight: 0},
],
},
Expand Down
61 changes: 61 additions & 0 deletions lighthouse-core/test/audits/bootup-time-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @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';

/* eslint-env mocha */
const BootupTime = require('../../audits/bootup-time.js');
const fs = require('fs');
const assert = require('assert');

// sadly require(file) is not working correctly.
// traceParser parser returns preact trace data the same as JSON.parse
// fails when require is used
const acceptableTrace = JSON.parse(
fs.readFileSync(__dirname + '/../fixtures/traces/progressive-app-m60.json')
);
const errorTrace = JSON.parse(
fs.readFileSync(__dirname + '/../fixtures/traces/airhorner_no_fcp.json')
);

describe('Performance: bootup-time audit', () => {
it('should compute the correct BootupTime values', (done) => {
const artifacts = {
traces: {
[BootupTime.DEFAULT_PASS]: acceptableTrace,
},
};

const output = BootupTime.audit(artifacts);
assert.equal(output.details.items.length, 8);
assert.equal(output.score, true);
assert.equal(Math.round(output.rawValue), 176);

const valueOf = name => output.extendedInfo.value[name];
assert.deepEqual(valueOf('https://www.google-analytics.com/analytics.js'), {'Evaluate Script': 40.1, 'Compile Script': 9.6, 'Recalculate Style': 0.2});
Copy link
Member

Choose a reason for hiding this comment

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

It was kind of confusing that our extendedInfo reported tasks, but the UI reported taskGroups. I don't have a strong opinion on what should be exposed in extendedInfo. Maybe we just leave extendedInfo empty this time? In the tests we could assert against the table details instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it might be confusing indeed but isn't it handy to show the raw information somewhere in the json report so people who want to use it can use it? Or isn't that why extendedInfo exists?

assert.deepEqual(valueOf('https://pwa.rocks/script.js'), {'Evaluate Script': 31.8, 'Layout': 5.5, 'Compile Script': 1.3});
assert.deepEqual(valueOf('https://www.googletagmanager.com/gtm.js?id=GTM-Q5SW'), {'Evaluate Script': 25, 'Compile Script': 5.5, 'Recalculate Style': 1.2});
assert.deepEqual(valueOf('https://www.google-analytics.com/plugins/ua/linkid.js'), {'Evaluate Script': 25.2, 'Compile Script': 1.2});
assert.deepEqual(valueOf('https://www.google-analytics.com/cx/api.js?experiment=jdCfRmudTmy-0USnJ8xPbw'), {'Compile Script': 3, 'Evaluate Script': 1.2});
assert.deepEqual(valueOf('https://www.google-analytics.com/cx/api.js?experiment=qvpc5qIfRC2EMnbn6bbN5A'), {'Compile Script': 2.5, 'Evaluate Script': 1});
assert.deepEqual(valueOf('https://pwa.rocks/'), {'Parse HTML': 14.2, 'Evaluate Script': 6.1, 'Compile Script': 1.2});
assert.deepEqual(valueOf('https://pwa.rocks/0ff789bf.js'), {'Parse HTML': 0});

done();
});

it('should get no data when no events are present', () => {
const artifacts = {
traces: {
[BootupTime.DEFAULT_PASS]: errorTrace,
},
};

const output = BootupTime.audit(artifacts);
assert.equal(output.details.items.length, 0);
assert.equal(output.score, true);
assert.equal(Math.round(output.rawValue), 0);
});
});