Skip to content

Commit 2924e86

Browse files
committed
Adding benchmark file
1 parent fc56745 commit 2924e86

File tree

5 files changed

+54
-2
lines changed

5 files changed

+54
-2
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
/phpunit.xml export-ignore
1313
/**/*Test.php export-ignore
1414
/**/Test** export-ignore
15+
/**/*Bench.php export-ignore

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
2020
### Fixed
2121

2222
- Test suite migrated to PHPUnit 10
23+
- Adding Benchmark test with PHPBench
2324
- Improve Collection immutability with method changes
2425
- **[BC Break]** `ParameterAccess` interface signature updated to use the `Value` interface instead of the `Item` implementation.
2526
- **[BC Break]** `MemberList::remove`, `MemberOrderedMap::remove` and `MemberOrderedMap::keys` methods are moved to their parent interface.

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"phpstan/phpstan-strict-rules": "^1.5.0",
3535
"phpstan/phpstan-phpunit": "^1.3.10",
3636
"phpstan/phpstan-deprecation-rules": "^1.1.2",
37-
"phpunit/phpunit": "^10.0.15"
37+
"phpunit/phpunit": "^10.0.15",
38+
"phpbench/phpbench": "^1.2"
3839
},
3940
"autoload": {
4041
"psr-4": {
@@ -46,14 +47,16 @@
4647
]
4748
},
4849
"scripts": {
50+
"benchmark": "phpbench run --report=default",
4951
"phpcs": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix --dry-run --diff -vvv --allow-risky=yes --ansi",
5052
"phpcs:fix": "php-cs-fixer fix -vvv --allow-risky=yes --ansi",
5153
"phpstan": "phpstan analyse -c phpstan.neon --ansi --xdebug --memory-limit 192M",
5254
"phpunit": "XDEBUG_MODE=coverage phpunit --coverage-text",
5355
"test": [
5456
"@phpunit",
5557
"@phpstan",
56-
"@phpcs"
58+
"@phpcs",
59+
"@benchmark"
5760
]
5861
},
5962
"scripts-descriptions": {

phpbench.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"$schema": "vendor/phpbench/phpbench/phpbench.schema.json",
3+
"runner.bootstrap": "vendor/autoload.php",
4+
"runner.path": ["src"],
5+
"runner.file_pattern": "*Bench.php",
6+
"runner.php_disable_ini": true
7+
}

src/ParserBench.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Bakame\Http\StructuredFields;
6+
7+
use PhpBench\Attributes as Bench;
8+
9+
final class ParserBench
10+
{
11+
#[Bench\OutputTimeUnit('seconds')]
12+
#[Bench\Assert('mode(variant.mem.peak) < 2097152'), Bench\Assert('mode(variant.time.avg) < 10000000')]
13+
public function benchParsingAListFormAnHTTPHeaderValue(): void
14+
{
15+
$httpValue = '("lang" "en-US"); expires=@1623233894; samesite=Strict; secure';
16+
for ($i = 0; $i < 100_000; $i++) {
17+
Parser::parseList($httpValue);
18+
}
19+
}
20+
21+
#[Bench\OutputTimeUnit('seconds')]
22+
#[Bench\Assert('mode(variant.mem.peak) < 2097152'), Bench\Assert('mode(variant.time.avg) < 10000000')]
23+
public function benchParsingAnItemFormAnHTTPHeaderValue(): void
24+
{
25+
$httpValue = '"lang"; expires=@1623233894; samesite=Strict; secure';
26+
for ($i = 0; $i < 100_000; $i++) {
27+
Item::fromHttpValue($httpValue);
28+
}
29+
}
30+
31+
#[Bench\OutputTimeUnit('seconds')]
32+
#[Bench\Assert('mode(variant.mem.peak) < 2097152'), Bench\Assert('mode(variant.time.avg) < 10000000')]
33+
public function benchParsingAnDictionaryFormAnHTTPHeaderValue(): void
34+
{
35+
$httpValue = 'lang="en-US", type=42.0; expires=@1623233894; samesite=Strict; secure';
36+
for ($i = 0; $i < 100_000; $i++) {
37+
Parser::parseDictionary($httpValue);
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)