|
| 1 | +# Playwright Performance Metrics |
| 2 | + |
| 3 | +A comprehensive performance metrics collector for Playwright tests. Collect and analyze web vital metrics, network timing, and resource usage in your Playwright tests. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Collect Web Vitals (LCP, FID, CLS) |
| 8 | +- Network timing metrics (TTFB, resource timing) |
| 9 | +- Network condition emulation (3G, 4G, WiFi) |
| 10 | +- Resource usage tracking |
| 11 | +- TypeScript support |
| 12 | +- Easy integration with Playwright tests |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```bash |
| 17 | +npm install -D playwright-performance-metrics |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +Basic usage: |
| 23 | + |
| 24 | +```typescript |
| 25 | +import { test } from '@playwright/test'; |
| 26 | +import { PerformanceMetricsCollector } from 'playwright-performance-metrics'; |
| 27 | + |
| 28 | +test('measure page performance', async ({ page }) => { |
| 29 | + const collector = new PerformanceMetricsCollector(page); |
| 30 | + |
| 31 | + await page.goto('https://example.com'); |
| 32 | + |
| 33 | + const metrics = await collector.collectMetrics({ |
| 34 | + timeout: 10000, |
| 35 | + initialDelay: 1000 |
| 36 | + }); |
| 37 | + |
| 38 | + console.log('Performance metrics:', { |
| 39 | + 'First Paint': metrics.paint?.firstPaint, |
| 40 | + 'First Contentful Paint': metrics.paint?.firstContentfulPaint, |
| 41 | + 'Largest Contentful Paint': metrics.largestContentfulPaint, |
| 42 | + 'Cumulative Layout Shift': metrics.cumulativeLayoutShift |
| 43 | + }); |
| 44 | +}); |
| 45 | +``` |
| 46 | + |
| 47 | +With network emulation: |
| 48 | + |
| 49 | +```typescript |
| 50 | +import { DefaultNetworkPresets } from 'playwright-performance-metrics'; |
| 51 | + |
| 52 | +test('measure performance with slow 3G', async ({ page }) => { |
| 53 | + const collector = new PerformanceMetricsCollector(page); |
| 54 | + |
| 55 | + const metrics = await collector.collectMetrics({ |
| 56 | + networkConditions: DefaultNetworkPresets.SLOW_3G, |
| 57 | + timeout: 30000 |
| 58 | + }); |
| 59 | + |
| 60 | + console.log('Slow 3G metrics:', metrics); |
| 61 | +}); |
| 62 | +``` |
| 63 | + |
| 64 | +## API Reference |
| 65 | + |
| 66 | +### PerformanceMetricsCollector |
| 67 | + |
| 68 | +Main class for collecting performance metrics. |
| 69 | + |
| 70 | +#### Constructor |
| 71 | + |
| 72 | +```typescript |
| 73 | +constructor(page: Page) |
| 74 | +``` |
| 75 | + |
| 76 | +#### Methods |
| 77 | + |
| 78 | +##### collectMetrics(options?: PerformanceOptions): Promise<PerformanceMetrics> |
| 79 | + |
| 80 | +Collects performance metrics from the page. |
| 81 | + |
| 82 | +Options: |
| 83 | +- `startMark`: Performance mark to use as start time |
| 84 | +- `endMark`: Performance mark to use as end time |
| 85 | +- `timeout`: Collection timeout in milliseconds |
| 86 | +- `initialDelay`: Initial delay before starting collection |
| 87 | +- `retryTimeout`: Maximum time to retry collecting metrics |
| 88 | +- `networkConditions`: Network conditions to emulate |
| 89 | + |
| 90 | +### Network Presets |
| 91 | + |
| 92 | +Available network presets: |
| 93 | +- `REGULAR_4G` |
| 94 | +- `SLOW_3G` |
| 95 | +- `FAST_WIFI` |
| 96 | + |
| 97 | +## Contributing |
| 98 | + |
| 99 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 100 | + |
| 101 | +## License |
| 102 | + |
| 103 | +MIT License - see LICENSE file for details |
0 commit comments