diff --git a/lighthouse-core/audits/mainthread-work-breakdown.js b/lighthouse-core/audits/mainthread-work-breakdown.js new file mode 100644 index 000000000000..d93afeba6f88 --- /dev/null +++ b/lighthouse-core/audits/mainthread-work-breakdown.js @@ -0,0 +1,178 @@ +/** + * @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. + */ + +/** + * @fileoverview Audit a page to show a breakdown of execution timings on the main thread + */ + +'use strict'; + +const Audit = require('./audit'); +const Util = require('../report/v2/renderer/util'); +const DevtoolsTimelineModel = require('../lib/traces/devtools-timeline-model'); + +// We group all trace events into groups to show a highlevel breakdown of the page +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 PageExecutionTimings extends Audit { + /** + * @return {!AuditMeta} + */ + static get meta() { + return { + category: 'Performance', + name: 'mainthread-work-breakdown', + description: 'Main thread work breakdown', + informative: true, + 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 getExecutionTimingsByCategory(trace) { + const timelineModel = new DevtoolsTimelineModel(trace); + const bottomUpByName = timelineModel.bottomUpGroupBy('EventName'); + + const result = new Map(); + bottomUpByName.children.forEach((event, eventName) => + result.set(eventName, event.selfTime)); + + return result; + } + + /** + * @param {!Artifacts} artifacts + * @return {!AuditResult} + */ + static audit(artifacts) { + const trace = artifacts.traces[PageExecutionTimings.DEFAULT_PASS]; + const executionTimings = PageExecutionTimings.getExecutionTimingsByCategory(trace); + let totalExecutionTime = 0; + + const extendedInfo = {}; + const categoryTotals = {}; + const results = Array.from(executionTimings).map(([eventName, duration]) => { + totalExecutionTime += duration; + extendedInfo[eventName] = duration; + const groupName = taskToGroup[eventName]; + + const categoryTotal = categoryTotals[groupName] || 0; + categoryTotals[groupName] = categoryTotal + duration; + + return { + category: eventName, + group: groupName, + duration: Util.formatMilliseconds(duration, 1), + }; + }); + + const headings = [ + {key: 'group', itemType: 'text', text: 'Category'}, + {key: 'category', itemType: 'text', text: 'Work'}, + {key: 'duration', itemType: 'text', text: 'Time spent'}, + ]; + results.stableSort((a, b) => categoryTotals[b.group] - categoryTotals[a.group]); + const tableDetails = PageExecutionTimings.makeTableDetails(headings, results); + + return { + score: false, + rawValue: totalExecutionTime, + displayValue: Util.formatMilliseconds(totalExecutionTime), + details: tableDetails, + extendedInfo: { + value: extendedInfo, + }, + }; + } +} + +module.exports = PageExecutionTimings; diff --git a/lighthouse-core/config/default.js b/lighthouse-core/config/default.js index f67c2b1fc451..191f38b94d8d 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', + 'mainthread-work-breakdown', 'bootup-time', 'manual/pwa-cross-browser', 'manual/pwa-page-transitions', @@ -253,6 +254,7 @@ module.exports = { {id: 'user-timings', weight: 0, group: 'perf-info'}, {id: 'bootup-time', weight: 0, group: 'perf-info'}, {id: 'screenshot-thumbnails', weight: 0}, + {id: 'mainthread-work-breakdown', weight: 0, group: 'perf-info'}, ], }, 'accessibility': { diff --git a/lighthouse-core/test/audits/mainthread-work-breakdown-test.js b/lighthouse-core/test/audits/mainthread-work-breakdown-test.js new file mode 100644 index 000000000000..40341692e2b6 --- /dev/null +++ b/lighthouse-core/test/audits/mainthread-work-breakdown-test.js @@ -0,0 +1,144 @@ +/** + * @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 PageExecutionTimings = require('../../audits/mainthread-work-breakdown.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 siteWithRedirectTrace = JSON.parse( + fs.readFileSync(__dirname + '/../fixtures/traces/site-with-redirect.json') +); +const loadTrace = JSON.parse( + fs.readFileSync(__dirname + '/../fixtures/traces/load.json') +); +const errorTrace = JSON.parse( + fs.readFileSync(__dirname + '/../fixtures/traces/airhorner_no_fcp.json') +); + +const acceptableTraceExpectations = { + 'Compile Script': 25, + 'Composite Layers': 6, + 'DOM GC': 33, + 'Evaluate Script': 131, + 'Image Decode': 1, + 'Layout': 138, + 'Major GC': 8, + 'Minor GC': 7, + 'Paint': 52, + 'Parse HTML': 14, + 'Recalculate Style': 170, + 'Update Layer Tree': 25, +}; +const siteWithRedirectTraceExpectations = { + 'Compile Script': 38, + 'Composite Layers': 2, + 'DOM GC': 25, + 'Evaluate Script': 122, + 'Image Decode': 0, + 'Layout': 209, + 'Major GC': 10, + 'Minor GC': 11, + 'Paint': 4, + 'Parse HTML': 52, + 'Parse Stylesheet': 51, + 'Recalculate Style': 66, + 'Update Layer Tree': 5, +}; +const loadTraceExpectations = { + 'Animation Frame Fired': 6, + 'Composite Layers': 15, + 'Evaluate Script': 296, + 'Image Decode': 4, + 'Layout': 51, + 'Minor GC': 3, + 'Paint': 9, + 'Parse HTML': 25, + 'Recalculate Style': 80, + 'Update Layer Tree': 16, + 'XHR Load': 19, + 'XHR Ready State Change': 1, +}; + +describe('Performance: page execution timings audit', () => { + it('should compute the correct pageExecutionTiming values for the pwa trace', () => { + const artifacts = { + traces: { + [PageExecutionTimings.DEFAULT_PASS]: acceptableTrace, + }, + }; + const output = PageExecutionTimings.audit(artifacts); + const valueOf = name => Math.round(output.extendedInfo.value[name]); + + assert.equal(output.details.items.length, 12); + assert.equal(output.score, false); + assert.equal(Math.round(output.rawValue), 611); + + for (const category in output.extendedInfo.value) { + if (output.extendedInfo.value[category]) { + assert.equal(valueOf(category), acceptableTraceExpectations[category]); + } + } + }); + + it('should compute the correct pageExecutionTiming values for the redirect trace', () => { + const artifacts = { + traces: { + [PageExecutionTimings.DEFAULT_PASS]: siteWithRedirectTrace, + }, + }; + const output = PageExecutionTimings.audit(artifacts); + const valueOf = name => Math.round(output.extendedInfo.value[name]); + assert.equal(output.details.items.length, 13); + assert.equal(output.score, false); + assert.equal(Math.round(output.rawValue), 596); + + for (const category in output.extendedInfo.value) { + if (output.extendedInfo.value[category]) { + assert.equal(valueOf(category), siteWithRedirectTraceExpectations[category]); + } + } + }); + + it('should compute the correct pageExecutionTiming values for the load trace', () => { + const artifacts = { + traces: { + [PageExecutionTimings.DEFAULT_PASS]: loadTrace, + }, + }; + const output = PageExecutionTimings.audit(artifacts); + const valueOf = name => Math.round(output.extendedInfo.value[name]); + assert.equal(output.details.items.length, 12); + assert.equal(output.score, false); + assert.equal(Math.round(output.rawValue), 524); + + for (const category in output.extendedInfo.value) { + if (output.extendedInfo.value[category]) { + assert.equal(valueOf(category), loadTraceExpectations[category]); + } + } + }); + + it('should get no data when no events are present', () => { + const artifacts = { + traces: { + [PageExecutionTimings.DEFAULT_PASS]: errorTrace, + }, + }; + + const output = PageExecutionTimings.audit(artifacts); + assert.equal(output.details.items.length, 0); + assert.equal(output.score, false); + assert.equal(Math.round(output.rawValue), 0); + }); +});