Skip to content

Repository files navigation

Playwright PHP

PHP VersionCIReleaseLicense

Playwright PHP - Performance

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

composer require --dev playwright-php/performance

Usage

usePlaywright\Performance\Monitor\PerformanceMonitor;
usePlaywright\Playwright;
$browser = Playwright::chromium();
$page = $browser->newPage();
$monitor = newPerformanceMonitor($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

// ...// $vitals = $monitor->collectCoreWebVitals();echo$vitals->lcp; // Largest Contentful Paint (ms)echo$vitals->fcp; // First Contentful Paint (ms)echo$vitals->cls; // Cumulative Layout Shiftecho$vitals->inp; // Interaction to Next Paint (ms)echo$vitals->fid; // First Input Delayecho$vitals->ttfb; // Time to First Byte (ms)echo$vitals->tbt; // Total Blocking Time (ms)

Resources Loaded

// ...// $resources = $monitor->collectResourceMetrics();foreach ($resourcesas$resource) {
echo$resource->toArray();
}

Format Results

usePlaywright\Performance\Reporter\JsonReporter;
usePlaywright\Performance\Reporter\MarkdownReporter;
// ...// $resources = $monitor->collectResourceMetrics();// JSON (default)$reporter = newJsonReporter();
file_put_contents('report.json', $reporter->generate($vitals, $resources));
// Markdown$reporter = newMarkdownReporter();
file_put_contents('report.md', $reporter->generate($vitals, $resources));

Testing

Use MockPerformanceMonitor to test your code without launching a browser:

usePlaywright\Performance\Monitor\MockPerformanceMonitor;
usePlaywright\Performance\Metrics\CoreWebVitals;
class MyServiceTest extends TestCase
{
publicfunctiontestPerformanceCheck(): void
{
$mock = newMockPerformanceMonitor();
// Define expected values (optional)$mock->setCoreWebVitals(newCoreWebVitals(100.0, 50.0, 0.01, 0.0, 0.0, 80.0, 0.0));
$service = newMyService($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 project under the MIT License. See the LICENSE file for details.

About

Measure Core Web Vitals and browser performance timings from PHP

Topics

Resources

Code of conduct

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Sponsor this project

Contributors

Languages