askrobots / dbbasic-web-native
Public
100+ copy-paste UI components in plain HTML and CSS: no dependencies, no build step.
danq9
Add canonical.html v1.0 - The single source of truth for usability standards
fb6e650
1 year ago
History
test_performance.py · 14.5 KB · 416 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Performance benchmark tests. |
| 4 | Tests load time, render time, memory usage, and component initialization. |
| 5 | Cost: FREE (Performance metrics from browser) |
| 6 | Run: On PR |
| 7 | """ |
| 8 | |
| 9 | from playwright.sync_api import sync_playwright |
| 10 | from pathlib import Path |
| 11 | import json |
| 12 | import time |
| 13 | |
| 14 | class PerformanceTester: |
| 15 | def __init__(self): |
| 16 | self.results = [] |
| 17 | |
| 18 | def test_component_load_time(self, page, component_name): |
| 19 | """Test component page load time""" |
| 20 | issues = [] |
| 21 | |
| 22 | file_path = Path(__file__).parent / 'docs' / component_name |
| 23 | |
| 24 | # Measure load time |
| 25 | start_time = time.time() |
| 26 | page.goto(f'file://{file_path}') |
| 27 | page.wait_for_load_state('networkidle') |
| 28 | load_time = (time.time() - start_time) * 1000 # Convert to ms |
| 29 | |
| 30 | # Get performance metrics from browser |
| 31 | metrics = page.evaluate("""() => { |
| 32 | const perfData = window.performance.timing; |
| 33 | const navigation = performance.getEntriesByType('navigation')[0]; |
| 34 | |
| 35 | return { |
| 36 | domContentLoaded: perfData.domContentLoadedEventEnd - perfData.navigationStart, |
| 37 | domComplete: perfData.domComplete - perfData.navigationStart, |
| 38 | loadComplete: perfData.loadEventEnd - perfData.navigationStart, |
| 39 | // Modern metrics if available |
| 40 | navigationStart: navigation ? navigation.startTime : 0, |
| 41 | responseEnd: navigation ? navigation.responseEnd : 0, |
| 42 | domInteractive: navigation ? navigation.domInteractive : 0 |
| 43 | }; |
| 44 | }""") |
| 45 | |
| 46 | # Thresholds: |
| 47 | # < 100ms: Excellent |
| 48 | # 100-300ms: Good |
| 49 | # 300-500ms: Acceptable |
| 50 | # > 500ms: Slow |
| 51 | |
| 52 | if load_time > 500: |
| 53 | issues.append({ |
| 54 | 'type': 'slow_load_time', |
| 55 | 'load_time': f"{load_time:.0f}ms", |
| 56 | 'threshold': '500ms', |
| 57 | 'severity': 'error', |
| 58 | 'message': f"Load time {load_time:.0f}ms exceeds 500ms threshold" |
| 59 | }) |
| 60 | elif load_time > 300: |
| 61 | issues.append({ |
| 62 | 'type': 'acceptable_load_time', |
| 63 | 'load_time': f"{load_time:.0f}ms", |
| 64 | 'severity': 'warning', |
| 65 | 'message': f"Load time {load_time:.0f}ms is acceptable but could be improved" |
| 66 | }) |
| 67 | |
| 68 | return { |
| 69 | 'component': component_name, |
| 70 | 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, |
| 71 | 'load_time_ms': round(load_time, 2), |
| 72 | 'metrics': metrics, |
| 73 | 'issues': issues |
| 74 | } |
| 75 | |
| 76 | def test_web_component_registration(self, page, component_name): |
| 77 | """Test how quickly web components are registered and upgraded""" |
| 78 | issues = [] |
| 79 | |
| 80 | file_path = Path(__file__).parent / 'docs' / component_name |
| 81 | |
| 82 | # Inject performance measurement |
| 83 | page.goto(f'file://{file_path}') |
| 84 | |
| 85 | # Measure time until web components are defined |
| 86 | registration_time = page.evaluate("""() => { |
| 87 | const start = performance.now(); |
| 88 | const components = [ |
| 89 | 'semantic-action', |
| 90 | 'semantic-card', |
| 91 | 'semantic-input', |
| 92 | 'semantic-modal', |
| 93 | 'semantic-navigator', |
| 94 | 'semantic-list', |
| 95 | 'semantic-menu', |
| 96 | 'semantic-feedback', |
| 97 | 'semantic-adjuster' |
| 98 | ]; |
| 99 | |
| 100 | const promises = components.map(name => customElements.whenDefined(name)); |
| 101 | return Promise.all(promises).then(() => performance.now() - start); |
| 102 | }""") |
| 103 | |
| 104 | # Web components should be registered quickly |
| 105 | # < 50ms: Excellent |
| 106 | # 50-100ms: Good |
| 107 | # > 100ms: Slow |
| 108 | |
| 109 | if registration_time > 100: |
| 110 | issues.append({ |
| 111 | 'type': 'slow_component_registration', |
| 112 | 'registration_time': f"{registration_time:.1f}ms", |
| 113 | 'threshold': '100ms', |
| 114 | 'severity': 'warning', |
| 115 | 'message': f"Web component registration took {registration_time:.1f}ms" |
| 116 | }) |
| 117 | |
| 118 | return { |
| 119 | 'component': component_name, |
| 120 | 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, |
| 121 | 'registration_time_ms': round(registration_time, 2), |
| 122 | 'issues': issues |
| 123 | } |
| 124 | |
| 125 | def test_render_performance(self, page, component_name): |
| 126 | """Test first paint and first contentful paint""" |
| 127 | issues = [] |
| 128 | |
| 129 | file_path = Path(__file__).parent / 'docs' / component_name |
| 130 | page.goto(f'file://{file_path}') |
| 131 | page.wait_for_load_state('networkidle') |
| 132 | |
| 133 | # Get paint metrics |
| 134 | paint_metrics = page.evaluate("""() => { |
| 135 | const paints = performance.getEntriesByType('paint'); |
| 136 | const result = {}; |
| 137 | |
| 138 | paints.forEach(paint => { |
| 139 | result[paint.name] = paint.startTime; |
| 140 | }); |
| 141 | |
| 142 | return result; |
| 143 | }""") |
| 144 | |
| 145 | # First Contentful Paint thresholds: |
| 146 | # < 1000ms: Good |
| 147 | # 1000-2500ms: Needs improvement |
| 148 | # > 2500ms: Poor |
| 149 | |
| 150 | fcp = paint_metrics.get('first-contentful-paint', 0) |
| 151 | if fcp > 2500: |
| 152 | issues.append({ |
| 153 | 'type': 'poor_fcp', |
| 154 | 'fcp': f"{fcp:.0f}ms", |
| 155 | 'threshold': '2500ms', |
| 156 | 'severity': 'error', |
| 157 | 'message': f"First Contentful Paint {fcp:.0f}ms exceeds 2500ms" |
| 158 | }) |
| 159 | elif fcp > 1000: |
| 160 | issues.append({ |
| 161 | 'type': 'slow_fcp', |
| 162 | 'fcp': f"{fcp:.0f}ms", |
| 163 | 'severity': 'warning', |
| 164 | 'message': f"First Contentful Paint {fcp:.0f}ms could be improved" |
| 165 | }) |
| 166 | |
| 167 | return { |
| 168 | 'component': component_name, |
| 169 | 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, |
| 170 | 'paint_metrics': paint_metrics, |
| 171 | 'issues': issues |
| 172 | } |
| 173 | |
| 174 | def test_memory_usage(self, page, component_name): |
| 175 | """Test memory footprint of component""" |
| 176 | issues = [] |
| 177 | |
| 178 | file_path = Path(__file__).parent / 'docs' / component_name |
| 179 | page.goto(f'file://{file_path}') |
| 180 | page.wait_for_load_state('networkidle') |
| 181 | |
| 182 | # Get memory info (Chromium-specific) |
| 183 | memory = page.evaluate("""() => { |
| 184 | if (performance.memory) { |
| 185 | return { |
| 186 | usedJSHeapSize: performance.memory.usedJSHeapSize, |
| 187 | totalJSHeapSize: performance.memory.totalJSHeapSize, |
| 188 | jsHeapSizeLimit: performance.memory.jsHeapSizeLimit |
| 189 | }; |
| 190 | } |
| 191 | return null; |
| 192 | }""") |
| 193 | |
| 194 | if memory: |
| 195 | # Convert to MB |
| 196 | used_mb = memory['usedJSHeapSize'] / (1024 * 1024) |
| 197 | |
| 198 | # Threshold: Single component page shouldn't use more than 10MB |
| 199 | if used_mb > 10: |
| 200 | issues.append({ |
| 201 | 'type': 'high_memory_usage', |
| 202 | 'used_mb': f"{used_mb:.2f}MB", |
| 203 | 'threshold': '10MB', |
| 204 | 'severity': 'warning', |
| 205 | 'message': f"Memory usage {used_mb:.2f}MB exceeds 10MB for single component" |
| 206 | }) |
| 207 | |
| 208 | return { |
| 209 | 'component': component_name, |
| 210 | 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, |
| 211 | 'memory_mb': round(used_mb, 2), |
| 212 | 'issues': issues |
| 213 | } |
| 214 | else: |
| 215 | return { |
| 216 | 'component': component_name, |
| 217 | 'passed': True, |
| 218 | 'memory_mb': None, |
| 219 | 'issues': [{'type': 'memory_api_unavailable', 'severity': 'info'}] |
| 220 | } |
| 221 | |
| 222 | def test_dom_complexity(self, page, component_name): |
| 223 | """Test DOM tree size and depth""" |
| 224 | issues = [] |
| 225 | |
| 226 | file_path = Path(__file__).parent / 'docs' / component_name |
| 227 | page.goto(f'file://{file_path}') |
| 228 | page.wait_for_load_state('networkidle') |
| 229 | |
| 230 | dom_metrics = page.evaluate("""() => { |
| 231 | // Count DOM nodes |
| 232 | const allElements = document.querySelectorAll('*'); |
| 233 | const nodeCount = allElements.length; |
| 234 | |
| 235 | // Calculate max DOM depth |
| 236 | function getDepth(element) { |
| 237 | let depth = 0; |
| 238 | let current = element; |
| 239 | while (current.parentElement) { |
| 240 | depth++; |
| 241 | current = current.parentElement; |
| 242 | } |
| 243 | return depth; |
| 244 | } |
| 245 | |
| 246 | let maxDepth = 0; |
| 247 | allElements.forEach(el => { |
| 248 | const depth = getDepth(el); |
| 249 | if (depth > maxDepth) maxDepth = depth; |
| 250 | }); |
| 251 | |
| 252 | // Count shadow DOM elements |
| 253 | const componentsWithShadow = Array.from(allElements).filter( |
| 254 | el => el.shadowRoot |
| 255 | ).length; |
| 256 | |
| 257 | return { |
| 258 | nodeCount, |
| 259 | maxDepth, |
| 260 | shadowDomCount: componentsWithShadow |
| 261 | }; |
| 262 | }""") |
| 263 | |
| 264 | # Thresholds: |
| 265 | # DOM nodes: < 1500 good, 1500-3000 acceptable, > 3000 excessive |
| 266 | # DOM depth: < 32 good, > 32 problematic |
| 267 | |
| 268 | if dom_metrics['nodeCount'] > 3000: |
| 269 | issues.append({ |
| 270 | 'type': 'excessive_dom_size', |
| 271 | 'node_count': dom_metrics['nodeCount'], |
| 272 | 'threshold': '3000', |
| 273 | 'severity': 'error', |
| 274 | 'message': f"DOM has {dom_metrics['nodeCount']} nodes (>3000 threshold)" |
| 275 | }) |
| 276 | elif dom_metrics['nodeCount'] > 1500: |
| 277 | issues.append({ |
| 278 | 'type': 'large_dom_size', |
| 279 | 'node_count': dom_metrics['nodeCount'], |
| 280 | 'severity': 'warning', |
| 281 | 'message': f"DOM has {dom_metrics['nodeCount']} nodes (>1500)" |
| 282 | }) |
| 283 | |
| 284 | if dom_metrics['maxDepth'] > 32: |
| 285 | issues.append({ |
| 286 | 'type': 'excessive_dom_depth', |
| 287 | 'max_depth': dom_metrics['maxDepth'], |
| 288 | 'threshold': '32', |
| 289 | 'severity': 'warning', |
| 290 | 'message': f"DOM depth is {dom_metrics['maxDepth']} levels (>32)" |
| 291 | }) |
| 292 | |
| 293 | return { |
| 294 | 'component': component_name, |
| 295 | 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, |
| 296 | 'dom_metrics': dom_metrics, |
| 297 | 'issues': issues |
| 298 | } |
| 299 | |
| 300 | def test_reflow_performance(self, page, component_name): |
| 301 | """Test layout reflow performance""" |
| 302 | issues = [] |
| 303 | |
| 304 | file_path = Path(__file__).parent / 'docs' / component_name |
| 305 | page.goto(f'file://{file_path}') |
| 306 | page.wait_for_load_state('networkidle') |
| 307 | |
| 308 | # Trigger reflows and measure |
| 309 | reflow_time = page.evaluate("""() => { |
| 310 | const start = performance.now(); |
| 311 | const iterations = 100; |
| 312 | |
| 313 | // Force layout reflows |
| 314 | for (let i = 0; i < iterations; i++) { |
| 315 | document.body.offsetHeight; // Force reflow |
| 316 | } |
| 317 | |
| 318 | return (performance.now() - start) / iterations; |
| 319 | }""") |
| 320 | |
| 321 | # Each reflow should be < 1ms on average |
| 322 | if reflow_time > 1: |
| 323 | issues.append({ |
| 324 | 'type': 'slow_reflow', |
| 325 | 'reflow_time': f"{reflow_time:.2f}ms", |
| 326 | 'threshold': '1ms', |
| 327 | 'severity': 'warning', |
| 328 | 'message': f"Layout reflow takes {reflow_time:.2f}ms (>1ms)" |
| 329 | }) |
| 330 | |
| 331 | return { |
| 332 | 'component': component_name, |
| 333 | 'passed': len([i for i in issues if i['severity'] == 'error']) == 0, |
| 334 | 'reflow_time_ms': round(reflow_time, 3), |
| 335 | 'issues': issues |
| 336 | } |
| 337 | |
| 338 | def run_all_tests(self): |
| 339 | """Run all performance tests""" |
| 340 | print("\n⚡ Running performance tests...\n") |
| 341 | |
| 342 | components = [ |
| 343 | 'semantic-action.html', |
| 344 | 'semantic-card.html', |
| 345 | 'semantic-input.html', |
| 346 | 'semantic-modal.html', |
| 347 | 'semantic-navigator.html', |
| 348 | 'semantic-list.html', |
| 349 | 'semantic-menu.html', |
| 350 | 'semantic-feedback.html', |
| 351 | 'semantic-adjuster.html' |
| 352 | ] |
| 353 | |
| 354 | with sync_playwright() as p: |
| 355 | browser = p.chromium.launch(headless=True) |
| 356 | page = browser.new_page(viewport={'width': 1400, 'height': 1200}) |
| 357 | |
| 358 | for component in components: |
| 359 | print(f"\n📋 Testing {component}:") |
| 360 | |
| 361 | tests = [ |
| 362 | ('Load Time', lambda: self.test_component_load_time(page, component)), |
| 363 | ('Component Registration', lambda: self.test_web_component_registration(page, component)), |
| 364 | ('Render Performance', lambda: self.test_render_performance(page, component)), |
| 365 | ('Memory Usage', lambda: self.test_memory_usage(page, component)), |
| 366 | ('DOM Complexity', lambda: self.test_dom_complexity(page, component)), |
| 367 | ('Reflow Performance', lambda: self.test_reflow_performance(page, component)) |
| 368 | ] |
| 369 | |
| 370 | for test_name, test_func in tests: |
| 371 | print(f" 🔍 {test_name}...", end='') |
| 372 | result = test_func() |
| 373 | self.results.append({ |
| 374 | 'test': test_name, |
| 375 | 'component': component, |
| 376 | 'passed': result['passed'], |
| 377 | 'issues': result.get('issues', []) |
| 378 | }) |
| 379 | |
| 380 | error_count = len([i for i in result.get('issues', []) if i['severity'] == 'error']) |
| 381 | warning_count = len([i for i in result.get('issues', []) if i['severity'] == 'warning']) |
| 382 | |
| 383 | if result['passed']: |
| 384 | if warning_count > 0: |
| 385 | print(f" ⚠️ ({warning_count} warnings)") |
| 386 | else: |
| 387 | print(" ✅") |
| 388 | else: |
| 389 | print(f" ❌ ({error_count} errors)") |
| 390 | |
| 391 | browser.close() |
| 392 | |
| 393 | self.generate_report() |
| 394 | |
| 395 | def generate_report(self): |
| 396 | """Generate test report""" |
| 397 | passed = sum(1 for r in self.results if r['passed']) |
| 398 | total = len(self.results) |
| 399 | |
| 400 | # Save JSON |
| 401 | results_path = Path(__file__).parent / 'test-results' / 'results-performance.json' |
| 402 | results_path.parent.mkdir(exist_ok=True) |
| 403 | with open(results_path, 'w') as f: |
| 404 | json.dump(self.results, f, indent=2) |
| 405 | |
| 406 | print(f"\n📊 Performance tests complete!") |
| 407 | print(f" Passed: {passed}/{total}") |
| 408 | print(f" Results: {results_path}") |
| 409 | |
| 410 | # Exit with error if any tests failed |
| 411 | if passed < total: |
| 412 | exit(1) |
| 413 | |
| 414 | if __name__ == '__main__': |
| 415 | tester = PerformanceTester() |
| 416 | tester.run_all_tests() |