https://github.com/playwright-php/performance
Measure Core Web Vitals and browser performance timings from PHP
https://github.com/playwright-php/performance
browser core-web-vitals network performance php-browser playwright playwright-php testing timing web-performance
Last synced: about 14 hours ago
JSON representation
Measure Core Web Vitals and browser performance timings from PHP
- Host: GitHub
- URL: https://github.com/playwright-php/performance
- Owner: playwright-php
- License: mit
- Created: 2025-10-12T20:13:21.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-11-08T13:39:42.000Z (2 months ago)
- Last Synced: 2025-11-08T15:19:16.657Z (2 months ago)
- Topics: browser, core-web-vitals, network, performance, php-browser, playwright, playwright-php, testing, timing, web-performance
- Language: PHP
- Homepage: https://playwright-php.dev
- Size: 29.3 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Notice: NOTICE
Awesome Lists containing this project
README




# Playwright PHP - Performance
> [!IMPORTANT]
> This package is **experimental**. Its API may still change before the upcoming `1.0` release.
>
> Curious or interested? Try it out, [share your feedback](https://github.com/playwright-php/performance/issues), or ideas!
The Performance package helps you inspect how a page behaves in a real browser
by extracting Core Web Vitals and network timing data with a single API.
## Features
- Capture all Core Web Vitals directly from the browser with resilient fallbacks:
- **LCP** (Largest Contentful Paint) - Loading performance
- **FCP** (First Contentful Paint) - Initial render timing
- **CLS** (Cumulative Layout Shift) - Visual stability
- **INP** (Interaction to Next Paint) - Responsiveness (Core Web Vital as of 2024)
- **FID** (First Input Delay) - Input responsiveness
- **TTFB** (Time to First Byte) - Server response time
- **TBT** (Total Blocking Time) - Main thread blocking
- Collect resource timing entries and expose them as value objects for downstream analysis.
## Getting Started
### Installation
```bash
composer require --dev playwright-php/performance
```
## Usage
```php
use Playwright\Performance\Monitor\PerformanceMonitor;
use Playwright\Playwright;
$browser = Playwright::chromium();
$page = $browser->newPage();
$monitor = new PerformanceMonitor($page);
$monitor->navigate('https://example.com');
$resources = $monitor->collectResourceMetrics();
// Core Web Vitals
$vitals = $monitor->collectCoreWebVitals();
// Resource Metrics
$resources = $monitor->collectResourceMetrics();
$browser->close();
```
### Core Web Vitals
```php
// ...
// $vitals = $monitor->collectCoreWebVitals();
echo $vitals->lcp; // Largest Contentful Paint (ms)
echo $vitals->fcp; // First Contentful Paint (ms)
echo $vitals->cls; // Cumulative Layout Shift
echo $vitals->inp; // Interaction to Next Paint (ms)
echo $vitals->fid; // First Input Delay
echo $vitals->ttfb; // Time to First Byte (ms)
echo $vitals->tbt; // Total Blocking Time (ms)
```
### Resources Loaded
```php
// ...
// $resources = $monitor->collectResourceMetrics();
foreach ($resources as $resource) {
echo $resource->toArray();
}
```
### Format Results
```php
use Playwright\Performance\Reporter\JsonReporter;
use Playwright\Performance\Reporter\MarkdownReporter;
// ...
// $resources = $monitor->collectResourceMetrics();
// JSON (default)
$reporter = new JsonReporter();
file_put_contents('report.json', $reporter->generate($vitals, $resources));
// Markdown
$reporter = new MarkdownReporter();
file_put_contents('report.md', $reporter->generate($vitals, $resources));
```
## Testing
Use `MockPerformanceMonitor` to test your code without launching a browser:
```php
use Playwright\Performance\Monitor\MockPerformanceMonitor;
use Playwright\Performance\Metrics\CoreWebVitals;
class MyServiceTest extends TestCase
{
public function testPerformanceCheck(): void
{
$mock = new MockPerformanceMonitor();
// Define expected values (optional)
$mock->setCoreWebVitals(new CoreWebVitals(100.0, 50.0, 0.01, 0.0, 0.0, 80.0, 0.0));
$service = new MyService($mock);
// No real browser is launched here
$service->analyzePerformance('https://example.com');
}
}
```
The package also includes a PHPUnit trait with performance assertions. See the
full documentation for details.
## License
This package is released by the [Playwright PHP](https://playwright-php.dev)
project under the MIT License. See the [LICENSE](LICENSE) file for details.