From 05cea61d57d420a86bfdb78838e8ea930c01cb3a Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Fri, 17 Nov 2017 22:21:46 +0100 Subject: [PATCH] new_audit(bootup-time): Bootup time per script (#3563) --- lighthouse-core/audits/bootup-time.js | 203 ++++++++++++++++++ lighthouse-core/config/default.js | 2 + .../test/audits/bootup-time-test.js | 61 ++++++ 3 files changed, 266 insertions(+) create mode 100644 lighthouse-core/audits/bootup-time.js create mode 100644 lighthouse-core/test/audits/bootup-time-test.js diff --git a/lighthouse-core/audits/bootup-time.js b/lighthouse-core/audits/bootup-time.js new file mode 100644 index 000000000000..36fd5777e871 --- /dev/null +++ b/lighthouse-core/audits/bootup-time.js @@ -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 = { + 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=} trace + * @return {!Map} + */ + static getExecutionTimingsByURL(trace) { + const timelineModel = new DevtoolsTimelineModel(trace); + const bottomUpByName = timelineModel.bottomUpGroupBy('URL'); + 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)); + }); + 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]) => { + 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, + rawValue: totalBootupTime, + displayValue: Util.formatMilliseconds(totalBootupTime), + details: tableDetails, + extendedInfo: { + value: extendedInfo, + }, + }; + } +} + +module.exports = BootupTime; diff --git a/lighthouse-core/config/default.js b/lighthouse-core/config/default.js index 729ec073d6ec..f67c2b1fc451 100644 --- a/lighthouse-core/config/default.js +++ b/lighthouse-core/config/default.js @@ -90,6 +90,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', @@ -250,6 +251,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}, ], }, diff --git a/lighthouse-core/test/audits/bootup-time-test.js b/lighthouse-core/test/audits/bootup-time-test.js new file mode 100644 index 000000000000..27989c9cafbe --- /dev/null +++ b/lighthouse-core/test/audits/bootup-time-test.js @@ -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}); + 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); + }); +});