-
Notifications
You must be signed in to change notification settings - Fork 4.7k
E2E Test Utils: Add new fixtures for performance metrics #52993
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
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
a48f6dc
Add Playwright metrics fixtures
swissspidy aaaf0e4
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy dd8a12c
Fix version discrepancy
swissspidy 81843ce
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy fb27c7a
Fix doc block
swissspidy 5594c52
Fix ESM issues
swissspidy 26cc7d4
Remove comments
swissspidy f7c6a3a
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy a61dcb1
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy 24c9caa
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy b0a6ac3
Downgrade again to 1.32.0
swissspidy 1364564
Update docblocks
swissspidy 068e63a
Remove unneeded entry
swissspidy 3c2ba72
Revert "Remove unneeded entry"
swissspidy fae4e49
Remove unnecessary peer dependency
swissspidy 360e9ae
Add comments
swissspidy c5243ef
Rename variables
swissspidy f0d53e1
TTFB measures the elapsed time between startTime and responseStart
swissspidy 7b64157
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy 581ef5d
Simplify `getTimeToFirstByte` util
swissspidy c4951e6
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy 5a16e43
Combine `lighthouseBrowser` & `lighthousePort` fixture
swissspidy fc5b5fa
Add comment
swissspidy bed3ea6
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy d607c81
Disable throttling in Lighthouse
swissspidy 2eb1b9a
Disable mobile emulation
swissspidy db123bf
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy 30b6d47
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy f77e26f
disableFullPageScreenshot
swissspidy 918f630
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy a3b4df8
Split into two classes
swissspidy 7c7cdce
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy 18fbdc5
Use patch-package instead
swissspidy 85efcc8
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy e36f502
Merge branch 'trunk' into add/playwright-utils-metrics
swissspidy 7b1c83c
Add explanatory comment
swissspidy ded25ba
Lint fixes
swissspidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add Playwright metrics fixtures
- Loading branch information
commit a48f6dc6d0bd735b3b473932c56870a3d9994583
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -104,7 +104,7 @@ | |
| "@octokit/rest": "16.26.0", | ||
| "@octokit/types": "6.34.0", | ||
| "@octokit/webhooks-types": "5.6.0", | ||
| "@playwright/test": "1.32.0", | ||
| "@playwright/test": "1.34.3", | ||
| "@pmmmwh/react-refresh-webpack-plugin": "0.5.2", | ||
| "@storybook/addon-a11y": "6.5.7", | ||
| "@storybook/addon-actions": "6.5.7", | ||
|
|
@@ -218,6 +218,7 @@ | |
| "node-watch": "0.7.0", | ||
| "npm-run-all": "4.1.5", | ||
| "patch-package": "6.2.2", | ||
| "playwright": "1.34.3", | ||
| "postcss": "8.4.16", | ||
| "postcss-loader": "6.2.1", | ||
| "prettier": "npm:[email protected]", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
packages/e2e-test-utils-playwright/src/metrics/index.ts
swissspidy marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import lighthouse from 'lighthouse'; | ||
| import type { Page } from '@playwright/test'; | ||
|
|
||
| export class Metrics { | ||
swissspidy marked this conversation as resolved.
Show resolved
Hide resolved
felixarntz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| constructor( public readonly page: Page, public readonly port: number ) { | ||
swissspidy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| this.page = page; | ||
| this.port = port; | ||
| } | ||
|
|
||
| /** | ||
| * Returns durations from the Server-Timing header. | ||
| * | ||
| * @param fields Optional fields to filter. | ||
| */ | ||
| async getServerTiming( fields: string[] = [] ) { | ||
| return this.page.evaluate< Record< string, number >, string[] >( | ||
| ( f: string[] ) => | ||
| ( | ||
| performance.getEntriesByType( | ||
| 'navigation' | ||
| ) as PerformanceNavigationTiming[] | ||
| )[ 0 ].serverTiming.reduce( ( acc, entry ) => { | ||
| if ( f.length === 0 || f.includes( entry.name ) ) { | ||
| acc[ entry.name ] = entry.duration; | ||
| } | ||
| return acc; | ||
| }, {} as Record< string, number > ), | ||
| fields | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns time to first byte (TTFB) from PerformanceObserver. | ||
| */ | ||
| async getTimeToFirstByte() { | ||
| return this.page.evaluate< number >( | ||
| () => | ||
| new Promise( ( resolve ) => { | ||
| new PerformanceObserver( ( entryList ) => { | ||
| const [ pageNav ] = entryList.getEntriesByType( | ||
| 'navigation' | ||
| ) as PerformanceNavigationTiming[]; | ||
|
|
||
| resolve( pageNav.responseStart ); | ||
| } ).observe( { | ||
| type: 'navigation', | ||
| buffered: true, | ||
| } ); | ||
| } ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns Largest Contentful Paint (LCP) time. | ||
| */ | ||
| async getLargestContentfulPaint() { | ||
| return this.page.evaluate< number >( | ||
| () => | ||
| new Promise( ( resolve ) => { | ||
| new PerformanceObserver( ( entryList ) => { | ||
| const entries = entryList.getEntries(); | ||
| // The last entry is the largest contentful paint. | ||
| const largestPaintEntry = entries.at( -1 ); | ||
|
|
||
| resolve( largestPaintEntry?.startTime || 0 ); | ||
| } ).observe( { | ||
| type: 'largest-contentful-paint', | ||
| buffered: true, | ||
| } ); | ||
| } ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns Largest Contentful Paint (LCP) time. | ||
| */ | ||
| async getCumulativeLayoutShift() { | ||
| return this.page.evaluate< number >( | ||
| () => | ||
| new Promise( ( resolve ) => { | ||
| let CLS = 0; | ||
|
|
||
| new PerformanceObserver( ( l ) => { | ||
| const entries = l.getEntries() as LayoutShift[]; | ||
|
|
||
| entries.forEach( ( entry ) => { | ||
| if ( ! entry.hadRecentInput ) { | ||
| CLS += entry.value; | ||
| } | ||
| } ); | ||
|
|
||
| resolve( CLS ); | ||
| } ).observe( { | ||
| type: 'layout-shift', | ||
| buffered: true, | ||
| } ); | ||
| } ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the Lighthouse report for the current URL. | ||
| * | ||
| * Runs several Lighthouse audits in a separate browser window and returns | ||
| * the summary. | ||
| */ | ||
| async getLighthouseReport() { | ||
| // From https://github.com/GoogleChrome/lighthouse/blob/d149e9c1b628d5881ca9ca451278d99ff1b31d9a/core/config/default-config.js#L433-L503 | ||
| const audits = { | ||
| 'largest-contentful-paint': 'LCP', | ||
| 'total-blocking-time': 'TBT', | ||
| interactive: 'TTI', | ||
| 'cumulative-layout-shift': 'CLS', | ||
| 'experimental-interaction-to-next-paint': 'INP', | ||
| }; | ||
|
|
||
| const report = await lighthouse( | ||
| this.page.url(), | ||
| { port: this.port }, | ||
| { | ||
| settings: { | ||
| // Only run certain audits to speed things up. | ||
| onlyAudits: Object.keys( audits ), | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| const result: Record< string, number > = {}; | ||
|
|
||
| if ( ! report ) { | ||
| return result; | ||
| } | ||
|
|
||
| const { lhr } = report; | ||
|
|
||
| for ( const [ audit, acronym ] of Object.entries( audits ) ) { | ||
| result[ acronym ] = lhr.audits[ audit ].numericValue || 0; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.