Skip to content
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

Wrap individual puppeteer navigations in try/catch block #175

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
213 changes: 112 additions & 101 deletions cli/commands/benchmark-web-vitals.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -460,121 +460,132 @@ async function benchmarkURL(
}

for ( let requestNum = 0; requestNum < params.amount; requestNum++ ) {
if ( logProgress ) {
logPartial(
`Benchmarking ${ requestNum + 1 } / ${ params.amount }...`
);
}
const page = await browser.newPage();
await page.setBypassCSP( true ); // Bypass CSP so the web vitals script tag can be injected below.
if ( params.cpuThrottleFactor ) {
await page.emulateCPUThrottling( params.cpuThrottleFactor );
}
try {
if ( logProgress ) {
logPartial(
`Benchmarking ${ requestNum + 1 } / ${ params.amount }...`
);
}
const page = await browser.newPage();
await page.setBypassCSP( true ); // Bypass CSP so the web vitals script tag can be injected below.
if ( params.cpuThrottleFactor ) {
await page.emulateCPUThrottling( params.cpuThrottleFactor );
}

if ( params.networkConditions ) {
await page.emulateNetworkConditions( params.networkConditions );
}
if ( params.networkConditions ) {
await page.emulateNetworkConditions( params.networkConditions );
}

if ( params.emulateDevice ) {
await page.emulate( params.emulateDevice );
}
if ( params.windowViewport ) {
await page.setViewport( {
...( params.emulateDevice
? params.emulateDevice.viewport
: {} ),
...params.windowViewport,
} );
}
if ( params.emulateDevice ) {
await page.emulate( params.emulateDevice );
}
if ( params.windowViewport ) {
await page.setViewport( {
...( params.emulateDevice
? params.emulateDevice.viewport
: {} ),
...params.windowViewport,
} );
}

// Load the page.
const urlObj = new URL( url );
urlObj.searchParams.append( 'rnd', String( Math.random() ) );
// Load the page.
const urlObj = new URL( url );
urlObj.searchParams.append( 'rnd', String( Math.random() ) );

// Make sure any username and password in the URL is passed along for authentication.
if ( urlObj.username && urlObj.password ) {
await page.authenticate( {
username: urlObj.username,
password: urlObj.password,
} );
}
// Make sure any username and password in the URL is passed along for authentication.
if ( urlObj.username && urlObj.password ) {
await page.authenticate( {
username: urlObj.username,
password: urlObj.password,
} );
}

const response = await page.goto( urlObj.toString(), {
waitUntil: 'networkidle0',
} );
if ( scriptTag ) {
await page.addScriptTag( { content: scriptTag, type: 'module' } );
}
const response = await page.goto( urlObj.toString(), {
waitUntil: 'networkidle0',
} );
if ( scriptTag ) {
await page.addScriptTag( {
content: scriptTag,
type: 'module',
} );
}

if ( response.status() !== 200 ) {
if ( logProgress ) {
log(
formats.error(
`Error: Bad response code ${ response.status() }.`
)
);
if ( response.status() !== 200 ) {
if ( logProgress ) {
log(
formats.error(
`Error: Bad response code ${ response.status() }.`
)
);
}
continue;
}
continue;
}

completeRequests++;
completeRequests++;

if ( groupedMetrics.webVitals ) {
await Promise.all(
Object.values( groupedMetrics.webVitals ).map(
async ( value ) => {
// Wait until global is populated.
await page.waitForFunction(
`window.${ value.global } !== undefined`
);
if ( groupedMetrics.webVitals ) {
await Promise.all(
Object.values( groupedMetrics.webVitals ).map(
async ( value ) => {
// Wait until global is populated.
await page.waitForFunction(
`window.${ value.global } !== undefined`
);

/*
* Do a random click, since only that triggers certain metrics
* like LCP, as only a user interaction stops reporting new LCP
* entries. See https://web.dev/lcp/.
*
* Click off screen to prevent clicking a link by accident and navigating away.
*/
await page.click( 'body', {
offset: { x: -500, y: -500 },
} );
// Get the metric value from the global.
const metric =
/** @type {number} */ await page.evaluate(
( global ) => window[ global ],
value.global
/*
* Do a random click, since only that triggers certain metrics
* like LCP, as only a user interaction stops reporting new LCP
* entries. See https://web.dev/lcp/.
*
* Click off screen to prevent clicking a link by accident and navigating away.
*/
await page.click( 'body', {
offset: { x: -500, y: -500 },
} );
// Get the metric value from the global.
const metric =
/** @type {number} */ await page.evaluate(
( global ) => window[ global ],
value.global
);
value.results.push( metric );
}
)
).catch( () => {
/* Ignore errors. */
} );
}

if ( groupedMetrics.serverTiming ) {
const serverTimingMetrics = await page.evaluate( () => {
const entry = performance.getEntries().find(
( ent ) => ent instanceof PerformanceNavigationTiming // eslint-disable-line no-undef
);
// eslint-disable-next-line no-undef
if ( entry instanceof PerformanceNavigationTiming ) {
return entry.serverTiming.reduce( ( acc, value ) => {
acc[ value.name ] = value.duration;
return acc;
}, {} );
}
return {};
} );
Object.values( groupedMetrics.serverTiming ).forEach(
( value ) => {
if ( serverTimingMetrics[ value.name ] ) {
value.results.push(
serverTimingMetrics[ value.name ]
);
value.results.push( metric );
}
}
)
).catch( () => {
/* Ignore errors. */
} );
}

if ( groupedMetrics.serverTiming ) {
const serverTimingMetrics = await page.evaluate( () => {
const entry = performance.getEntries().find(
( ent ) => ent instanceof PerformanceNavigationTiming // eslint-disable-line no-undef
);
// eslint-disable-next-line no-undef
if ( entry instanceof PerformanceNavigationTiming ) {
return entry.serverTiming.reduce( ( acc, value ) => {
acc[ value.name ] = value.duration;
return acc;
}, {} );
}
return {};
} );
Object.values( groupedMetrics.serverTiming ).forEach( ( value ) => {
if ( serverTimingMetrics[ value.name ] ) {
value.results.push( serverTimingMetrics[ value.name ] );
}
} );
}
}

if ( logProgress ) {
log( formats.success( 'Success.' ) );
if ( logProgress ) {
log( formats.success( 'Success.' ) );
}
} catch ( err ) {
log( formats.error( `Error: ${ err.message }.` ) );
}
}

Expand Down