-
Notifications
You must be signed in to change notification settings - Fork 48.4k
[mcp] Add proper web-vitals metric collection #33109
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
Open
jorge-cab
wants to merge
4
commits into
main
Choose a base branch
from
improve-web-vitals
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+122
−64
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,25 +8,51 @@ import * as babelPresetEnv from '@babel/preset-env'; | |||||||||||||
import * as babelPresetReact from '@babel/preset-react'; | ||||||||||||||
|
||||||||||||||
type PerformanceResults = { | ||||||||||||||
renderTime: number; | ||||||||||||||
renderTime: number[]; | ||||||||||||||
webVitals: { | ||||||||||||||
cls: number; | ||||||||||||||
lcp: number; | ||||||||||||||
inp: number; | ||||||||||||||
fid: number; | ||||||||||||||
ttfb: number; | ||||||||||||||
cls: number[]; | ||||||||||||||
lcp: number[]; | ||||||||||||||
inp: number[]; | ||||||||||||||
fid: number[]; | ||||||||||||||
ttfb: number[]; | ||||||||||||||
}; | ||||||||||||||
reactProfiler: { | ||||||||||||||
id: number; | ||||||||||||||
phase: number; | ||||||||||||||
actualDuration: number; | ||||||||||||||
baseDuration: number; | ||||||||||||||
startTime: number; | ||||||||||||||
commitTime: number; | ||||||||||||||
id: number[]; | ||||||||||||||
phase: number[]; | ||||||||||||||
actualDuration: number[]; | ||||||||||||||
baseDuration: number[]; | ||||||||||||||
startTime: number[]; | ||||||||||||||
commitTime: number[]; | ||||||||||||||
}; | ||||||||||||||
error: Error | null; | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
type EvaluationResults = { | ||||||||||||||
renderTime: number | null; | ||||||||||||||
webVitals: { | ||||||||||||||
cls: number | null; | ||||||||||||||
lcp: number | null; | ||||||||||||||
inp: number | null; | ||||||||||||||
fid: number | null; | ||||||||||||||
ttfb: number | null; | ||||||||||||||
}; | ||||||||||||||
reactProfiler: { | ||||||||||||||
id: number | null; | ||||||||||||||
phase: number | null; | ||||||||||||||
actualDuration: number | null; | ||||||||||||||
baseDuration: number | null; | ||||||||||||||
startTime: number | null; | ||||||||||||||
commitTime: number | null; | ||||||||||||||
}; | ||||||||||||||
error: Error | null; | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
function delay(time: number) { | ||||||||||||||
return new Promise(function (resolve) { | ||||||||||||||
setTimeout(resolve, time); | ||||||||||||||
}); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
export async function measurePerformance( | ||||||||||||||
code: string, | ||||||||||||||
iterations: number, | ||||||||||||||
|
@@ -72,21 +98,21 @@ export async function measurePerformance( | |||||||||||||
const html = buildHtml(transpiled); | ||||||||||||||
|
||||||||||||||
let performanceResults: PerformanceResults = { | ||||||||||||||
renderTime: 0, | ||||||||||||||
renderTime: [], | ||||||||||||||
webVitals: { | ||||||||||||||
cls: 0, | ||||||||||||||
lcp: 0, | ||||||||||||||
inp: 0, | ||||||||||||||
fid: 0, | ||||||||||||||
ttfb: 0, | ||||||||||||||
cls: [], | ||||||||||||||
lcp: [], | ||||||||||||||
inp: [], | ||||||||||||||
fid: [], | ||||||||||||||
ttfb: [], | ||||||||||||||
}, | ||||||||||||||
reactProfiler: { | ||||||||||||||
id: 0, | ||||||||||||||
phase: 0, | ||||||||||||||
actualDuration: 0, | ||||||||||||||
baseDuration: 0, | ||||||||||||||
startTime: 0, | ||||||||||||||
commitTime: 0, | ||||||||||||||
id: [], | ||||||||||||||
phase: [], | ||||||||||||||
actualDuration: [], | ||||||||||||||
baseDuration: [], | ||||||||||||||
startTime: [], | ||||||||||||||
commitTime: [], | ||||||||||||||
}, | ||||||||||||||
error: null, | ||||||||||||||
}; | ||||||||||||||
|
@@ -96,38 +122,67 @@ export async function measurePerformance( | |||||||||||||
await page.waitForFunction( | ||||||||||||||
'window.__RESULT__ !== undefined && (window.__RESULT__.renderTime !== null || window.__RESULT__.error !== null)', | ||||||||||||||
); | ||||||||||||||
|
||||||||||||||
// ui chaos monkey | ||||||||||||||
await page.waitForFunction(`window.__RESULT__ !== undefined && (function() { | ||||||||||||||
for (const el of [...document.querySelectorAll('a'), ...document.querySelectorAll('button')]) { | ||||||||||||||
console.log(el); | ||||||||||||||
el.click(); | ||||||||||||||
const selectors = await page.evaluate(() => { | ||||||||||||||
window.__INTERACTABLE_SELECTORS__ = []; | ||||||||||||||
const elements = Array.from(document.querySelectorAll('a')).concat( | ||||||||||||||
Array.from(document.querySelectorAll('button')), | ||||||||||||||
); | ||||||||||||||
for (const el of elements) { | ||||||||||||||
window.__INTERACTABLE_SELECTORS__.push(el.tagName.toLowerCase()); | ||||||||||||||
} | ||||||||||||||
return true; | ||||||||||||||
})() `); | ||||||||||||||
const evaluationResult: PerformanceResults = await page.evaluate(() => { | ||||||||||||||
return window.__INTERACTABLE_SELECTORS__; | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
for (const selector of selectors) { | ||||||||||||||
await page.click(selector); | ||||||||||||||
await delay(500); | ||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. depending on the number of elements this could be pretty slow. I don't think you need to wait 500ms after every click before clicking again right?
Suggested change
|
||||||||||||||
|
||||||||||||||
// Visit a new page for 1s to background the current page so that WebVitals can finish being calculated | ||||||||||||||
const tempPage = await browser.newPage(); | ||||||||||||||
await tempPage.evaluate(() => { | ||||||||||||||
return new Promise(resolve => { | ||||||||||||||
setTimeout(() => { | ||||||||||||||
resolve(true); | ||||||||||||||
}, 1000); | ||||||||||||||
}); | ||||||||||||||
}); | ||||||||||||||
await tempPage.close(); | ||||||||||||||
|
||||||||||||||
const evaluationResult: EvaluationResults = await page.evaluate(() => { | ||||||||||||||
return (window as any).__RESULT__; | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
// TODO: investigate why webvital metrics are not populating correctly | ||||||||||||||
performanceResults.renderTime += evaluationResult.renderTime; | ||||||||||||||
performanceResults.webVitals.cls += evaluationResult.webVitals.cls || 0; | ||||||||||||||
performanceResults.webVitals.lcp += evaluationResult.webVitals.lcp || 0; | ||||||||||||||
performanceResults.webVitals.inp += evaluationResult.webVitals.inp || 0; | ||||||||||||||
performanceResults.webVitals.fid += evaluationResult.webVitals.fid || 0; | ||||||||||||||
performanceResults.webVitals.ttfb += evaluationResult.webVitals.ttfb || 0; | ||||||||||||||
|
||||||||||||||
performanceResults.reactProfiler.id += | ||||||||||||||
evaluationResult.reactProfiler.actualDuration || 0; | ||||||||||||||
performanceResults.reactProfiler.phase += | ||||||||||||||
evaluationResult.reactProfiler.phase || 0; | ||||||||||||||
performanceResults.reactProfiler.actualDuration += | ||||||||||||||
evaluationResult.reactProfiler.actualDuration || 0; | ||||||||||||||
performanceResults.reactProfiler.baseDuration += | ||||||||||||||
evaluationResult.reactProfiler.baseDuration || 0; | ||||||||||||||
performanceResults.reactProfiler.startTime += | ||||||||||||||
evaluationResult.reactProfiler.startTime || 0; | ||||||||||||||
performanceResults.reactProfiler.commitTime += | ||||||||||||||
evaluationResult.reactProfiler.commitTime || 0; | ||||||||||||||
if (evaluationResult.renderTime !== null) { | ||||||||||||||
performanceResults.renderTime.push(evaluationResult.renderTime); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
const webVitalMetrics = ['cls', 'lcp', 'inp', 'fid', 'ttfb'] as const; | ||||||||||||||
for (const metric of webVitalMetrics) { | ||||||||||||||
if (evaluationResult.webVitals[metric] !== null) { | ||||||||||||||
performanceResults.webVitals[metric].push( | ||||||||||||||
evaluationResult.webVitals[metric], | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
const profilerMetrics = [ | ||||||||||||||
'id', | ||||||||||||||
'phase', | ||||||||||||||
'actualDuration', | ||||||||||||||
'baseDuration', | ||||||||||||||
'startTime', | ||||||||||||||
'commitTime', | ||||||||||||||
] as const; | ||||||||||||||
for (const metric of profilerMetrics) { | ||||||||||||||
if (evaluationResult.reactProfiler[metric] !== null) { | ||||||||||||||
performanceResults.reactProfiler[metric].push( | ||||||||||||||
evaluationResult.reactProfiler[metric], | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
performanceResults.error = evaluationResult.error; | ||||||||||||||
} | ||||||||||||||
|
@@ -159,14 +214,14 @@ function buildHtml(transpiled: string) { | |||||||||||||
renderTime: null, | ||||||||||||||
webVitals: {}, | ||||||||||||||
reactProfiler: {}, | ||||||||||||||
error: null | ||||||||||||||
error: null, | ||||||||||||||
}; | ||||||||||||||
|
||||||||||||||
webVitals.onCLS((metric) => { window.__RESULT__.webVitals.cls = metric; }); | ||||||||||||||
webVitals.onLCP((metric) => { window.__RESULT__.webVitals.lcp = metric; }); | ||||||||||||||
webVitals.onINP((metric) => { window.__RESULT__.webVitals.inp = metric; }); | ||||||||||||||
webVitals.onFID((metric) => { window.__RESULT__.webVitals.fid = metric; }); | ||||||||||||||
webVitals.onTTFB((metric) => { window.__RESULT__.webVitals.ttfb = metric; }); | ||||||||||||||
webVitals.onCLS(({value}) => { window.__RESULT__.webVitals.cls = value; }); | ||||||||||||||
webVitals.onLCP(({value}) => { window.__RESULT__.webVitals.lcp = value; }); | ||||||||||||||
webVitals.onINP(({value}) => { window.__RESULT__.webVitals.inp = value; }); | ||||||||||||||
webVitals.onFID(({value}) => { window.__RESULT__.webVitals.fid = value; }); | ||||||||||||||
webVitals.onTTFB(({value}) => { window.__RESULT__.webVitals.ttfb = value; }); | ||||||||||||||
|
||||||||||||||
try { | ||||||||||||||
${transpiled} | ||||||||||||||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the
TEST
for?