Collect Core Web Vitals and resource timing data from pages opened with Playwright PHP.
composer require --dev playwright-php/performance
vendor/bin/playwright-install --browsersThe package requires PHP 8.2+ and Playwright PHP 1.x.
Navigate to a page, collect its metrics, and generate a report:
<?php
require __DIR__.'/vendor/autoload.php';
use Playwright\Performance\Monitor\PerformanceMonitor;
use Playwright\Performance\Reporter\JsonReporter;
use Playwright\Playwright;
$context = Playwright::chromium();
$page = $context->newPage();
$monitor = new PerformanceMonitor($page);
$monitor->navigate('https://example.com');
$vitals = $monitor->collectCoreWebVitals();
$resources = $monitor->collectResourceMetrics();
$report = (new JsonReporter())->generate($vitals, $resources);
file_put_contents('performance.json', $report);
$context->close();CoreWebVitals exposes LCP, FCP, CLS, INP, FID, TTFB, and TBT as public readonly properties and through getter methods.
Each ResourceMetrics value contains the URL, resource type, duration, transfer size, and detailed network timing values.
Use PerformanceAssertions with the core Playwright test case to enforce broad, stable budgets:
use Playwright\Performance\Monitor\PerformanceMonitor;
use Playwright\Performance\Test\PerformanceAssertions;
use Playwright\Testing\PlaywrightTestCase;
final class HomepagePerformanceTest extends PlaywrightTestCase
{
use PerformanceAssertions;
public function testHomepageBudgets(): void
{
$monitor = new PerformanceMonitor($this->page);
$monitor->navigate('https://example.com');
$vitals = $monitor->collectCoreWebVitals();
$resources = $monitor->collectResourceMetrics();
$this->assertLcpBelowThreshold($vitals, 2500);
$this->assertClsBelowThreshold($vitals, 0.1);
$this->assertResourceCountBelowThreshold($resources, 50);
}
}Shared CI runners vary. Use budgets that detect meaningful regressions instead of asserting exact timings.
JsonReporter produces structured JSON. MarkdownReporter produces a readable summary with Core Web Vitals and the slowest resources.
use Playwright\Performance\Reporter\MarkdownReporter;
$markdown = (new MarkdownReporter())->generate($vitals, $resources);
file_put_contents('performance.md', $markdown);MockPerformanceMonitor implements the same interface as the browser-backed monitor:
use Playwright\Performance\Metrics\CoreWebVitals;
use Playwright\Performance\Monitor\MockPerformanceMonitor;
$monitor = new MockPerformanceMonitor();
$monitor->setCoreWebVitals(new CoreWebVitals(
lcp: 1000.0,
fcp: 500.0,
cls: 0.05,
inp: 0.0,
fid: 0.0,
ttfb: 100.0,
tbt: 0.0,
));- Automated browser metrics are laboratory data, not real-user monitoring.
- INP and FID require user interaction and may remain zero in page-load tests.
- Browser and runner variance can make narrow timing thresholds unreliable.
Contributions are welcome. Before submitting a pull request, run:
composer validate --strict
vendor/bin/php-cs-fixer fix --dry-run --diff
vendor/bin/phpstan analyse
vendor/bin/phpunitPlaywright PHP Performance is released under the MIT License.
