Skip to content

Commit

Permalink
chore(webpage-impact): delete lighthouse functionality
Browse files Browse the repository at this point in the history
Initially, the idea was to generate custom lighthouse reports
that are weighted according to ecological criteria. But I do not
see myself doing that in the near future because I do not believe this
feature would provide enough additional value, plus it blows up the plugin.
Thus, cleaning up.

Signed-off-by: alexzurbonsen <[email protected]>
  • Loading branch information
alexzurbonsen committed Oct 26, 2024
1 parent 12be9b2 commit bd608a8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/lib/timer/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const TimerStart = PluginFactory({
'timer/start': {
description:
'Timestamp, usually set by a prior invocation of `TimerStart`. (But that is no requirement.)',
unit: 'none',
unit: 'ISO date string',
'aggregation-method': {time: 'none', component: 'none'},
},
},
Expand Down
9 changes: 2 additions & 7 deletions src/lib/webpage-impact/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Webpage Impact

> [!NOTE] > `WebpageImpact` (based on [Puppeteer](https://github.com/puppeteer/puppeteer) and [Lighthouse](https://github.com/GoogleChrome/lighthouse)) is a community plugin, not part of the IF standard library. This means the IF core team are not closely monitoring these plugins to keep them up to date. You should do your own research before implementing them!
> [!NOTE] > `WebpageImpact` (based on [Puppeteer](https://github.com/puppeteer/puppeteer)) is a community plugin, not part of the IF standard library. This means the IF core team are not closely monitoring these plugins to keep them up to date. You should do your own research before implementing them!
The `WebpageImpact` plugin measures the weight of a webpage in bytes and the weights of the different loaded resources categorized by type. It can also approximate, with certain restrictions, the percentage of data that needs to be reloaded if the page is revisited. The plugin is build with [Puppeteer](https://github.com/puppeteer/puppeteer). It can also generate a Lighthouse report for further investigation, if needed. Its outputs can be fed to the [co2js plugin](https://github.com/Green-Software-Foundation/if-unofficial-plugins/tree/main/src/lib/co2js) to estimate carbon impacts.
The `WebpageImpact` plugin measures the weight of a webpage in bytes and the weights of the different loaded resources categorized by type. It can also approximate, with certain restrictions, the percentage of data that needs to be reloaded if the page is revisited. The plugin is build with [Puppeteer](https://github.com/puppeteer/puppeteer). Its outputs can be fed to the [co2js plugin](https://github.com/Green-Software-Foundation/if-unofficial-plugins/tree/main/src/lib/co2js) to estimate carbon impacts.

**Note**: The plugin and the example manifest below were tested with v0.7.1 of the Impact Framework (IF). Since IF's interface is still subject to change, it cannot be guaranteed that plugin and manifest will work with future versions of IF.

Expand All @@ -23,16 +23,13 @@ The follwing config parameters are optional:
- `headers`:
- `accept`: string https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept
- `accept-encoding`: array of allowed encodings (a single encoding can also be passed as a string) https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding
- `lighthouse`: boolean, if true, a lighthouse report is generated

### Returns

- `network/data/bytes`: page weight in bytes
- `network/data/resources/bytes`: resource weights by category in bytes
- `dataReloadRatio`: the percentage of data that is downloaded by return visitors (can be fed into the CO2.JS plugin)
if `options.dataReloadRatio` is already provided in input, the plugin won't calculate it
- `lighthouse-report`: file name of the full lighthouse report, stored in html format in the directory in which `if-run` is executed
if `lighthouse` is set to true in the config
- `timestamp`: set to the time of the plugin execution
- `duration`: set to 0 (because the request time does not seem of particular interest here to the author)

Expand All @@ -51,8 +48,6 @@ Several config options are provided to modify the loading of the page, e.g. emul
The plugin can also approximate the `dataReloadRatio` that is needed for carbon emissions estimation with the Sustainable Webdesign Model (provided by the co2js plugin). To approximate the `dataReloadRatio` the page weight is calculated for a first visit and a return visit. The difference `weight of initial load - weight of reload` plus the weight of the resources that were loaded from browser cache on reload, is assumed to be the weight of resources that did not need reloading.
This assumption can be off. For example if there is a lot of dynamic content on the page, that is requested only under certain conditions or at specific times. Also, cached resources provided by service workers are not taken into account. Possibly, content personalization can also distort the measurement if initial load and reload do not get comparable content.

Additionally, the plugin can also generate a lighthouse report to provide additional insights into the performance of the webpage.

Further remarks:

- Cookies are not supported.
Expand Down
58 changes: 4 additions & 54 deletions src/lib/webpage-impact/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// SPDX-FileCopyrightText: 2016 Google LLC
// SPDX-License-Identifier: Apache-2.0

import fs from 'fs';
import lighthouse from 'lighthouse/core/index.cjs';
import puppeteer, {
HTTPRequest,
HTTPResponse,
Expand Down Expand Up @@ -110,7 +108,7 @@ export const WebpageImpact = PluginFactory({
return validateConfig(config);
},
implementation: async (inputs: PluginParams[], config: ConfigParams) => {
const {measurePageImpactMetrics, writeReportToFile} = WebpageImpactUtils();
const {measurePageImpactMetrics} = WebpageImpactUtils();

if (inputs.length === 0) {
inputs.push({});
Expand All @@ -120,28 +118,18 @@ export const WebpageImpact = PluginFactory({
inputs.map(async input => {
const startTime = Date.now();

const {
pageWeight,
resourceTypeWeights,
dataReloadRatio,
lighthouseResult,
} = await measurePageImpactMetrics(config.url, config);
const {pageWeight, resourceTypeWeights, dataReloadRatio} =
await measurePageImpactMetrics(config.url, config);

const durationInSeconds = (Date.now() - startTime) / 1000;

let reportPath;
if (lighthouseResult) {
reportPath = writeReportToFile(lighthouseResult.report, input);
}

return {
...input,
timestamp: new Date(startTime).toISOString(),
duration: durationInSeconds,
url: config.url,
'network/data/bytes': pageWeight,
'network/data/resources/bytes': resourceTypeWeights,
...(lighthouseResult ? {'lighthouse-report': reportPath} : {}),
...(config.options || dataReloadRatio // TODO not sure it is necessary to copy input.options here in every case instead of referencing them
? {
options: {
Expand All @@ -162,6 +150,7 @@ const WebpageImpactUtils = () => {
config?: ConfigParams
) => {
const computeReloadRatio = !config?.options?.dataReloadRatio;

const requestHandler = (interceptedRequest: HTTPRequest) => {
const headers = Object.assign({}, interceptedRequest.headers(), {
...(config?.headers?.accept && {
Expand Down Expand Up @@ -212,26 +201,12 @@ const WebpageImpactUtils = () => {
scrollToBottom: config?.scrollToBottom,
});

let lighthouseResult;
if (config?.lighthouse) {
lighthouseResult = await lighthouse(
url,
{
output: 'html',
logLevel: 'info',
},
undefined,
page
);
}

return {
...computeMetrics(
initialResources,
reloadedResources,
computeReloadRatio
),
lighthouseResult,
};
} finally {
await browser.close();
Expand Down Expand Up @@ -433,30 +408,6 @@ const WebpageImpactUtils = () => {
return Math.round(num * factor) / factor;
};

const writeReportToFile = (
lighthouseReport: string | string[],
validatedInput: PluginParams
): string => {
const timestamp = validatedInput['timer/start']
? validatedInput['timer/start']
: validatedInput.timestamp;
const unescapedFileName = `lighthouse-report-${validatedInput.url}-${timestamp}.html`;

const fileName = getEscapedFileName(unescapedFileName);
fs.writeFileSync(
fileName,
Array.isArray(lighthouseReport)
? lighthouseReport.join(' ')
: lighthouseReport,
'utf8'
);
return fileName;
};

const getEscapedFileName = (url: string): string => {
return url.replace(/[/\\?%*:|"<>]/g, '_');
};

const validateConfig = (config: ConfigParams) => {
if (!config || !Object.keys(config)?.length) {
throw new ConfigError(MISSING_CONFIG);
Expand Down Expand Up @@ -522,7 +473,6 @@ const WebpageImpactUtils = () => {

return {
measurePageImpactMetrics,
writeReportToFile,
validateConfig,
};
};

0 comments on commit bd608a8

Please sign in to comment.