|
| 1 | +<div align="center"> |
| 2 | +<img src="https://github.com/playwright-php/.github/raw/main/profile/playwright-php.png" alt="Playwright PHP" /> |
| 3 | + |
| 4 | +  |
| 5 | +  |
| 6 | +  |
| 7 | +  |
| 8 | + |
| 9 | +</div> |
| 10 | + |
| 11 | +# Playwright PHP - Performance |
| 12 | + |
| 13 | +The Performance package helps you inspect how a page behaves in a real browser |
| 14 | +by extracting Core Web Vitals and network timing data with a single API. |
| 15 | + |
| 16 | +## Features |
| 17 | + |
| 18 | +- Capture all Core Web Vitals directly from the browser with resilient fallbacks: |
| 19 | + - **LCP** (Largest Contentful Paint) - Loading performance |
| 20 | + - **FCP** (First Contentful Paint) - Initial render timing |
| 21 | + - **CLS** (Cumulative Layout Shift) - Visual stability |
| 22 | + - **INP** (Interaction to Next Paint) - Responsiveness (Core Web Vital as of 2024) |
| 23 | + - **FID** (First Input Delay) - Input responsiveness |
| 24 | + - **TTFB** (Time to First Byte) - Server response time |
| 25 | + - **TBT** (Total Blocking Time) - Main thread blocking |
| 26 | +- Collect resource timing entries and expose them as value objects for downstream analysis. |
| 27 | + |
| 28 | +## Getting Started |
| 29 | + |
| 30 | +### Installation |
| 31 | + |
| 32 | +```bash |
| 33 | +composer require --dev playwright-php/performance |
| 34 | +``` |
| 35 | + |
| 36 | +## Usage |
| 37 | + |
| 38 | +```php |
| 39 | +use Playwright\Performance\Monitor\PerformanceMonitor; |
| 40 | +use Playwright\Playwright; |
| 41 | + |
| 42 | +$browser = Playwright::chromium(); |
| 43 | +$page = $browser->newPage(); |
| 44 | + |
| 45 | +$monitor = new PerformanceMonitor($page); |
| 46 | +$monitor->navigate('https://example.com'); |
| 47 | + |
| 48 | +$resources = $monitor->collectResourceMetrics(); |
| 49 | + |
| 50 | +// Core Web Vitals |
| 51 | +$vitals = $monitor->collectCoreWebVitals(); |
| 52 | + |
| 53 | +// Resource Metrics |
| 54 | +$resources = $monitor->collectResourceMetrics(); |
| 55 | + |
| 56 | +$browser->close(); |
| 57 | +``` |
| 58 | + |
| 59 | +### Core Web Vitals |
| 60 | + |
| 61 | +```php |
| 62 | +// ... |
| 63 | +// $vitals = $monitor->collectCoreWebVitals(); |
| 64 | + |
| 65 | +echo $vitals->lcp; // Largest Contentful Paint (ms) |
| 66 | +echo $vitals->fcp; // First Contentful Paint (ms) |
| 67 | +echo $vitals->cls; // Cumulative Layout Shift |
| 68 | +echo $vitals->inp; // Interaction to Next Paint (ms) |
| 69 | +echo $vitals->fid; // First Input Delay |
| 70 | +echo $vitals->ttfb; // Time to First Byte (ms) |
| 71 | +echo $vitals->tbt; // Total Blocking Time (ms) |
| 72 | +``` |
| 73 | + |
| 74 | +### Resources Loaded |
| 75 | + |
| 76 | +```php |
| 77 | +// ... |
| 78 | +// $resources = $monitor->collectResourceMetrics(); |
| 79 | + |
| 80 | +foreach ($resources as $resource) { |
| 81 | + echo $resource->toArray(); |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +### Format Results |
| 86 | + |
| 87 | +```php |
| 88 | +use Playwright\Performance\Reporter\JsonReporter; |
| 89 | +use Playwright\Performance\Reporter\MarkdownReporter; |
| 90 | + |
| 91 | +// ... |
| 92 | +// $resources = $monitor->collectResourceMetrics(); |
| 93 | + |
| 94 | +// JSON (default) |
| 95 | +$reporter = new JsonReporter(); |
| 96 | +file_put_contents('report.json', $reporter->generate($vitals, $resources)); |
| 97 | + |
| 98 | +// Markdown |
| 99 | +$reporter = new MarkdownReporter(); |
| 100 | +file_put_contents('report.md', $reporter->generate($vitals, $resources)); |
| 101 | +``` |
| 102 | + |
| 103 | +## Testing |
| 104 | + |
| 105 | +Use `MockPerformanceMonitor` to test your code without launching a browser: |
| 106 | + |
| 107 | +```php |
| 108 | +use Playwright\Performance\Monitor\MockPerformanceMonitor; |
| 109 | +use Playwright\Performance\Metrics\CoreWebVitals; |
| 110 | + |
| 111 | +class MyServiceTest extends TestCase |
| 112 | +{ |
| 113 | + public function testPerformanceCheck(): void |
| 114 | + { |
| 115 | + $mock = new MockPerformanceMonitor(); |
| 116 | + |
| 117 | + // Define expected values (optional) |
| 118 | + $mock->setCoreWebVitals(new CoreWebVitals(100.0, 50.0, 0.01, 0.0, 0.0, 80.0, 0.0)); |
| 119 | + $service = new MyService($mock); |
| 120 | + |
| 121 | + // No real browser is launched here |
| 122 | + $service->analyzePerformance('https://example.com'); |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +The package also includes a PHPUnit trait with performance assertions. See the |
| 128 | +full documentation for details. |
| 129 | + |
| 130 | +## License |
| 131 | + |
| 132 | +This package is released by the [Playwright PHP](https://playwright-php.dev) |
| 133 | +project under the MIT License. See the [LICENSE](LICENSE) file for details. |
0 commit comments