Skip to content
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
8 changes: 8 additions & 0 deletions lighthouse-cli/test/cli/__snapshots__/index-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ Object {
Object {
"path": "offline-start-url",
},
Object {
"path": "resource-summary",
},
Object {
"path": "manual/pwa-cross-browser",
},
Expand Down Expand Up @@ -811,6 +814,11 @@ Object {
"id": "font-display",
"weight": 0,
},
Object {
"group": "diagnostics",
"id": "resource-summary",
"weight": 0,
},
Object {
"id": "network-requests",
"weight": 0,
Expand Down
106 changes: 106 additions & 0 deletions lighthouse-core/audits/resource-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* @license Copyright 2019 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.js');
const NetworkRecords = require('../computed/network-records.js');
const ComputedResourceSummary = require('../computed/resource-summary.js');
const i18n = require('../lib/i18n/i18n.js');
const MainResource = require('../computed/main-resource.js');

const UIStrings = {
/** Imperative title of a Lighthouse audit that tells the user to minimize the size and quantity of resources used to load the page. */
title: 'Keep request counts and transfer sizes small',
/** Description of a Lighthouse audit that tells the user that they can setup a budgets for the quantity and size of page resources. No character length limits. */
description: 'To set budgets for the quantity and size of page resources,' +
Copy link
Collaborator

Choose a reason for hiding this comment

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

sorry if I already said this but are we gonna link this up to a doc? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was planning on doing that in a separate PR once we know where that lives.

Copy link
Contributor

Choose a reason for hiding this comment

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

I also added it to #8331 in the lightwallet section

' add a budget.json file.',
/** [ICU Syntax] Label for an audit identifying the number of requests and kilobytes used to load the page. */
displayValue: `{requestCount, plural, =1 {1 request} other {# requests}}` +
` • { byteCount, number, bytes } KB`,
};

const str_ = i18n.createMessageInstanceIdFn(__filename, UIStrings);

class ResourceSummary extends Audit {
/**
* @return {LH.Audit.Meta}
*/
static get meta() {
return {
id: 'resource-summary',
title: str_(UIStrings.title),
description: str_(UIStrings.description),
scoreDisplayMode: Audit.SCORING_MODES.INFORMATIVE,
requiredArtifacts: ['devtoolsLogs'],
};
}

/**
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts, context) {
const devtoolsLog = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
const networkRecords = await NetworkRecords.request(devtoolsLog, context);
const mainResource = await MainResource.request({devtoolsLog, URL: artifacts.URL}, context);
const summary = ComputedResourceSummary.summarize(networkRecords, mainResource.url);

/** @type {LH.Audit.Details.Table['headings']} */
const headings = [
{key: 'label', itemType: 'text', text: 'Resource Type'},
{key: 'count', itemType: 'numeric', text: 'Requests'},
{key: 'sizpi18e', itemType: 'bytes', text: 'Transfer Size'},
];


/** @type {Record<LH.Budget.ResourceType,string>} */
const strMappings = {
'total': str_(i18n.UIStrings.totalResourceType),
'document': str_(i18n.UIStrings.documentResourceType),
'script': str_(i18n.UIStrings.scriptResourceType),
'stylesheet': str_(i18n.UIStrings.stylesheetResourceType),
'image': str_(i18n.UIStrings.imageResourceType),
'media': str_(i18n.UIStrings.mediaResourceType),
'font': str_(i18n.UIStrings.fontResourceType),
'other': str_(i18n.UIStrings.otherResourceType),
'third-party': str_(i18n.UIStrings.thirdPartyResourceType),
};

const types = /** @type {Array<LH.Budget.ResourceType>} */ (Object.keys(summary));
const tableContents = types.map(type => {
return {
// ResourceType is included as an "id" for ease of use.
// It does not appear directly in the table.
resourceType: type,
label: strMappings[type],
count: summary[type].count,
size: summary[type].size,
};
}).sort((a, b) => {
// Sorts table rows to be:
// 1st row: Total
// 2nd to n-1 row: Sorted by descending size
// Last row: Third-party
if (a.resourceType === 'third-party') return 1;
return b.size - a.size;
});

const tableDetails = Audit.makeTableDetails(headings, tableContents);

return {
details: tableDetails,
score: 1,
displayValue: str_(UIStrings.displayValue, {
requestCount: summary.total.count,
byteCount: summary.total.size,
}),
};
}
}

module.exports = ResourceSummary;
module.exports.UIStrings = UIStrings;
2 changes: 2 additions & 0 deletions lighthouse-core/config/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ const defaultConfig = {
'main-thread-tasks',
'metrics',
'offline-start-url',
'resource-summary',
'manual/pwa-cross-browser',
'manual/pwa-page-transitions',
'manual/pwa-each-page-has-url',
Expand Down Expand Up @@ -378,6 +379,7 @@ const defaultConfig = {
{id: 'bootup-time', weight: 0, group: 'diagnostics'},
{id: 'mainthread-work-breakdown', weight: 0, group: 'diagnostics'},
{id: 'font-display', weight: 0, group: 'diagnostics'},
{id: 'resource-summary', weight: 0, group: 'diagnostics'},
// Audits past this point don't belong to a group and will not be shown automatically
{id: 'network-requests', weight: 0},
{id: 'network-rtt', weight: 0},
Expand Down
56 changes: 52 additions & 4 deletions lighthouse-core/lib/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,18 @@
"message": "Avoid multiple page redirects",
"description": "Imperative title of a Lighthouse audit that tells the user to eliminate the redirects taken through multiple URLs to load the page. This is shown in a list of audits that Lighthouse generates."
},
"lighthouse-core/audits/resource-summary.js | description": {
"message": "To set budgets for the quantity and size of page resources, add a budget.json file.",
"description": "Description of a Lighthouse audit that tells the user that they can setup a budgets for the quantity and size of page resources. No character length limits."
},
"lighthouse-core/audits/resource-summary.js | displayValue": {
"message": "{requestCount, plural, =1 {1 request} other {# requests}} • { byteCount, number, bytes } KB",
"description": "[ICU Syntax] Label for an audit identifying the number of requests and kilobytes used to load the page."
},
"lighthouse-core/audits/resource-summary.js | title": {
"message": "Keep request counts and transfer sizes small",
"description": "Imperative title of a Lighthouse audit that tells the user to minimize the size and quantity of resources used to load the page."
},
"lighthouse-core/audits/seo/canonical.js | description": {
"message": "Canonical links suggest which URL to show in search results. [Learn more](https://developers.google.com/web/tools/lighthouse/audits/canonical).",
"description": "Description of a Lighthouse audit that tells the user *why* they need to have a valid rel=canonical link. This is displayed after a user expands the section to see more. No character length limits. 'Learn More' becomes link text to additional documentation."
Expand Down Expand Up @@ -1223,14 +1235,54 @@
"message": "Potential savings of {wastedMs, number, milliseconds} ms",
"description": "Label shown per-audit to show how many milliseconds faster the page load could be if the user implemented the suggestions. The `{wastedMs}` placeholder will be replaced with the time duration, shown in milliseconds (e.g. 140 ms)"
},
"lighthouse-core/lib/i18n/i18n.js | documentResourceType": {
"message": "Document",
"description": "Label for a row in a data table; entries will be the total number and byte size of all 'Document' resources loaded by a web page."
},
"lighthouse-core/lib/i18n/i18n.js | fontResourceType": {
"message": "Font",
"description": "Label for a row in a data table; entries will be the total number and byte size of all 'Font' resources loaded by a web page."
},
"lighthouse-core/lib/i18n/i18n.js | imageResourceType": {
"message": "Image",
"description": "Label for a row in a data table; entries will be the total number and byte size of all 'Image' resources loaded by a web page."
},
"lighthouse-core/lib/i18n/i18n.js | mediaResourceType": {
"message": "Media",
"description": "Label for a row in a data table; entries will be the total number and byte size of all 'Media' resources loaded by a web page. 'Media' refers to audio and video files."
},
"lighthouse-core/lib/i18n/i18n.js | ms": {
"message": "{timeInMs, number, milliseconds} ms",
"description": "Used to show the duration in milliseconds that something lasted. The `{timeInMs}` placeholder will be replaced with the time duration, shown in milliseconds (e.g. 63 ms)"
},
"lighthouse-core/lib/i18n/i18n.js | otherResourceType": {
"message": "Other",
"description": "Label for a row in a data table; entries will be the total number and byte size of all resources loaded by a web page that don't fit into the categories of Document, Script, Stylesheet, Image, Media, & Font."
},
"lighthouse-core/lib/i18n/i18n.js | scriptResourceType": {
"message": "Script",
"description": "Label for a row in a data table; entries will be the total number and byte size of all 'Script' resources loaded by a web page. 'Script' refers to JavaScript or other files that are executable by a browser."
},
"lighthouse-core/lib/i18n/i18n.js | seconds": {
"message": "{timeInMs, number, seconds} s",
"description": "Used to show the duration in seconds that something lasted. The {timeInMs} placeholder will be replaced with the time duration, shown in seconds (e.g. 5.2 s)"
},
"lighthouse-core/lib/i18n/i18n.js | stylesheetResourceType": {
"message": "Stylesheet",
"description": "Label for a row in a data table; entries will be the total number and byte size of all 'Stylesheet' resources loaded by a web page. 'Stylesheet' refers to CSS stylesheets."
},
"lighthouse-core/lib/i18n/i18n.js | thirdPartyResourcesLabel": {
"message": "Show 3rd-party resources",
"description": "This label is for a checkbox above a table of items loaded by a web page. The checkbox is used to show or hide third-party (or \"3rd-party\") resources in the table, where \"third-party resources\" refers to items loaded by a web page from URLs that aren't controlled by the owner of the web page."
},
"lighthouse-core/lib/i18n/i18n.js | thirdPartyResourceType": {
"message": "Third-party",
"description": "Label for a row in a data table; entries will be the total number and byte size of all third-party resources loaded by a web page. 'Third-party resources are items loaded from URLs that aren't controlled by the owner of the web page."
},
"lighthouse-core/lib/i18n/i18n.js | totalResourceType": {
"message": "Total",
"description": "Label for a row in a data table; entries will be the total number and byte size of all resources loaded by a web page."
},
"lighthouse-core/lib/lh-error.js | badTraceRecording": {
"message": "Something went wrong with recording the trace over your page load. Please run Lighthouse again. ({errorCode})",
"description": "Error message explaining that the network trace was not able to be recorded for the Lighthouse run."
Expand Down Expand Up @@ -1343,10 +1395,6 @@
"message": "Expand snippet",
"description": "Label for button that shows all lines of the snippet when clicked"
},
"lighthouse-core/report/html/renderer/util.js | thirdPartyResourcesLabel": {
"message": "Show 3rd-party resources",
"description": "This label is for a checkbox above a table of items loaded by a web page. The checkbox is used to show or hide third-party (or \"3rd-party\") resources in the table, where \"third-party resources\" refers to items loaded by a web page from URLs that aren't controlled by the owner of the web page."
},
"lighthouse-core/report/html/renderer/util.js | toplevelWarningsMessage": {
"message": "There were issues affecting this run of Lighthouse:",
"description": "Label shown preceding any important warnings that may have invalidated the entire report. For example, if the user has Chrome extensions installed, they may add enough performance overhead that Lighthouse's performance metrics are unreliable. If shown, this will be displayed at the top of the report UI."
Expand Down
20 changes: 20 additions & 0 deletions lighthouse-core/lib/i18n/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ const UIStrings = {
columnWastedMs: 'Potential Savings',
/** Label for the time spent column in data tables, entries will be the number of milliseconds spent during a particular activity */
columnTimeSpent: 'Time Spent',
/** This label is for a checkbox above a table of items loaded by a web page. The checkbox is used to show or hide third-party (or "3rd-party") resources in the table, where "third-party resources" refers to items loaded by a web page from URLs that aren't controlled by the owner of the web page. */
thirdPartyResourcesLabel: 'Show 3rd-party resources',
/** Label for a row in a data table; entries will be the total number and byte size of all resources loaded by a web page. */
totalResourceType: 'Total',
/** Label for a row in a data table; entries will be the total number and byte size of all 'Document' resources loaded by a web page. */
documentResourceType: 'Document',
/** Label for a row in a data table; entries will be the total number and byte size of all 'Script' resources loaded by a web page. 'Script' refers to JavaScript or other files that are executable by a browser. */
scriptResourceType: 'Script',
/** Label for a row in a data table; entries will be the total number and byte size of all 'Stylesheet' resources loaded by a web page. 'Stylesheet' refers to CSS stylesheets. */
stylesheetResourceType: 'Stylesheet',
/** Label for a row in a data table; entries will be the total number and byte size of all 'Image' resources loaded by a web page. */
imageResourceType: 'Image',
/** Label for a row in a data table; entries will be the total number and byte size of all 'Media' resources loaded by a web page. 'Media' refers to audio and video files. */
mediaResourceType: 'Media',
/** Label for a row in a data table; entries will be the total number and byte size of all 'Font' resources loaded by a web page. */
fontResourceType: 'Font',
/** Label for a row in a data table; entries will be the total number and byte size of all resources loaded by a web page that don't fit into the categories of Document, Script, Stylesheet, Image, Media, & Font.*/
otherResourceType: 'Other',
/** Label for a row in a data table; entries will be the total number and byte size of all third-party resources loaded by a web page. 'Third-party resources are items loaded from URLs that aren't controlled by the owner of the web page. */
thirdPartyResourceType: 'Third-party',
};

const formats = {
Expand Down
3 changes: 0 additions & 3 deletions lighthouse-core/report/html/renderer/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,6 @@ Util.UIStrings = {
lsPerformanceCategoryDescription: '[Lighthouse](https://developers.google.com/web/tools/lighthouse/) analysis of the current page on an emulated mobile network. Values are estimated and may vary.',
/** Title of the lab data section of the Performance category. Within this section are various speed metrics which quantify the pageload performance into values presented in seconds and milliseconds. "Lab" is an abbreviated form of "laboratory", and refers to the fact that the data is from a controlled test of a website, not measurements from real users visiting that site. */
labDataTitle: 'Lab Data',

/** This label is for a checkbox above a table of items loaded by a web page. The checkbox is used to show or hide third-party (or "3rd-party") resources in the table, where "third-party resources" refers to items loaded by a web page from URLs that aren't controlled by the owner of the web page. */
thirdPartyResourcesLabel: 'Show 3rd-party resources',
};

if (typeof module !== 'undefined' && module.exports) {
Expand Down
89 changes: 89 additions & 0 deletions lighthouse-core/test/audits/resource-summary-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @license Copyright 2019 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 ResourceSummaryAudit = require('../../audits/resource-summary.js');
const networkRecordsToDevtoolsLog = require('../network-records-to-devtools-log.js');

/* eslint-env jest */

describe('Performance: Resource summary audit', () => {
let artifacts;
let context;
beforeEach(() => {
context = {computedCache: new Map()};

artifacts = {
devtoolsLogs: {
defaultPass: networkRecordsToDevtoolsLog([
{url: 'http://example.com/file.html', resourceType: 'Document', transferSize: 30},
{url: 'http://example.com/app.js', resourceType: 'Script', transferSize: 10},
{url: 'http://third-party.com/script.js', resourceType: 'Script', transferSize: 50},
{url: 'http://third-party.com/file.jpg', resourceType: 'Image', transferSize: 70},
])},
URL: {requestedUrl: 'https://example.com', finalUrl: 'https://example.com'},
};
});

it('has three table columns', async () => {
const result = await ResourceSummaryAudit.audit(artifacts, context);
expect(result.details.headings.length).toBe(3);
});

it('has the correct score', async () => {
const result = await ResourceSummaryAudit.audit(artifacts, context);
expect(result.score).toBe(1);
});

it('has the correct display value', async () => {
const result = await ResourceSummaryAudit.audit(artifacts, context);
expect(result.displayValue).toBeDisplayString('4 requests • 0 KB');
});

it('includes the correct properties for each table item', async () => {
const result = await ResourceSummaryAudit.audit(artifacts, context);
const item = result.details.items[0];
expect(item.resourceType).toEqual('total');
expect(item.label).toBeDisplayString('Total');
expect(item.count).toBe(4);
expect(item.size).toBe(160);
});

it('includes all resource types, regardless of whether page contains them', async () => {
const result = await ResourceSummaryAudit.audit(artifacts, context);
expect(Object.keys(result.details.items).length).toBe(9);
});

it('it displays "0" if there are no resources of that type', async () => {
const result = await ResourceSummaryAudit.audit(artifacts, context);
const fontItem = result.details.items.find(item => item.resourceType === 'font');
expect(fontItem.count).toBe(0);
expect(fontItem.size).toBe(0);
});

describe('table ordering', () => {
it('except for the last row, it sorts items by size (descending)', async () => {
const result = await ResourceSummaryAudit.audit(artifacts, context);
const items = result.details.items;
items.forEach((item, index) => {
if (index + 2 < items.length) {
expect(item.size).toBeGreaterThanOrEqual(items[index + 1].size);
}
});
});

it('"Total" is the first tow', async () => {
const result = await ResourceSummaryAudit.audit(artifacts, context);
expect(result.details.items[0].resourceType).toBe('total');
});

it('"Third-party" is the last-row', async () => {
const result = await ResourceSummaryAudit.audit(artifacts, context);
const items = result.details.items;
expect(items[items.length - 1].resourceType).toBe('third-party');
});
});
});
Loading