[go: nahoru, domu]

Skip to content

Commit

Permalink
new_audit: add third party entity summary (#9067)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Jun 25, 2019
1 parent d3d3828 commit e780ef2
Show file tree
Hide file tree
Showing 14 changed files with 465 additions and 19 deletions.
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 @@ -132,6 +132,9 @@ Object {
Object {
"path": "resource-summary",
},
Object {
"path": "third-party-summary",
},
Object {
"path": "manual/pwa-cross-browser",
},
Expand Down Expand Up @@ -837,6 +840,11 @@ Object {
"id": "resource-summary",
"weight": 0,
},
Object {
"group": "diagnostics",
"id": "third-party-summary",
"weight": 0,
},
Object {
"id": "network-requests",
"weight": 0,
Expand Down
21 changes: 15 additions & 6 deletions lighthouse-core/audits/bootup-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ class BootupTime extends Audit {
return urls;
}

/**
* @param {LH.Artifacts.TaskNode} task
* @param {Set<string>} jsURLs
* @return {string}
*/
static getAttributableURLForTask(task, jsURLs) {
const jsURL = task.attributableURLs.find(url => jsURLs.has(url));
const fallbackURL = task.attributableURLs[0];
let attributableURL = jsURL || fallbackURL;
// If we can't find what URL was responsible for this execution, just attribute it to the root page.
if (!attributableURL || attributableURL === 'about:blank') attributableURL = 'Other';
return attributableURL;
}

/**
* @param {LH.Artifacts.TaskNode[]} tasks
* @param {Set<string>} jsURLs
Expand All @@ -87,12 +101,7 @@ class BootupTime extends Audit {
const result = new Map();

for (const task of tasks) {
const jsURL = task.attributableURLs.find(url => jsURLs.has(url));
const fallbackURL = task.attributableURLs[0];
let attributableURL = jsURL || fallbackURL;
// If we can't find what URL was responsible for this execution, just attribute it to the root page.
if (!attributableURL || attributableURL === 'about:blank') attributableURL = 'Other';

const attributableURL = BootupTime.getAttributableURLForTask(task, jsURLs);
const timingByGroupId = result.get(attributableURL) || {};
const originalTime = timingByGroupId[task.group.id] || 0;
timingByGroupId[task.group.id] = originalTime + task.selfTime;
Expand Down
167 changes: 167 additions & 0 deletions lighthouse-core/audits/third-party-summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/**
* @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 thirdPartyWeb = require('third-party-web/httparchive-nostats-subset');

const Audit = require('./audit.js');
const BootupTime = require('./bootup-time.js');
const i18n = require('../lib/i18n/i18n.js');
const NetworkRecords = require('../computed/network-records.js');
const MainThreadTasks = require('../computed/main-thread-tasks.js');

const UIStrings = {
/** Title of a Lighthouse audit that identifies the code on the page that the user doesn't control. This is shown in a list of audits that Lighthouse generates. */
title: 'Third-Party Usage',
/** Description of a Lighthouse audit that identifies the code on the page that the user doesn't control. This is displayed after a user expands the section to see more. No character length limits. 'Learn More' becomes link text to additional documentation. */
description: 'Third-party code can significantly impact load performance. ' +
'Limit the number of redundant third-party providers and try to load third-party code after ' +
'your page has primarily finished loading. [Learn more](https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/loading-third-party-javascript/).',
/** Label for a table column that displays the name of a third-party provider that potentially links to their website. */
columnThirdParty: 'Third-Party',
/** Label for a table column that displays how much time each row spent executing on the main thread, entries will be the number of milliseconds spent. */
columnMainThreadTime: 'Main Thread Time',
/** Summary text for the result of a Lighthouse audit that identifies the code on the page that the user doesn't control. This text summarizes the number of distinct entities that were found on the page. */
displayValue: `{itemCount, plural,
=1 {1 Third-Party Found}
other {# Third-Parties Found}
}`,
};

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

/** @typedef {import("third-party-web").IEntity} ThirdPartyEntity */

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

/**
* `third-party-web` throws when the passed in string doesn't appear to have any domain whatsoever.
* We pass in some not-so-url-like things, so make the dependent-code simpler by making this call safe.
* @param {string} url
* @return {ThirdPartyEntity|undefined}
*/
static getEntitySafe(url) {
try {
return thirdPartyWeb.getEntity(url);
} catch (_) {
return undefined;
}
}


/**
*
* @param {Array<LH.Artifacts.NetworkRequest>} networkRecords
* @param {Array<LH.Artifacts.TaskNode>} mainThreadTasks
* @param {number} cpuMultiplier
* @return {Map<ThirdPartyEntity, {mainThreadTime: number, transferSize: number}>}
*/
static getSummaryByEntity(networkRecords, mainThreadTasks, cpuMultiplier) {
/** @type {Map<ThirdPartyEntity, {mainThreadTime: number, transferSize: number}>} */
const entities = new Map();

for (const request of networkRecords) {
const entity = ThirdPartySummary.getEntitySafe(request.url);
if (!entity) continue;

const entityStats = entities.get(entity) || {mainThreadTime: 0, transferSize: 0};
entityStats.transferSize += request.transferSize;
entities.set(entity, entityStats);
}

const jsURLs = BootupTime.getJavaScriptURLs(networkRecords);

for (const task of mainThreadTasks) {
const attributeableURL = BootupTime.getAttributableURLForTask(task, jsURLs);
const entity = ThirdPartySummary.getEntitySafe(attributeableURL);
if (!entity) continue;

const entityStats = entities.get(entity) || {mainThreadTime: 0, transferSize: 0};
entityStats.mainThreadTime += task.selfTime * cpuMultiplier;
entities.set(entity, entityStats);
}

return entities;
}

/**
* @param {LH.Artifacts} artifacts
* @param {LH.Audit.Context} context
* @return {Promise<LH.Audit.Product>}
*/
static async audit(artifacts, context) {
const settings = context.settings || {};
const trace = artifacts.traces[Audit.DEFAULT_PASS];
const devtoolsLog = artifacts.devtoolsLogs[Audit.DEFAULT_PASS];
const networkRecords = await NetworkRecords.request(devtoolsLog, context);
const tasks = await MainThreadTasks.request(trace, context);
const multiplier = settings.throttlingMethod === 'simulate' ?
settings.throttling.cpuSlowdownMultiplier : 1;

const summaryByEntity = ThirdPartySummary.getSummaryByEntity(networkRecords, tasks, multiplier);

const summary = {wastedBytes: 0, wastedMs: 0};

// Sort by a combined measure of bytes + main thread time.
// 1KB ~= 1 ms
/** @param {{transferSize: number, mainThreadTime: number}} stats */
const computeSortValue = stats => stats.transferSize / 1024 + stats.mainThreadTime;

const results = Array.from(summaryByEntity.entries())
.map(([entity, stats]) => {
summary.wastedBytes += stats.transferSize;
summary.wastedMs += stats.mainThreadTime;

return {
entity: /** @type {LH.Audit.Details.LinkValue} */ ({
type: 'link',
text: entity.name,
url: entity.homepage || '',
}),
transferSize: stats.transferSize,
mainThreadTime: stats.mainThreadTime,
};
})
.sort((a, b) => computeSortValue(b) - computeSortValue(a));

/** @type {LH.Audit.Details.Table['headings']} */
const headings = [
{key: 'entity', itemType: 'link', text: str_(UIStrings.columnThirdParty)},
{key: 'transferSize', granularity: 1, itemType: 'bytes',
text: str_(i18n.UIStrings.columnSize)},
{key: 'mainThreadTime', granularity: 1, itemType: 'ms',
text: str_(UIStrings.columnMainThreadTime)},
];

if (!results.length) {
return {
score: 1,
notApplicable: true,
};
}

return {
score: Number(results.length === 0),
displayValue: str_(UIStrings.displayValue, {itemCount: results.length}),
details: Audit.makeTableDetails(headings, results, summary),
};
}
}

module.exports = ThirdPartySummary;
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 @@ -199,6 +199,7 @@ const defaultConfig = {
'offline-start-url',
'performance-budget',
'resource-summary',
'third-party-summary',
'manual/pwa-cross-browser',
'manual/pwa-page-transitions',
'manual/pwa-each-page-has-url',
Expand Down Expand Up @@ -391,6 +392,7 @@ const defaultConfig = {
{id: 'font-display', weight: 0, group: 'diagnostics'},
{id: 'performance-budget', weight: 0, group: 'budgets'},
{id: 'resource-summary', weight: 0, group: 'diagnostics'},
{id: 'third-party-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
20 changes: 20 additions & 0 deletions lighthouse-core/lib/i18n/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,26 @@
"message": "Tap targets are sized appropriately",
"description": "Title of a Lighthouse audit that provides detail on whether tap targets (like buttons and links) on a page are big enough so they can easily be tapped on a mobile device. This descriptive title is shown when tap targets are easy to tap on."
},
"lighthouse-core/audits/third-party-summary.js | columnMainThreadTime": {
"message": "Main Thread Time",
"description": "Label for a table column that displays how much time each row spent executing on the main thread, entries will be the number of milliseconds spent."
},
"lighthouse-core/audits/third-party-summary.js | columnThirdParty": {
"message": "Third-Party",
"description": "Label for a table column that displays the name of a third-party provider that potentially links to their website."
},
"lighthouse-core/audits/third-party-summary.js | description": {
"message": "Third-party code can significantly impact load performance. Limit the number of redundant third-party providers and try to load third-party code after your page has primarily finished loading. [Learn more](https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/loading-third-party-javascript/).",
"description": "Description of a Lighthouse audit that identifies the code on the page that the user doesn't control. This is displayed after a user expands the section to see more. No character length limits. 'Learn More' becomes link text to additional documentation."
},
"lighthouse-core/audits/third-party-summary.js | displayValue": {
"message": "{itemCount, plural,\n =1 {1 Third-Party Found}\n other {# Third-Parties Found}\n }",
"description": "Summary text for the result of a Lighthouse audit that identifies the code on the page that the user doesn't control. This text summarizes the number of distinct entities that were found on the page."
},
"lighthouse-core/audits/third-party-summary.js | title": {
"message": "Third-Party Usage",
"description": "Title of a Lighthouse audit that identifies the code on the page that the user doesn't control. This is shown in a list of audits that Lighthouse generates."
},
"lighthouse-core/audits/time-to-first-byte.js | description": {
"message": "Time To First Byte identifies the time at which your server sends a response. [Learn more](https://developers.google.com/web/tools/lighthouse/audits/ttfb).",
"description": "Description of a Lighthouse audit that tells the user *why* they should reduce the amount of time it takes their server to start responding to requests. 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
10 changes: 7 additions & 3 deletions lighthouse-core/report/html/renderer/details-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,13 @@ class DetailsRenderer {
*/
_renderLink(details) {
const allowedProtocols = ['https:', 'http:'];
const url = new URL(details.url);
if (!allowedProtocols.includes(url.protocol)) {
// Fall back to just the link text if protocol not allowed.
let url;
try {
url = new URL(details.url);
} catch (_) {}

if (!url || !allowedProtocols.includes(url.protocol)) {
// Fall back to just the link text if invalid or protocol not allowed.
return this._renderText(details.text);
}

Expand Down
76 changes: 76 additions & 0 deletions lighthouse-core/test/audits/third-party-summary-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @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 ThirdPartySummary = require('../../audits/third-party-summary.js');
const networkRecordsToDevtoolsLog = require('../network-records-to-devtools-log.js');

const pwaTrace = require('../fixtures/traces/progressive-app-m60.json');
const pwaDevtoolsLog = require('../fixtures/traces/progressive-app-m60.devtools.log.json');
const noThirdPartyTrace = require('../fixtures/traces/no-tracingstarted-m74.json');

/* eslint-env jest */
describe('Third party summary', () => {
it('surface the discovered third parties', async () => {
const artifacts = {
devtoolsLogs: {defaultPass: pwaDevtoolsLog},
traces: {defaultPass: pwaTrace},
};

const results = await ThirdPartySummary.audit(artifacts, {computedCache: new Map()});

expect(results.displayValue).toBeDisplayString('2 Third-Parties Found');
expect(results.details.items).toEqual([
{
entity: {
text: 'Google Tag Manager',
type: 'link',
url: 'https://marketingplatform.google.com/about/tag-manager/',
},
mainThreadTime: 104.70300000000002,
transferSize: 30827,
},
{
entity: {
text: 'Google Analytics',
type: 'link',
url: 'https://www.google.com/analytics/analytics/',
},
mainThreadTime: 87.576,
transferSize: 20913,
},
]);
});

it('account for simulated throttling', async () => {
const artifacts = {
devtoolsLogs: {defaultPass: pwaDevtoolsLog},
traces: {defaultPass: pwaTrace},
};

const settings = {throttlingMethod: 'simulate', throttling: {cpuSlowdownMultiplier: 4}};
const results = await ThirdPartySummary.audit(artifacts, {computedCache: new Map(), settings});

expect(results.details.items).toHaveLength(2);
expect(Math.round(results.details.items[0].mainThreadTime)).toEqual(419);
expect(Math.round(results.details.items[1].mainThreadTime)).toEqual(350);
});

it('be not applicable when no third parties are present', async () => {
const artifacts = {
devtoolsLogs: {defaultPass: networkRecordsToDevtoolsLog([{url: 'chrome://version'}])},
traces: {defaultPass: noThirdPartyTrace},
};

const settings = {throttlingMethod: 'simulate', throttling: {cpuSlowdownMultiplier: 4}};
const results = await ThirdPartySummary.audit(artifacts, {computedCache: new Map(), settings});

expect(results).toEqual({
score: 1,
notApplicable: true,
});
});
});
20 changes: 20 additions & 0 deletions lighthouse-core/test/report/html/renderer/details-renderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,26 @@ describe('DetailsRenderer', () => {
assert.equal(linkEl.textContent, linkText);
});

it('renders link value as text if URL is invalid', () => {
const linkText = 'Invalid Link';
const linkUrl = 'link nonsense';
const link = {
type: 'link',
text: linkText,
url: linkUrl,
};
const details = {
type: 'table',
headings: [{key: 'content', itemType: 'link', text: 'Heading'}],
items: [{content: link}],
};

const el = renderer.render(details);
const linkEl = el.querySelector('td.lh-table-column--link > .lh-text');
assert.equal(linkEl.localName, 'div');
assert.equal(linkEl.textContent, linkText);
});

it('renders node values', () => {
const node = {
type: 'node',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('PerfCategoryRenderer', () => {

it('renders the failing diagnostics', () => {
const categoryDOM = renderer.render(category, sampleResults.categoryGroups);
const diagnosticSection = categoryDOM.querySelectorAll('.lh-category > .lh-audit-group')[2];
const diagnosticSection = categoryDOM.querySelectorAll('.lh-category > .lh-audit-group')[3];

const diagnosticAudits = category.auditRefs.filter(audit => audit.group === 'diagnostics' &&
!Util.showAsPassed(audit.result));
Expand Down
Loading

0 comments on commit e780ef2

Please sign in to comment.