Skip to content

Commit 743913b

Browse files
committed
playwright-php/performance
0 parents  commit 743913b

32 files changed

+2645
-0
lines changed

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/.git* export-ignore
2+
/docs/ export-ignore
3+
/tests/ export-ignore
4+
/.php-cs-fixer* export-ignore
5+
/phpstan* export-ignore
6+
/phpunit* export-ignore

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: [smnandre]

.github/workflows/CI.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ "*" ]
6+
pull_request:
7+
branches: [ "*" ]
8+
workflow_dispatch:
9+
10+
permissions:
11+
contents: read
12+
13+
concurrency:
14+
group: ${{ github.workflow }}-${{ github.ref }}
15+
cancel-in-progress: true
16+
17+
jobs:
18+
19+
cs:
20+
uses: playwright-php/.github/.github/workflows/CS.yml@main
21+
# with:
22+
# php-version: '8.4'
23+
# composer-validate: true
24+
# php-cs-fixer-args: '--diff --dry-run'
25+
26+
sa:
27+
uses: playwright-php/.github/.github/workflows/SA.yml@main
28+
# with:
29+
# php-version: '8.4'
30+
# phpstan-args: 'analyse --no-progress --memory-limit=-1'

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/.phpunit.cache/
2+
/vendor/
3+
/.php-cs-fixer.cache
4+
/composer.lock
5+
/phpstan.neon
6+
/phpunit.xml

.php-cs-fixer.dist.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
$license = <<<LICENSE
4+
This file is part of the community-maintained Playwright PHP project.
5+
It is not affiliated with or endorsed by Microsoft.
6+
7+
(c) 2025-Present - Playwright PHP - https://github.com/playwright-php
8+
9+
For the full copyright and license information, please view the LICENSE
10+
file that was distributed with this source code.
11+
LICENSE;
12+
13+
$finder = (new PhpCsFixer\Finder())
14+
->in(__DIR__);
15+
16+
return (new PhpCsFixer\Config())
17+
->setParallelConfig(PhpCsFixer\Runner\Parallel\ParallelConfigFactory::detect())
18+
->setFinder($finder)
19+
->setRiskyAllowed(true)
20+
->setRules([
21+
'@Symfony' => true,
22+
'declare_strict_types' => true,
23+
'header_comment' => ['header' => $license],
24+
])
25+
;

LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
MIT License
2+
3+
Copyright (c) 2025-present Simon André & Playwright PHP
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
23+
---
24+
25+
See NOTICE file for additional details.

NOTICE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
THIRD-PARTY SOFTWARE NOTICES AND INFORMATION
2+
3+
This project depends on Playwright, an open-source project by Microsoft,
4+
available at https://github.com/microsoft/playwright and licensed under
5+
the Apache License 2.0 (https://github.com/microsoft/playwright/blob/main/LICENSE).
6+
"Playwright" is a trademark of Microsoft. This project is independent and not
7+
affiliated with or endorsed by Microsoft.

README.md

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<div align="center">
2+
<img src="https://github.com/playwright-php/.github/raw/main/profile/playwright-php.png" alt="Playwright PHP" />
3+
4+
&nbsp; ![PHP Version](https://img.shields.io/badge/PHP-8.2-05971B?labelColor=09161E&color=1D8D23&logoColor=FFFFFF)
5+
&nbsp; ![CI](https://img.shields.io/github/actions/workflow/status/playwright-php/performance/CI.yaml?branch=main&label=Tests&color=1D8D23&labelColor=09161E&logoColor=FFFFFF)
6+
&nbsp; ![Release](https://img.shields.io/github/v/release/playwright-php/performance?label=Stable&labelColor=09161E&color=1D8D23&logoColor=FFFFFF)
7+
&nbsp; ![License](https://img.shields.io/github/license/playwright-php/performance?label=License&labelColor=09161E&color=1D8D23&logoColor=FFFFFF)
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.

composer.json

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"name": "playwright-php/performance",
3+
"description": "Performance monitoring for Playwright PHP: collect Core Web Vitals and resource metrics",
4+
"license": "MIT",
5+
"type": "library",
6+
"keywords": [
7+
"playwright",
8+
"playwright-php",
9+
"performance",
10+
"monitoring",
11+
"core-web-vitals",
12+
"lcp",
13+
"fcp",
14+
"cls",
15+
"web-performance",
16+
"metrics"
17+
],
18+
"authors": [
19+
{
20+
"name": "Simon André",
21+
"email": "smn.andre@gmail.com",
22+
"homepage": "https://github.com/smnandre"
23+
},
24+
{
25+
"name": "Playwright PHP Contributors",
26+
"homepage": "https://github.com/playwright-php"
27+
}
28+
],
29+
"require": {
30+
"php": "^8.2",
31+
"playwright-php/playwright": "dev-main"
32+
},
33+
"require-dev": {
34+
"friendsofphp/php-cs-fixer": "^3.88",
35+
"phpstan/phpstan": "^2.1",
36+
"phpunit/phpunit": "^12.4"
37+
},
38+
"minimum-stability": "dev",
39+
"prefer-stable": true,
40+
"autoload": {
41+
"psr-4": {
42+
"Playwright\\Performance\\": "src/"
43+
}
44+
},
45+
"autoload-dev": {
46+
"psr-4": {
47+
"Playwright\\Performance\\Tests\\": "tests/"
48+
}
49+
},
50+
"config": {
51+
"allow-plugins": {
52+
"phpstan/extension-installer": true
53+
},
54+
"sort-packages": true
55+
},
56+
"extra": {
57+
"branch-alias": {
58+
"dev-main": "0.x-dev"
59+
},
60+
"thanks": {
61+
"name": "playwright-php/playwright",
62+
"url": "https://github.com/playwright-php/playwright"
63+
}
64+
}
65+
}

phpstan.dist.neon

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
parameters:
2+
level: 8
3+
paths:
4+
- src/
5+
treatPhpDocTypesAsCertain: false
6+
ignoreErrors:
7+
-
8+
message: '#^Trait Playwright\\Performance\\Test\\PerformanceAssertions is used zero times and is not analysed\.$#'
9+
path: src/Test/PerformanceAssertions.php

0 commit comments

Comments
 (0)