Unverified Commit 31dc9381 authored by Jude Gao's avatar Jude Gao Committed by GitHub

only log when instrumentation client takes too long (#77378)

Only log when the sync code inside instrumentation client takes more
than 16ms, instead of logging every single time.
parent 2b635e55
...@@ -6,12 +6,27 @@ ...@@ -6,12 +6,27 @@
*/ */
if (process.env.__NEXT_EXPERIMENTAL_CLIENT_INSTRUMENTATION_HOOK) { if (process.env.__NEXT_EXPERIMENTAL_CLIENT_INSTRUMENTATION_HOOK) {
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
const measureName = 'Client Instrumentation Hook'
const startTime = performance.now() const startTime = performance.now()
require('private-next-instrumentation-client') require('private-next-instrumentation-client')
const endTime = performance.now() const endTime = performance.now()
console.log(
`[Client Instrumentation Hook] executed in ${(endTime - startTime).toFixed(0)}ms (Note: Code download overhead is not included in this measurement)` const duration = endTime - startTime
) performance.measure(measureName, {
start: startTime,
end: endTime,
detail: 'Client instrumentation initialization',
})
// Using 16ms threshold as it represents one frame (1000ms/60fps)
// This helps identify if the instrumentation hook initialization
// could potentially cause frame drops during development.
const THRESHOLD = 16
if (duration > THRESHOLD) {
console.log(
`[${measureName}] Slow execution detected: ${duration.toFixed(0)}ms (Note: Code download overhead is not included in this measurement)`
)
}
} else { } else {
require('private-next-instrumentation-client') require('private-next-instrumentation-client')
} }
......
;(window as any).__INSTRUMENTATION_CLIENT_EXECUTED_AT = performance.now() ;(window as any).__INSTRUMENTATION_CLIENT_EXECUTED_AT = performance.now()
const start = performance.now()
while (performance.now() - start < 20) {
// Intentionally block for 20ms to test instrumentation timing
}
...@@ -7,18 +7,21 @@ describe('Instrumentation Client Hook', () => { ...@@ -7,18 +7,21 @@ describe('Instrumentation Client Hook', () => {
{ {
name: 'With src folder', name: 'With src folder',
appDir: 'app-with-src', appDir: 'app-with-src',
shouldLog: false,
}, },
{ {
name: 'App Router', name: 'App Router',
appDir: 'app-router', appDir: 'app-router',
shouldLog: true,
}, },
{ {
name: 'Pages Router', name: 'Pages Router',
appDir: 'pages-router', appDir: 'pages-router',
shouldLog: false,
}, },
] ]
testCases.forEach(({ name, appDir }) => { testCases.forEach(({ name, appDir, shouldLog }) => {
describe(name, () => { describe(name, () => {
const { next, isNextDev } = nextTestSetup({ const { next, isNextDev } = nextTestSetup({
files: path.join(__dirname, appDir), files: path.join(__dirname, appDir),
...@@ -37,9 +40,11 @@ describe('Instrumentation Client Hook', () => { ...@@ -37,9 +40,11 @@ describe('Instrumentation Client Hook', () => {
expect(instrumentationTime).toBeLessThan(hydrationTime) expect(instrumentationTime).toBeLessThan(hydrationTime)
expect( expect(
(await browser.log()).some((log) => (await browser.log()).some((log) =>
log.message.startsWith('[Client Instrumentation Hook]') log.message.startsWith(
'[Client Instrumentation Hook] Slow execution detected'
)
) )
).toBe(isNextDev) ).toBe(isNextDev && shouldLog)
}) })
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment