Skip to content

Commit e6e8f5a

Browse files
committed
docs: update custom gatherer recipe for 10.0 (#14765)
1 parent c456c2c commit e6e8f5a

File tree

7 files changed

+85
-98
lines changed

7 files changed

+85
-98
lines changed

docs/recipes/custom-audit/custom-config.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,24 @@ export default {
88
// 1. Run your custom tests along with all the default Lighthouse tests.
99
extends: 'lighthouse:default',
1010

11-
// 2. Add gatherer to the default Lighthouse load ('pass') of the page.
12-
passes: [{
13-
passName: 'defaultPass',
14-
gatherers: [
15-
'searchable-gatherer',
16-
],
17-
}],
11+
// 2. Register new artifact with custom gatherer.
12+
artifacts: [
13+
{id: 'MemoryProfile', gatherer: 'memory-gatherer'},
14+
],
1815

1916
// 3. Add custom audit to the list of audits 'lighthouse:default' will run.
2017
audits: [
21-
'searchable-audit',
18+
'memory-audit',
2219
],
2320

24-
// 4. Create a new 'My site metrics' section in the default report for our results.
21+
// 4. Create a new 'My site audits' section in the default report for our results.
2522
categories: {
2623
mysite: {
27-
title: 'My site metrics',
28-
description: 'Metrics for our super awesome site',
24+
title: 'My site audits',
25+
description: 'Audits for our super awesome site',
2926
auditRefs: [
3027
// When we add more custom audits, `weight` controls how they're averaged together.
31-
{id: 'searchable-audit', weight: 1},
28+
{id: 'memory-audit', weight: 1},
3229
],
3330
},
3431
},
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Copyright 2023 The Lighthouse Authors. 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+
7+
import {Audit} from 'lighthouse';
8+
9+
const MAX_MEMORY_USAGE = 1_000_000;
10+
11+
/**
12+
* @fileoverview Tests that the memory usage is below a certain threshold.
13+
*/
14+
15+
class MemoryUsage extends Audit {
16+
static get meta() {
17+
return {
18+
id: 'memory-audit',
19+
title: 'Did not find any large memory usage',
20+
failureTitle: 'Found large memory usage',
21+
description: 'Detects if any memory sample was larger than 1 MB',
22+
23+
// The name of the custom gatherer class that provides input to this audit.
24+
requiredArtifacts: ['MemoryProfile'],
25+
};
26+
}
27+
28+
static audit(artifacts) {
29+
let largestMemoryUsage = 0;
30+
for (const sample of artifacts.MemoryProfile.samples) {
31+
if (sample.total > largestMemoryUsage) {
32+
largestMemoryUsage = sample.total;
33+
}
34+
}
35+
36+
return {
37+
numericValue: largestMemoryUsage,
38+
score: largestMemoryUsage > MAX_MEMORY_USAGE ? 0 : 1,
39+
};
40+
}
41+
}
42+
43+
export default MemoryUsage;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @license Copyright 2023 The Lighthouse Authors. 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+
7+
import {Gatherer} from 'lighthouse';
8+
9+
class MemoryProfile extends Gatherer {
10+
meta = {
11+
supportedModes: ['navigation', 'timespan'],
12+
};
13+
14+
async startInstrumentation(context) {
15+
const session = context.driver.defaultSession;
16+
await session.sendCommand('Memory.startSampling');
17+
}
18+
19+
async stopInstrumentation(context) {
20+
const session = context.driver.defaultSession;
21+
await session.sendCommand('Memory.stopSampling');
22+
}
23+
24+
async getArtifact(context) {
25+
const session = context.driver.defaultSession;
26+
const {profile} = await session.sendCommand('Memory.getSamplingProfile');
27+
28+
return profile;
29+
}
30+
}
31+
32+
export default MemoryProfile;

docs/recipes/custom-audit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"type": "module",
55
"scripts": {},
66
"devDependencies": {
7-
"lighthouse": "^9.5.0"
7+
"lighthouse": "file:../../../dist/lighthouse.tgz"
88
}
99
}

docs/recipes/custom-audit/searchable-audit.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

docs/recipes/custom-audit/searchable-gatherer.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

docs/recipes/custom-gatherer-puppeteer/custom-config.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ export default {
99
{id: 'CustomGatherer', gatherer: 'custom-gatherer'},
1010
],
1111

12-
navigations: [
13-
{
14-
id: 'default',
15-
artifacts: [
16-
'CustomGatherer',
17-
],
18-
},
19-
],
20-
2112
audits: [
2213
'custom-audit',
2314
],

0 commit comments

Comments
 (0)