|
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | + |
| 3 | +test.describe('performance', () => { |
| 4 | + test('should render expensive component within 1 second after checkbox click', async ({ |
| 5 | + page |
| 6 | + }) => { |
| 7 | + // Navigate to the perf page |
| 8 | + await page.goto('./perf', { waitUntil: 'load' }) |
| 9 | + |
| 10 | + // Inject performance measurement into the page |
| 11 | + await page.evaluate(() => { |
| 12 | + const checkboxInput = document.querySelector( |
| 13 | + 'input[type="checkbox"]' |
| 14 | + ) as HTMLInputElement |
| 15 | + let expensiveComponentContainer: HTMLElement | null = null |
| 16 | + const targetChildCount = 10_000 |
| 17 | + let startTime = 0 |
| 18 | + |
| 19 | + // Track when React starts and completes rendering |
| 20 | + const observer = new MutationObserver(mutations => { |
| 21 | + for (const mutation of mutations) { |
| 22 | + const addedNodes = Array.from(mutation.addedNodes) |
| 23 | + for (const node of addedNodes) { |
| 24 | + if (node instanceof HTMLElement) { |
| 25 | + const h2 = node.querySelector?.('h2') |
| 26 | + if (h2?.textContent === 'Expensive Component') { |
| 27 | + expensiveComponentContainer = node |
| 28 | + console.log('Found Expensive Component container') |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + if (expensiveComponentContainer) { |
| 34 | + const renderedComponents = |
| 35 | + expensiveComponentContainer.querySelectorAll('div > div').length |
| 36 | + |
| 37 | + if (renderedComponents % 1000 === 0 && renderedComponents > 0) { |
| 38 | + console.log(`Rendered ${renderedComponents} components...`) |
| 39 | + } |
| 40 | + |
| 41 | + if (renderedComponents >= targetChildCount) { |
| 42 | + console.log(`All ${renderedComponents} components rendered!`) |
| 43 | + |
| 44 | + // Use requestAnimationFrame to ensure the browser has painted |
| 45 | + requestAnimationFrame(() => { |
| 46 | + requestAnimationFrame(() => { |
| 47 | + // Double RAF to ensure we're after the paint |
| 48 | + window.performance.mark('expensive-component-painted') |
| 49 | + const paintTime = performance.now() - startTime |
| 50 | + console.log( |
| 51 | + `Total time including paint: ${paintTime.toFixed(2)}ms` |
| 52 | + ) |
| 53 | + }) |
| 54 | + }) |
| 55 | + |
| 56 | + observer.disconnect() |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + }) |
| 61 | + |
| 62 | + observer.observe(document.body, { childList: true, subtree: true }) |
| 63 | + |
| 64 | + // Capture more precise timing |
| 65 | + checkboxInput.addEventListener( |
| 66 | + 'click', |
| 67 | + () => { |
| 68 | + startTime = performance.now() |
| 69 | + window.performance.mark('state-change-start') |
| 70 | + console.log('Checkbox clicked, state change started') |
| 71 | + }, |
| 72 | + { once: true } |
| 73 | + ) |
| 74 | + }) |
| 75 | + |
| 76 | + // Find and click the checkbox |
| 77 | + const checkbox = page.locator('input[type="checkbox"]') |
| 78 | + await checkbox.click() |
| 79 | + |
| 80 | + // Wait for the expensive component to be fully rendered |
| 81 | + const expensiveComponentHeading = page.locator( |
| 82 | + 'h2:has-text("Expensive Component")' |
| 83 | + ) |
| 84 | + await expect(expensiveComponentHeading).toBeVisible({ timeout: 60_000 }) |
| 85 | + |
| 86 | + // Wait for all components and paint to complete |
| 87 | + await page.waitForFunction( |
| 88 | + () => { |
| 89 | + return ( |
| 90 | + window.performance.getEntriesByName('expensive-component-painted') |
| 91 | + .length > 0 |
| 92 | + ) |
| 93 | + }, |
| 94 | + { timeout: 5000 } |
| 95 | + ) |
| 96 | + |
| 97 | + // Get the render time from state change to paint |
| 98 | + const renderTime = await page.evaluate(() => { |
| 99 | + const startMark = |
| 100 | + window.performance.getEntriesByName('state-change-start')[0] |
| 101 | + const paintMark = window.performance.getEntriesByName( |
| 102 | + 'expensive-component-painted' |
| 103 | + )[0] |
| 104 | + |
| 105 | + if (!startMark || !paintMark) { |
| 106 | + throw new Error('Performance marks not found') |
| 107 | + } |
| 108 | + |
| 109 | + return paintMark.startTime - startMark.startTime |
| 110 | + }) |
| 111 | + |
| 112 | + // Assert that the rendering took less than 1 second (1000ms) |
| 113 | + expect(renderTime).toBeLessThan(1000) |
| 114 | + }) |
| 115 | +}) |
0 commit comments