|
| 1 | +import { test, expect, type Page } from '@playwright/test'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Grid Performance Tests using the standalone perf app. |
| 5 | + * |
| 6 | + * These tests use the grid-perf-app which provides a standalone Grid component |
| 7 | + * with MockGridModel data, allowing proper testing of Grid props without needing |
| 8 | + * a Deephaven server. |
| 9 | + * |
| 10 | + * Prerequisites: |
| 11 | + * 1. Install the perf app: cd tests/grid-perf-app && npm install |
| 12 | + * 2. Start the perf app: cd tests/grid-perf-app && npm run dev |
| 13 | + * 3. Run tests: RUN_PERF_TESTS=1 npx playwright test grid-perf-app.spec.ts |
| 14 | + * |
| 15 | + * The perf app supports query params: |
| 16 | + * - rows: Number of rows (default: 1000000) |
| 17 | + * - cols: Number of columns (default: 100) |
| 18 | + * - a11y: Enable accessibility layer (default: true, set to "false" to disable) |
| 19 | + */ |
| 20 | + |
| 21 | +const PERF_APP_URL = 'http://localhost:4020'; |
| 22 | + |
| 23 | +interface FPSResult { |
| 24 | + fps: number; |
| 25 | + avgFrameTime: number; |
| 26 | + minFrameTime: number; |
| 27 | + maxFrameTime: number; |
| 28 | + frameCount: number; |
| 29 | + droppedFrames: number; |
| 30 | +} |
| 31 | + |
| 32 | +async function startFPSMeasurement(page: Page): Promise<void> { |
| 33 | + await page.evaluate(() => { |
| 34 | + (window as any).__frameTimings = []; |
| 35 | + (window as any).__fpsRunning = true; |
| 36 | + let lastTime = performance.now(); |
| 37 | + |
| 38 | + function measureFrame() { |
| 39 | + if (!(window as any).__fpsRunning) return; |
| 40 | + |
| 41 | + const now = performance.now(); |
| 42 | + (window as any).__frameTimings.push(now - lastTime); |
| 43 | + lastTime = now; |
| 44 | + requestAnimationFrame(measureFrame); |
| 45 | + } |
| 46 | + requestAnimationFrame(measureFrame); |
| 47 | + }); |
| 48 | +} |
| 49 | + |
| 50 | +async function stopFPSMeasurement(page: Page): Promise<FPSResult> { |
| 51 | + const timings = await page.evaluate(() => { |
| 52 | + (window as any).__fpsRunning = false; |
| 53 | + return (window as any).__frameTimings as number[]; |
| 54 | + }); |
| 55 | + |
| 56 | + const validTimings = timings.filter(t => t < 500 && t > 0); |
| 57 | + |
| 58 | + if (validTimings.length === 0) { |
| 59 | + return { |
| 60 | + fps: 0, |
| 61 | + avgFrameTime: 0, |
| 62 | + minFrameTime: 0, |
| 63 | + maxFrameTime: 0, |
| 64 | + frameCount: 0, |
| 65 | + droppedFrames: 0, |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + const avgFrameTime = |
| 70 | + validTimings.reduce((a, b) => a + b, 0) / validTimings.length; |
| 71 | + const fps = 1000 / avgFrameTime; |
| 72 | + const minFrameTime = Math.min(...validTimings); |
| 73 | + const maxFrameTime = Math.max(...validTimings); |
| 74 | + const droppedFrames = validTimings.filter(t => t > 33).length; |
| 75 | + |
| 76 | + return { |
| 77 | + fps, |
| 78 | + avgFrameTime, |
| 79 | + minFrameTime, |
| 80 | + maxFrameTime, |
| 81 | + frameCount: validTimings.length, |
| 82 | + droppedFrames, |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Scrolls the grid in the perf app using mouse wheel events |
| 88 | + */ |
| 89 | +async function scrollPerfAppGrid( |
| 90 | + page: Page, |
| 91 | + totalDelta: number |
| 92 | +): Promise<void> { |
| 93 | + const canvas = page.locator('canvas').first(); |
| 94 | + const box = await canvas.boundingBox(); |
| 95 | + if (!box) throw new Error('Grid canvas not found'); |
| 96 | + |
| 97 | + await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2); |
| 98 | + |
| 99 | + const scrollStep = 100; |
| 100 | + const direction = Math.sign(totalDelta); |
| 101 | + let remaining = Math.abs(totalDelta); |
| 102 | + |
| 103 | + while (remaining > 0) { |
| 104 | + const step = Math.min(scrollStep, remaining); |
| 105 | + await page.mouse.wheel(0, step * direction); |
| 106 | + remaining -= step; |
| 107 | + await page.waitForTimeout(16); |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +function logResults( |
| 112 | + testName: string, |
| 113 | + result: FPSResult, |
| 114 | + expected: { minFps: number } |
| 115 | +): void { |
| 116 | + console.log(`\n${testName}:`); |
| 117 | + console.log(` Average FPS: ${result.fps.toFixed(1)}`); |
| 118 | + console.log(` Avg frame time: ${result.avgFrameTime.toFixed(2)}ms`); |
| 119 | + console.log( |
| 120 | + ` Frame time range: ${result.minFrameTime.toFixed( |
| 121 | + 2 |
| 122 | + )}ms - ${result.maxFrameTime.toFixed(2)}ms` |
| 123 | + ); |
| 124 | + console.log(` Total frames: ${result.frameCount}`); |
| 125 | + console.log( |
| 126 | + ` Dropped frames (>33ms): ${result.droppedFrames} (${( |
| 127 | + (result.droppedFrames / result.frameCount) * |
| 128 | + 100 |
| 129 | + ).toFixed(1)}%)` |
| 130 | + ); |
| 131 | + console.log(` Expected min FPS: ${expected.minFps}`); |
| 132 | +} |
| 133 | + |
| 134 | +test.describe('grid perf app - accessibility layer comparison', () => { |
| 135 | + test.skip( |
| 136 | + !process.env.RUN_PERF_TESTS, |
| 137 | + 'Performance tests skipped. Set RUN_PERF_TESTS=1 to run.' |
| 138 | + ); |
| 139 | + |
| 140 | + test.describe.configure({ mode: 'serial' }); |
| 141 | + |
| 142 | + test('compare scroll performance: accessibility layer ON vs OFF', async ({ |
| 143 | + page, |
| 144 | + }) => { |
| 145 | + // --- TEST 1: WITH ACCESSIBILITY LAYER --- |
| 146 | + await page.goto(`${PERF_APP_URL}/?rows=1000000&cols=100&a11y=true`); |
| 147 | + await page.waitForSelector('canvas'); |
| 148 | + |
| 149 | + // Verify accessibility layer is present |
| 150 | + const layerCount = await page |
| 151 | + .locator('[data-testid="grid-accessibility-layer"]') |
| 152 | + .count(); |
| 153 | + expect(layerCount).toBe(1); |
| 154 | + |
| 155 | + await startFPSMeasurement(page); |
| 156 | + await scrollPerfAppGrid(page, 5000); |
| 157 | + await scrollPerfAppGrid(page, -3000); |
| 158 | + await scrollPerfAppGrid(page, 4000); |
| 159 | + await scrollPerfAppGrid(page, -5000); |
| 160 | + const withLayerResult = await stopFPSMeasurement(page); |
| 161 | + |
| 162 | + // --- TEST 2: WITHOUT ACCESSIBILITY LAYER --- |
| 163 | + await page.goto(`${PERF_APP_URL}/?rows=1000000&cols=100&a11y=false`); |
| 164 | + await page.waitForSelector('canvas'); |
| 165 | + |
| 166 | + // Verify accessibility layer is NOT present |
| 167 | + const layerCountAfter = await page |
| 168 | + .locator('[data-testid="grid-accessibility-layer"]') |
| 169 | + .count(); |
| 170 | + expect(layerCountAfter).toBe(0); |
| 171 | + |
| 172 | + await startFPSMeasurement(page); |
| 173 | + await scrollPerfAppGrid(page, 5000); |
| 174 | + await scrollPerfAppGrid(page, -3000); |
| 175 | + await scrollPerfAppGrid(page, 4000); |
| 176 | + await scrollPerfAppGrid(page, -5000); |
| 177 | + const withoutLayerResult = await stopFPSMeasurement(page); |
| 178 | + |
| 179 | + // --- COMPARISON REPORT --- |
| 180 | + console.log('\n========================================'); |
| 181 | + console.log('ACCESSIBILITY LAYER PERFORMANCE COMPARISON'); |
| 182 | + console.log('========================================\n'); |
| 183 | + |
| 184 | + logResults('WITH Accessibility Layer', withLayerResult, { minFps: 28 }); |
| 185 | + logResults('WITHOUT Accessibility Layer', withoutLayerResult, { |
| 186 | + minFps: 28, |
| 187 | + }); |
| 188 | + |
| 189 | + const fpsDiff = withoutLayerResult.fps - withLayerResult.fps; |
| 190 | + const fpsPercentDiff = (fpsDiff / withoutLayerResult.fps) * 100; |
| 191 | + const frameTimeDiff = |
| 192 | + withLayerResult.avgFrameTime - withoutLayerResult.avgFrameTime; |
| 193 | + |
| 194 | + console.log('\n--- COMPARISON SUMMARY ---'); |
| 195 | + console.log(`FPS difference: ${fpsDiff.toFixed(2)} fps`); |
| 196 | + console.log( |
| 197 | + `Performance impact: ${fpsPercentDiff > 0 ? '-' : '+'}${Math.abs( |
| 198 | + fpsPercentDiff |
| 199 | + ).toFixed(2)}%` |
| 200 | + ); |
| 201 | + console.log(`Frame time overhead: ${frameTimeDiff.toFixed(3)}ms`); |
| 202 | + |
| 203 | + if (Math.abs(fpsPercentDiff) < 5) { |
| 204 | + console.log('\n✓ Accessibility layer has NEGLIGIBLE performance impact'); |
| 205 | + } else if (fpsPercentDiff > 0) { |
| 206 | + console.log( |
| 207 | + `\n⚠ Accessibility layer causes ${fpsPercentDiff.toFixed( |
| 208 | + 1 |
| 209 | + )}% performance decrease` |
| 210 | + ); |
| 211 | + } else { |
| 212 | + console.log( |
| 213 | + `\n✓ Accessibility layer has no negative impact (${Math.abs( |
| 214 | + fpsPercentDiff |
| 215 | + ).toFixed(1)}% faster)` |
| 216 | + ); |
| 217 | + } |
| 218 | + }); |
| 219 | +}); |
| 220 | + |
| 221 | +test.describe('grid perf app - stress tests', () => { |
| 222 | + test.skip( |
| 223 | + !process.env.RUN_PERF_TESTS, |
| 224 | + 'Performance tests skipped. Set RUN_PERF_TESTS=1 to run.' |
| 225 | + ); |
| 226 | + |
| 227 | + test.describe.configure({ mode: 'serial' }); |
| 228 | + |
| 229 | + test('scroll performance - 1M rows', async ({ page }) => { |
| 230 | + await page.goto(`${PERF_APP_URL}/?rows=1000000&cols=100`); |
| 231 | + await page.waitForSelector('canvas'); |
| 232 | + |
| 233 | + await startFPSMeasurement(page); |
| 234 | + |
| 235 | + await scrollPerfAppGrid(page, 5000); |
| 236 | + await scrollPerfAppGrid(page, -3000); |
| 237 | + await scrollPerfAppGrid(page, 4000); |
| 238 | + await scrollPerfAppGrid(page, -5000); |
| 239 | + |
| 240 | + const result = await stopFPSMeasurement(page); |
| 241 | + logResults('1M Rows Scroll', result, { minFps: 30 }); |
| 242 | + }); |
| 243 | + |
| 244 | + test('scroll performance - many columns', async ({ page }) => { |
| 245 | + await page.goto(`${PERF_APP_URL}/?rows=100000&cols=500`); |
| 246 | + await page.waitForSelector('canvas'); |
| 247 | + |
| 248 | + await startFPSMeasurement(page); |
| 249 | + |
| 250 | + // Horizontal and vertical scrolling |
| 251 | + for (let i = 0; i < 20; i += 1) { |
| 252 | + await page.mouse.wheel(500, 500); |
| 253 | + await page.waitForTimeout(32); |
| 254 | + await page.mouse.wheel(-300, 300); |
| 255 | + await page.waitForTimeout(32); |
| 256 | + } |
| 257 | + |
| 258 | + const result = await stopFPSMeasurement(page); |
| 259 | + logResults('500 Columns Scroll', result, { minFps: 28 }); |
| 260 | + }); |
| 261 | + |
| 262 | + test('sustained scrolling - 3 seconds', async ({ page }) => { |
| 263 | + await page.goto(`${PERF_APP_URL}/?rows=1000000&cols=100`); |
| 264 | + await page.waitForSelector('canvas'); |
| 265 | + |
| 266 | + const canvas = page.locator('canvas').first(); |
| 267 | + const box = await canvas.boundingBox(); |
| 268 | + if (!box) throw new Error('Grid canvas not found'); |
| 269 | + |
| 270 | + await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2); |
| 271 | + |
| 272 | + await startFPSMeasurement(page); |
| 273 | + |
| 274 | + const startTime = Date.now(); |
| 275 | + const duration = 3000; |
| 276 | + let direction = 1; |
| 277 | + |
| 278 | + while (Date.now() - startTime < duration) { |
| 279 | + await page.mouse.wheel(0, 300 * direction); |
| 280 | + await page.waitForTimeout(16); |
| 281 | + |
| 282 | + if (Math.random() < 0.1) { |
| 283 | + direction *= -1; |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + const result = await stopFPSMeasurement(page); |
| 288 | + logResults('Sustained Scroll (3s)', result, { minFps: 30 }); |
| 289 | + }); |
| 290 | +}); |
0 commit comments