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 2 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
97 changes: 97 additions & 0 deletions lighthouse-core/audits/bootup-time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* @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 Util = require('../report/v2/renderer/util.js');

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((value, url) => {
Copy link
Member

Choose a reason for hiding this comment

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

a bit of bikeshedding?

value ==> perUrlNode ?

// when url is "", we skip it
if (!url) {
return;
}

const evaluateTime = value.children.get('EvaluateScript:@' + url) || {};
Copy link
Member

Choose a reason for hiding this comment

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

we could do this but i'd rather iterate over the perUrlNode children (which would be perTaskPerUrlNode i guess)...

so iterate over them.. then grab perTaskPerUrlNode.event.name. This should give you v8.compile and EvaluateScript. We can then reuse this stuff:

const group = {
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,
};

and we'll want any children with events of name in the 3 script*categories. (also note: many of those task names are for instant events so they have 0 duration. ;)

Copy link
Member

Choose a reason for hiding this comment

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

we can exclude GC from the report for now but might as well collect it here and put in extInfo ? or ignore it and leave a comment saying so..

const compileTime = value.children.get('v8.compile:@' + url) || {};

if (evaluateTime.selfTime || compileTime.selfTime) {
result.set(url, {
evaluate: Number((evaluateTime.selfTime || 0).toFixed(1)),
compile: Number((compileTime.selfTime || 0).toFixed(1)),
});
}
});

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 results = Array.from(bootupTimings).map(([url, durations]) => {
totalBootupTime += durations.evaluate + durations.compile;
extendedInfo[url] = durations;

return {
url: url,
evaluate: Util.formatMilliseconds(durations.evaluate, 1),
compile: Util.formatMilliseconds(durations.compile, 1),
};
});

const headings = [
{key: 'url', itemType: 'url', text: 'URL'},
{key: 'evaluate', itemType: 'text', text: 'Time To Evaluate'},
{key: 'compile', itemType: 'text', text: 'Time To Compile'},
];
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
60 changes: 60 additions & 0 deletions lighthouse-core/test/audits/bootup-time-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @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, 7);
assert.equal(output.score, true);
assert.equal(Math.round(output.rawValue), 155);

const valueOf = name => output.extendedInfo.value[name];
assert.deepEqual(valueOf('https://www.google-analytics.com/analytics.js'), {evaluate: 40.1, compile: 9.6});
assert.deepEqual(valueOf('https://pwa.rocks/script.js'), {evaluate: 31.8, compile: 1.3});
assert.deepEqual(valueOf('https://www.googletagmanager.com/gtm.js?id=GTM-Q5SW'), {evaluate: 25, compile: 5.5});
assert.deepEqual(valueOf('https://www.google-analytics.com/plugins/ua/linkid.js'), {evaluate: 25.2, compile: 1.2});
assert.deepEqual(valueOf('https://pwa.rocks/'), {evaluate: 6.1, compile: 1.2});
assert.deepEqual(valueOf('https://www.google-analytics.com/cx/api.js?experiment=jdCfRmudTmy-0USnJ8xPbw'), {evaluate: 1.2, compile: 3});
assert.deepEqual(valueOf('https://www.google-analytics.com/cx/api.js?experiment=qvpc5qIfRC2EMnbn6bbN5A'), {evaluate: 1, compile: 2.5});

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);
});
});