Skip to content

docs: update custom gatherer recipe for 10.0 #14765

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
merged 2 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions docs/recipes/custom-audit/custom-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,24 @@ export default {
// 1. Run your custom tests along with all the default Lighthouse tests.
extends: 'lighthouse:default',

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

// 3. Add custom audit to the list of audits 'lighthouse:default' will run.
audits: [
'searchable-audit',
'memory-audit',
],

// 4. Create a new 'My site metrics' section in the default report for our results.
// 4. Create a new 'My site audits' section in the default report for our results.
categories: {
mysite: {
title: 'My site metrics',
description: 'Metrics for our super awesome site',
title: 'My site audits',
description: 'Audits for our super awesome site',
auditRefs: [
// When we add more custom audits, `weight` controls how they're averaged together.
{id: 'searchable-audit', weight: 1},
{id: 'memory-audit', weight: 1},
],
},
},
Expand Down
43 changes: 43 additions & 0 deletions docs/recipes/custom-audit/memory-audit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @license Copyright 2023 The Lighthouse Authors. 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.
*/

import {Audit} from 'lighthouse';

const MAX_MEMORY_USAGE = 1_000_000;

/**
* @fileoverview Tests that the memory usage is below a certain threshold.
*/

class MemoryUsage extends Audit {
static get meta() {
return {
id: 'memory-audit',
title: 'Did not find any large memory usage',
failureTitle: 'Found large memory usage',
description: 'Detects if any memory sample was larger than 1 MB',

// The name of the custom gatherer class that provides input to this audit.
requiredArtifacts: ['MemoryProfile'],
};
}

static audit(artifacts) {
let largestMemoryUsage = 0;
for (const sample of artifacts.MemoryProfile.samples) {
if (sample.total > largestMemoryUsage) {
largestMemoryUsage = sample.total;
}
}

return {
numericValue: largestMemoryUsage,
score: largestMemoryUsage > MAX_MEMORY_USAGE ? 0 : 1,
};
}
}

export default MemoryUsage;
32 changes: 32 additions & 0 deletions docs/recipes/custom-audit/memory-gatherer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @license Copyright 2023 The Lighthouse Authors. 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.
*/

import {Gatherer} from 'lighthouse';

class MemoryProfile extends Gatherer {
meta = {
supportedModes: ['navigation', 'timespan'],
};

async startInstrumentation(context) {
const session = context.driver.defaultSession;
await session.sendCommand('Memory.startSampling');
}

async stopInstrumentation(context) {
const session = context.driver.defaultSession;
await session.sendCommand('Memory.stopSampling');
}

async getArtifact(context) {
const session = context.driver.defaultSession;
const {profile} = await session.sendCommand('Memory.getSamplingProfile');

return profile;
}
}

export default MemoryProfile;
2 changes: 1 addition & 1 deletion docs/recipes/custom-audit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"type": "module",
"scripts": {},
"devDependencies": {
"lighthouse": "^9.5.0"
"lighthouse": "file:../../../dist/lighthouse.tgz"
}
}
45 changes: 0 additions & 45 deletions docs/recipes/custom-audit/searchable-audit.js

This file was deleted.

31 changes: 0 additions & 31 deletions docs/recipes/custom-audit/searchable-gatherer.js

This file was deleted.

9 changes: 0 additions & 9 deletions docs/recipes/custom-gatherer-puppeteer/custom-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@ export default {
{id: 'CustomGatherer', gatherer: 'custom-gatherer'},
],

navigations: [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive by

{
id: 'default',
artifacts: [
'CustomGatherer',
],
},
],

audits: [
'custom-audit',
],
Expand Down