Skip to content

Commit

Permalink
chore(webpage-impact): introduce config option for reload ratio compu…
Browse files Browse the repository at this point in the history
…tation

Until now the reload ratio was computed if no dataReloadRatio is provided in
the inputs. That seemed like a reasonable default but it is also too implicit.

Introduce a config option and only compute it if the option is provided.

Signed-off-by: alexzurbonsen <[email protected]>
  • Loading branch information
alexzurbonsen committed Oct 28, 2024
1 parent eb0b914 commit 75ab7b6
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 19 deletions.
3 changes: 2 additions & 1 deletion example-manifests/measure-webpage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ initialize:
method: WebpageImpact
path: '@tngtech/if-webpage-plugins'
config:
scrollToBottom: true
url: https://www.tngtech.com
scrollToBottom: true
computeReloadRati: true
'co2js':
method: Co2js
path: '@tngtech/if-webpage-plugins'
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/unit/lib/webpage-impact/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('lib/webpage-impact', () => {
jest.spyOn(Date, 'now').mockImplementation(() => mockTimestamp);

const webpageImpact = WebpageImpact(
{url: 'http://localhost:3000'},
{url: 'http://localhost:3000', computeReloadRatio: true},
{},
{}
);
Expand Down
5 changes: 3 additions & 2 deletions src/lib/webpage-impact/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The `WebpageImpact` plugin measures the weight of a webpage in bytes and the wei

The follwing config parameters are optional:

- `computeReloadRatio`: If true, a heuristic value for the `dataReloadRatio` is computed, which can be used as input for the co2js plugin. Any value for the `dataReloadRatio` that is provided as input will be overwritten. The data reload ratio expresses how much data has to be reloaded if the web page is visited a second time.
- `mobileDevice:`: You can pick a mobile device to emulate. Must be one of puppeteer's known devices: https://pptr.dev/api/puppeteer.knowndevices
- `emulateNetworkConditions`: You can pick one of puppeteer's predefined network conditions: https://pptr.dev/api/puppeteer.predefinednetworkconditions
- `scrollToBottom`: If true, emulates a user scrolling to the bottom of the page (which loads all content that isn't loaded on initial load). If false, the page is not scrolled. Default: false.
Expand All @@ -28,8 +29,7 @@ The follwing config parameters are optional:

- `network/data/bytes`: page weight in bytes
- `network/data/resources/bytes`: resource weights by category in bytes
- `dataReloadRatio`: an estimate of the percentage of data that is downloaded by return visitors (can be fed into the CO2.JS plugin)
if `options.dataReloadRatio` is already provided as an input, the plugin won't calculate it
- `dataReloadRatio`: If `computeReloadRatio` is true: estimate of the amount of data that is reloaded on return visits (Can be fed into the co2js plugin.)
- `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 Down Expand Up @@ -72,6 +72,7 @@ initialize:
path: '@tngtech/if-webpage-plugins'
config:
url: 'https://tngtech.com'
computeReloadRatio: true
tree:
children:
child:
Expand Down
19 changes: 4 additions & 15 deletions src/lib/webpage-impact/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,8 @@ export const WebpageImpact = PluginFactory({
inputs.map(async input => {
const startTime = Date.now();

const computeReloadRatio = !input?.options?.dataReloadRatio;

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

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

Expand All @@ -118,7 +112,7 @@ export const WebpageImpact = PluginFactory({
url: config.url,
'network/data/bytes': pageWeight,
'network/data/resources/bytes': resourceTypeWeights,
...(dataReloadRatio
...(config.computeReloadRatio && dataReloadRatio
? {
options: {
...input.options,
Expand All @@ -135,7 +129,6 @@ export const WebpageImpact = PluginFactory({
const WebpageImpactUtils = () => {
const measurePageImpactMetrics = async (
url: string,
computeReloadRatio: boolean,
config?: ConfigParams
) => {
const requestHandler = (interceptedRequest: HTTPRequest) => {
Expand Down Expand Up @@ -186,7 +179,7 @@ const WebpageImpactUtils = () => {
});

let reloadedResources: Resource[] | undefined;
if (computeReloadRatio) {
if (config?.computeReloadRatio) {
reloadedResources = await loadPageResources(page, url, {
reload: true,
cacheEnabled: true,
Expand Down Expand Up @@ -377,6 +370,7 @@ const WebpageImpactUtils = () => {
}

const optionalConfigs = z.object({
computeReloadRatio: z.boolean().optional(),
timeout: z.number().gte(0).optional(),
mobileDevice: z.string().optional(),
emulateNetworkConditions: z.string().optional(),
Expand All @@ -390,11 +384,6 @@ const WebpageImpactUtils = () => {
.optional(),
})
.optional(),
options: z
.object({
dataReloadRatio: z.number().optional(),
})
.optional(),
});

const configSchema = z
Expand Down

0 comments on commit 75ab7b6

Please sign in to comment.