-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: add largest-contentful-paint metric to JSON (#9706)
- Loading branch information
1 parent
9b41045
commit ac67b37
Showing
16 changed files
with
3,999 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
41 changes: 41 additions & 0 deletions
41
lighthouse-core/computed/metrics/largest-contentful-paint.js
This file contains 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,41 @@ | ||
/** | ||
* @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 ComputedMetric = require('./metric.js'); | ||
const LHError = require('../../lib/lh-error.js'); | ||
|
||
class LargestContentfulPaint extends ComputedMetric { | ||
/** | ||
* @param {LH.Artifacts.MetricComputationData} data | ||
* @param {LH.Audit.Context} context | ||
* @return {Promise<LH.Artifacts.LanternMetric>} | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
static computeSimulatedMetric(data, context) { | ||
throw new Error('Unimplemented'); | ||
} | ||
|
||
/** | ||
* @param {LH.Artifacts.MetricComputationData} data | ||
* @return {Promise<LH.Artifacts.Metric>} | ||
*/ | ||
static async computeObservedMetric(data) { | ||
const {traceOfTab} = data; | ||
if (!traceOfTab.timestamps.largestContentfulPaint) { | ||
throw new LHError(LHError.errors.NO_LCP); | ||
} | ||
|
||
return { | ||
// LCP established as existing, so cast | ||
timing: /** @type {number} */ (traceOfTab.timings.largestContentfulPaint), | ||
timestamp: traceOfTab.timestamps.largestContentfulPaint, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = makeComputedArtifact(LargestContentfulPaint); |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
42 changes: 42 additions & 0 deletions
42
lighthouse-core/test/computed/metrics/largest-contentful-paint-test.js
This file contains 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,42 @@ | ||
/** | ||
* @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 assert = require('assert'); | ||
|
||
const LargestContentfulPaint = require('../../../computed/metrics/largest-contentful-paint.js'); // eslint-disable-line max-len | ||
const trace = require('../../fixtures/traces/lcp-m79.json'); | ||
const devtoolsLog = require('../../fixtures/traces/lcp-m79.devtools.log.json'); | ||
const invalidTrace = require('../../fixtures/traces/progressive-app-m60.json'); | ||
const invalidDevtoolsLog = require('../../fixtures/traces/progressive-app-m60.devtools.log.json'); | ||
|
||
/* eslint-env jest */ | ||
|
||
describe('Metrics: LCP', () => { | ||
it('should error when computing a simulated value', async () => { | ||
const settings = {throttlingMethod: 'simulate'}; | ||
const context = {settings, computedCache: new Map()}; | ||
const resultPromise = LargestContentfulPaint.request({trace, devtoolsLog, settings}, context); | ||
await expect(resultPromise).rejects.toThrow(); | ||
}); | ||
|
||
it('should compute an observed value', async () => { | ||
const settings = {throttlingMethod: 'provided'}; | ||
const context = {settings, computedCache: new Map()}; | ||
const result = await LargestContentfulPaint.request({trace, devtoolsLog, settings}, context); | ||
|
||
assert.equal(Math.round(result.timing), 15024); | ||
assert.equal(result.timestamp, 1671236939268); | ||
}); | ||
|
||
it('should fail to compute an observed value for old trace', async () => { | ||
const settings = {throttlingMethod: 'provided'}; | ||
const context = {settings, computedCache: new Map()}; | ||
const resultPromise = LargestContentfulPaint.request( | ||
{trace: invalidTrace, devtoolsLog: invalidDevtoolsLog, settings}, context); | ||
await expect(resultPromise).rejects.toThrow('NO_LCP'); | ||
}); | ||
}); |
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
3,716 changes: 3,716 additions & 0 deletions
3,716
lighthouse-core/test/fixtures/traces/lcp-m79.json
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.