Skip to content

Commit 8537a47

Browse files
khempeniusbrendankenny
authored andcommitted
core(lightwallet): add resource-summary computed artifact (#8709)
1 parent 93a8d8c commit 8537a47

File tree

3 files changed

+183
-2
lines changed

3 files changed

+183
-2
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @license Copyright 2019 Google Inc. All Rights Reserved.
3+
* 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
4+
* 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.
5+
*/
6+
'use strict';
7+
8+
const makeComputedArtifact = require('./computed-artifact.js');
9+
const NetworkRecords = require('./network-records.js');
10+
const MainResource = require('./main-resource.js');
11+
const URL = require('../lib/url-shim.js');
12+
13+
/** @typedef {{count: number, size: number}} ResourceEntry */
14+
class ResourceSummary {
15+
/**
16+
* @param {LH.Artifacts.NetworkRequest} record
17+
* @return {LH.Budget.ResourceType}
18+
*/
19+
static determineResourceType(record) {
20+
if (!record.resourceType) return 'other';
21+
/** @type {Partial<Record<LH.Crdp.Page.ResourceType, LH.Budget.ResourceType>>} */
22+
const requestToResourceType = {
23+
'Stylesheet': 'stylesheet',
24+
'Image': 'image',
25+
'Media': 'media',
26+
'Font': 'font',
27+
'Script': 'script',
28+
'Document': 'document',
29+
};
30+
return requestToResourceType[record.resourceType] || 'other';
31+
}
32+
33+
/**
34+
* @param {Array<LH.Artifacts.NetworkRequest>} networkRecords
35+
* @param {string} mainResourceURL
36+
* @return {Record<LH.Budget.ResourceType,ResourceEntry>}
37+
*/
38+
static summarize(networkRecords, mainResourceURL) {
39+
/** @type {Record<LH.Budget.ResourceType,ResourceEntry>} */
40+
const resourceSummary = {
41+
'stylesheet': {count: 0, size: 0},
42+
'image': {count: 0, size: 0},
43+
'media': {count: 0, size: 0},
44+
'font': {count: 0, size: 0},
45+
'script': {count: 0, size: 0},
46+
'document': {count: 0, size: 0},
47+
'other': {count: 0, size: 0},
48+
'total': {count: 0, size: 0},
49+
'third-party': {count: 0, size: 0},
50+
};
51+
52+
for (const record of networkRecords) {
53+
const type = this.determineResourceType(record);
54+
resourceSummary[type].count++;
55+
resourceSummary[type].size += record.transferSize;
56+
57+
resourceSummary.total.count++;
58+
resourceSummary.total.size += record.transferSize;
59+
60+
// Ignores subdomains: i.e. blog.example.com & example.com would match
61+
if (!URL.rootDomainsMatch(record.url, mainResourceURL)) {
62+
resourceSummary['third-party'].count++;
63+
resourceSummary['third-party'].size += record.transferSize;
64+
}
65+
}
66+
return resourceSummary;
67+
}
68+
69+
/**
70+
* @param {{URL: LH.Artifacts['URL'], devtoolsLog: LH.DevtoolsLog}} data
71+
* @param {LH.Audit.Context} context
72+
* @return {Promise<Record<LH.Budget.ResourceType,ResourceEntry>>}
73+
*/
74+
static async compute_(data, context) {
75+
const [networkRecords, mainResource] = await Promise.all([
76+
NetworkRecords.request(data.devtoolsLog, context),
77+
MainResource.request(data, context),
78+
]);
79+
80+
return ResourceSummary.summarize(networkRecords, mainResource.url);
81+
}
82+
}
83+
84+
module.exports = makeComputedArtifact(ResourceSummary);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @license Copyright 2019 Google Inc. All Rights Reserved.
3+
* 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
4+
* 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.
5+
*/
6+
'use strict';
7+
8+
const ComputedResourceSummary = require('../../computed/resource-summary.js');
9+
const assert = require('assert');
10+
const networkRecordsToDevtoolsLog = require('../network-records-to-devtools-log.js');
11+
12+
/* eslint-env jest */
13+
14+
function mockArtifacts(networkRecords) {
15+
return {
16+
devtoolsLog: networkRecordsToDevtoolsLog(networkRecords),
17+
URL: {requestedUrl: networkRecords[0].url, finalUrl: networkRecords[0].url},
18+
};
19+
}
20+
21+
describe('Resource summary computed', () => {
22+
let artifacts;
23+
let context;
24+
beforeEach(() => {
25+
artifacts = mockArtifacts([
26+
{url: 'http://example.com/file.html', resourceType: 'Document', transferSize: 30},
27+
{url: 'http://example.com/app.js', resourceType: 'Script', transferSize: 10},
28+
{url: 'http://cdn.example.com/script.js', resourceType: 'Script', transferSize: 50},
29+
{url: 'http://third-party.com/file.jpg', resourceType: 'Image', transferSize: 70},
30+
]);
31+
context = {computedCache: new Map()};
32+
});
33+
34+
it('includes all resource types, regardless of whether page contains them', async () => {
35+
const result = await ComputedResourceSummary.request(artifacts, context);
36+
assert.equal(Object.keys(result).length, 9);
37+
});
38+
39+
it('sets size and count correctly', async () => {
40+
const result = await ComputedResourceSummary.request(artifacts, context);
41+
assert.equal(result.script.count, 2);
42+
assert.equal(result.script.size, 10 + 50);
43+
});
44+
45+
it('sets "total" resource metrics correctly', async () => {
46+
const result = await ComputedResourceSummary.request(artifacts, context);
47+
assert.equal(result.total.count, 4);
48+
assert.equal(result.total.size, 30 + 10 + 50 + 70);
49+
});
50+
51+
it('sets "other" resource metrics correctly', async () => {
52+
// networkRecordsToDevToolsLog errors with an 'other' resource type, so this test does not use it
53+
const networkRecords = [
54+
{url: 'http://example.com/file.html', resourceType: 'Document', transferSize: 30},
55+
{url: 'http://third-party.com/another-file.html', resourceType: 'manifest', transferSize: 50},
56+
];
57+
58+
const result = ComputedResourceSummary.summarize(networkRecords, networkRecords[0].url);
59+
assert.equal(result.other.count, 1);
60+
assert.equal(result.other.size, 50);
61+
});
62+
63+
describe('determining third-party resources', () => {
64+
it('with a third-party resource', async () => {
65+
artifacts = mockArtifacts([
66+
{url: 'http://example.com/file.html', resourceType: 'Document', transferSize: 30},
67+
{url: 'http://third-party.com/another-file.html', resourceType: 'Document', transferSize: 50},
68+
]);
69+
70+
const result = await ComputedResourceSummary.request(artifacts, context);
71+
assert.equal(result['third-party'].count, 1);
72+
assert.equal(result['third-party'].size, 50);
73+
});
74+
75+
it('with a first-party resource', async () => {
76+
artifacts = mockArtifacts([
77+
{url: 'http://example.com/file.html', resourceType: 'Document', transferSize: 30},
78+
{url: 'http://example.com/another-file.html', resourceType: 'Document', transferSize: 50},
79+
]);
80+
81+
const result = await ComputedResourceSummary.request(artifacts, context);
82+
assert.equal(result['third-party'].count, 0);
83+
assert.equal(result['third-party'].size, 0);
84+
});
85+
86+
it('with a first-party resource loaded from a subdomain', async () => {
87+
artifacts = mockArtifacts([
88+
{url: 'http://example.com/file.html', resourceType: 'Document', transferSize: 30},
89+
{url: 'http://blog.example.com/file.html', resourceType: 'Document', transferSize: 50},
90+
]);
91+
92+
const result = await ComputedResourceSummary.request(artifacts, context);
93+
assert.equal(result['third-party'].count, 0);
94+
assert.equal(result['third-party'].size, 0);
95+
});
96+
});
97+
});

types/budget.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ declare global {
3939
/** Supported timing metrics. */
4040
export type TimingMetric = 'first-contentful-paint' | 'first-cpu-idle' | 'interactive' | 'first-meaningful-paint' | 'estimated-input-latency';
4141

42-
/** Supported resource types. */
43-
export type ResourceType = 'stylesheet' | 'image' | 'media' | 'font' | 'script' | 'document' | 'other';
42+
/** Supported values for the resourceType property of a ResourceBudget. */
43+
export type ResourceType = 'stylesheet' | 'image' | 'media' | 'font' | 'script' | 'document' | 'other' | 'total' | 'third-party';
4444
}
4545
}
4646
}

0 commit comments

Comments
 (0)