Skip to content

Commit

Permalink
docs: update custom gatherer recipe for 10.0 (#14765)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamraine authored Feb 8, 2023
1 parent 7166a66 commit 72a10c2
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 98 deletions.
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: [
{
id: 'default',
artifacts: [
'CustomGatherer',
],
},
],

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

0 comments on commit 72a10c2

Please sign in to comment.