-
Notifications
You must be signed in to change notification settings - Fork 9.6k
lightwallet: add 'computed/resource-summary' #8709
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
Merged
brendankenny
merged 1 commit into
GoogleChrome:master
from
khempenius:lightwallet_computed
Apr 30, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /** | ||
| * @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 makeComputedArtifact = require('./computed-artifact.js'); | ||
| const NetworkRecords = require('./network-records.js'); | ||
| const MainResource = require('./main-resource.js'); | ||
| const URL = require('../lib/url-shim.js'); | ||
|
|
||
| /** @typedef {{count: number, size: number}} ResourceEntry */ | ||
| class ResourceSummary { | ||
| /** | ||
| * @param {LH.Artifacts.NetworkRequest} record | ||
| * @return {LH.Budget.ResourceType} | ||
| */ | ||
| static determineResourceType(record) { | ||
| if (!record.resourceType) return 'other'; | ||
| /** @type {Partial<Record<LH.Crdp.Page.ResourceType, LH.Budget.ResourceType>>} */ | ||
| const requestToResourceType = { | ||
| 'Stylesheet': 'stylesheet', | ||
| 'Image': 'image', | ||
| 'Media': 'media', | ||
| 'Font': 'font', | ||
| 'Script': 'script', | ||
| 'Document': 'document', | ||
| }; | ||
| return requestToResourceType[record.resourceType] || 'other'; | ||
| } | ||
|
|
||
| /** | ||
| * @param {Array<LH.Artifacts.NetworkRequest>} networkRecords | ||
| * @param {string} mainResourceURL | ||
| * @return {Record<LH.Budget.ResourceType,ResourceEntry>} | ||
| */ | ||
| static summarize(networkRecords, mainResourceURL) { | ||
| /** @type {Record<LH.Budget.ResourceType,ResourceEntry>} */ | ||
| const resourceSummary = { | ||
| 'stylesheet': {count: 0, size: 0}, | ||
| 'image': {count: 0, size: 0}, | ||
| 'media': {count: 0, size: 0}, | ||
| 'font': {count: 0, size: 0}, | ||
| 'script': {count: 0, size: 0}, | ||
| 'document': {count: 0, size: 0}, | ||
| 'other': {count: 0, size: 0}, | ||
| 'total': {count: 0, size: 0}, | ||
| 'third-party': {count: 0, size: 0}, | ||
| }; | ||
|
|
||
| for (const record of networkRecords) { | ||
| const type = this.determineResourceType(record); | ||
| resourceSummary[type].count++; | ||
| resourceSummary[type].size += record.transferSize; | ||
|
|
||
| resourceSummary.total.count++; | ||
| resourceSummary.total.size += record.transferSize; | ||
|
|
||
| // Ignores subdomains: i.e. blog.example.com & example.com would match | ||
| if (!URL.rootDomainsMatch(record.url, mainResourceURL)) { | ||
| resourceSummary['third-party'].count++; | ||
| resourceSummary['third-party'].size += record.transferSize; | ||
| } | ||
| } | ||
| return resourceSummary; | ||
| } | ||
|
|
||
| /** | ||
| * @param {{URL: LH.Artifacts['URL'], devtoolsLog: LH.DevtoolsLog}} data | ||
| * @param {LH.Audit.Context} context | ||
| * @return {Promise<Record<LH.Budget.ResourceType,ResourceEntry>>} | ||
| */ | ||
| static async compute_(data, context) { | ||
| const [networkRecords, mainResource] = await Promise.all([ | ||
| NetworkRecords.request(data.devtoolsLog, context), | ||
| MainResource.request(data, context), | ||
| ]); | ||
|
|
||
| return ResourceSummary.summarize(networkRecords, mainResource.url); | ||
| } | ||
| } | ||
|
|
||
| module.exports = makeComputedArtifact(ResourceSummary); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| /** | ||
| * @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 ComputedResourceSummary = require('../../computed/resource-summary.js'); | ||
| const assert = require('assert'); | ||
| const networkRecordsToDevtoolsLog = require('../network-records-to-devtools-log.js'); | ||
|
|
||
| /* eslint-env jest */ | ||
|
|
||
| function mockArtifacts(networkRecords) { | ||
| return { | ||
| devtoolsLog: networkRecordsToDevtoolsLog(networkRecords), | ||
| URL: {requestedUrl: networkRecords[0].url, finalUrl: networkRecords[0].url}, | ||
| }; | ||
| } | ||
|
|
||
| describe('Resource summary computed', () => { | ||
| let artifacts; | ||
| let context; | ||
| beforeEach(() => { | ||
| artifacts = mockArtifacts([ | ||
| {url: 'http://example.com/file.html', resourceType: 'Document', transferSize: 30}, | ||
| {url: 'http://example.com/app.js', resourceType: 'Script', transferSize: 10}, | ||
khempenius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| {url: 'http://cdn.example.com/script.js', resourceType: 'Script', transferSize: 50}, | ||
| {url: 'http://third-party.com/file.jpg', resourceType: 'Image', transferSize: 70}, | ||
| ]); | ||
| context = {computedCache: new Map()}; | ||
| }); | ||
|
|
||
| it('includes all resource types, regardless of whether page contains them', async () => { | ||
| const result = await ComputedResourceSummary.request(artifacts, context); | ||
| assert.equal(Object.keys(result).length, 9); | ||
| }); | ||
|
|
||
| it('sets size and count correctly', async () => { | ||
| const result = await ComputedResourceSummary.request(artifacts, context); | ||
| assert.equal(result.script.count, 2); | ||
| assert.equal(result.script.size, 10 + 50); | ||
| }); | ||
|
|
||
| it('sets "total" resource metrics correctly', async () => { | ||
| const result = await ComputedResourceSummary.request(artifacts, context); | ||
| assert.equal(result.total.count, 4); | ||
| assert.equal(result.total.size, 30 + 10 + 50 + 70); | ||
| }); | ||
|
|
||
| it('sets "other" resource metrics correctly', async () => { | ||
| // networkRecordsToDevToolsLog errors with an 'other' resource type, so this test does not use it | ||
| const networkRecords = [ | ||
| {url: 'http://example.com/file.html', resourceType: 'Document', transferSize: 30}, | ||
| {url: 'http://third-party.com/another-file.html', resourceType: 'manifest', transferSize: 50}, | ||
| ]; | ||
|
|
||
| const result = ComputedResourceSummary.summarize(networkRecords, networkRecords[0].url); | ||
| assert.equal(result.other.count, 1); | ||
| assert.equal(result.other.size, 50); | ||
| }); | ||
|
|
||
| describe('determining third-party resources', () => { | ||
| it('with a third-party resource', async () => { | ||
| artifacts = mockArtifacts([ | ||
| {url: 'http://example.com/file.html', resourceType: 'Document', transferSize: 30}, | ||
| {url: 'http://third-party.com/another-file.html', resourceType: 'Document', transferSize: 50}, | ||
| ]); | ||
|
|
||
| const result = await ComputedResourceSummary.request(artifacts, context); | ||
| assert.equal(result['third-party'].count, 1); | ||
| assert.equal(result['third-party'].size, 50); | ||
| }); | ||
|
|
||
| it('with a first-party resource', async () => { | ||
| artifacts = mockArtifacts([ | ||
| {url: 'http://example.com/file.html', resourceType: 'Document', transferSize: 30}, | ||
| {url: 'http://example.com/another-file.html', resourceType: 'Document', transferSize: 50}, | ||
| ]); | ||
|
|
||
| const result = await ComputedResourceSummary.request(artifacts, context); | ||
| assert.equal(result['third-party'].count, 0); | ||
| assert.equal(result['third-party'].size, 0); | ||
| }); | ||
|
|
||
| it('with a first-party resource loaded from a subdomain', async () => { | ||
khempenius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| artifacts = mockArtifacts([ | ||
| {url: 'http://example.com/file.html', resourceType: 'Document', transferSize: 30}, | ||
| {url: 'http://blog.example.com/file.html', resourceType: 'Document', transferSize: 50}, | ||
| ]); | ||
|
|
||
| const result = await ComputedResourceSummary.request(artifacts, context); | ||
| assert.equal(result['third-party'].count, 0); | ||
| assert.equal(result['third-party'].size, 0); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.