|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | 3 | // found in the LICENSE file. |
4 | 4 |
|
| 5 | +import type {RecursivePartial} from '../../../core/platform/TypescriptUtilities.js'; |
| 6 | +import * as Protocol from '../../../generated/protocol.js'; |
5 | 7 | import {describeWithEnvironment} from '../../../testing/EnvironmentHelpers.js'; |
6 | 8 | import {getFirstOrError, processTrace} from '../../../testing/InsightHelpers.js'; |
| 9 | +import type * as Types from '../types/types.js'; |
7 | 10 |
|
8 | 11 | import * as Insights from './insights.js'; |
9 | 12 |
|
10 | | -const {calculateMetricWeightsForSorting} = Insights.Common; |
| 13 | +const {calculateMetricWeightsForSorting, estimateCompressedContentSize} = Insights.Common; |
11 | 14 |
|
12 | 15 | describeWithEnvironment('Common', function() { |
13 | 16 | describe('calculateMetricWeightsForSorting', () => { |
@@ -53,4 +56,69 @@ describeWithEnvironment('Common', function() { |
53 | 56 | assert.deepEqual(weights, {lcp: 0.48649783990559314, inp: 0.48649783990559314, cls: 0.027004320188813675}); |
54 | 57 | }); |
55 | 58 | }); |
| 59 | + |
| 60 | + describe('#estimateCompressedContentSize', () => { |
| 61 | + const estimate = estimateCompressedContentSize; |
| 62 | + const encoding = [{name: 'Content-Encoding', value: 'gzip'}]; |
| 63 | + const makeRequest = (partial: { |
| 64 | + transferSize?: number, |
| 65 | + resourceSize?: number, resourceType: Protocol.Network.ResourceType, |
| 66 | + responseHeaders?: Array<{name: string, value: string}>, |
| 67 | + }): Types.Events.SyntheticNetworkRequest => { |
| 68 | + const request: RecursivePartial<Types.Events.SyntheticNetworkRequest> = { |
| 69 | + args: { |
| 70 | + data: { |
| 71 | + encodedDataLength: partial.transferSize ?? 0, |
| 72 | + decodedBodyLength: partial.resourceSize ?? 0, |
| 73 | + resourceType: partial.resourceType, |
| 74 | + responseHeaders: partial.responseHeaders ?? [], |
| 75 | + } |
| 76 | + }, |
| 77 | + }; |
| 78 | + return request as Types.Events.SyntheticNetworkRequest; |
| 79 | + }; |
| 80 | + |
| 81 | + it('should estimate by resource type compression ratio when no network info available', () => { |
| 82 | + assert.strictEqual(estimate(undefined, 1000, Protocol.Network.ResourceType.Stylesheet), 200); |
| 83 | + assert.strictEqual(estimate(undefined, 1000, Protocol.Network.ResourceType.Script), 330); |
| 84 | + assert.strictEqual(estimate(undefined, 1000, Protocol.Network.ResourceType.Document), 330); |
| 85 | + assert.strictEqual(estimate(undefined, 1000, Protocol.Network.ResourceType.Other), 500); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return transferSize when asset matches and is encoded', () => { |
| 89 | + const resourceType = Protocol.Network.ResourceType.Stylesheet; |
| 90 | + const request = makeRequest({transferSize: 1234, resourceType, responseHeaders: encoding}); |
| 91 | + const result = estimate(request, 10000, resourceType); |
| 92 | + assert.strictEqual(result, 1234); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should return resourceSize when asset matches and is not encoded', () => { |
| 96 | + const resourceType = Protocol.Network.ResourceType.Stylesheet; |
| 97 | + const request = makeRequest({transferSize: 1235, resourceSize: 1234, resourceType, responseHeaders: []}); |
| 98 | + const result = estimate(request, 10000, resourceType); |
| 99 | + assert.strictEqual(result, 1234); |
| 100 | + }); |
| 101 | + |
| 102 | + // Ex: JS script embedded in HTML response. |
| 103 | + it('should estimate by network compression ratio when asset does not match', () => { |
| 104 | + const resourceType = Protocol.Network.ResourceType.Other; |
| 105 | + const request = makeRequest({resourceSize: 2000, transferSize: 1000, resourceType, responseHeaders: encoding}); |
| 106 | + const result = estimate(request, 100, Protocol.Network.ResourceType.Script); |
| 107 | + assert.strictEqual(result, 50); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should not error when missing resource size', () => { |
| 111 | + const resourceType = Protocol.Network.ResourceType.Other; |
| 112 | + const request = makeRequest({transferSize: 1000, resourceType, responseHeaders: []}); |
| 113 | + const result = estimate(request, 100, Protocol.Network.ResourceType.Script); |
| 114 | + assert.strictEqual(result, 100); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should not error when resource size is 0', () => { |
| 118 | + const resourceType = Protocol.Network.ResourceType.Other; |
| 119 | + const request = makeRequest({transferSize: 1000, resourceSize: 0, resourceType, responseHeaders: []}); |
| 120 | + const result = estimate(request, 100, Protocol.Network.ResourceType.Script); |
| 121 | + assert.strictEqual(result, 100); |
| 122 | + }); |
| 123 | + }); |
56 | 124 | }); |
0 commit comments