Skip to content

Commit

Permalink
Adding benchmark file
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Mar 12, 2023
1 parent fc56745 commit 2924e86
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
/phpunit.xml export-ignore
/**/*Test.php export-ignore
/**/Test** export-ignore
/**/*Bench.php export-ignore
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ All Notable changes to `bakame/http-strucured-fields` will be documented in this
### Fixed

- Test suite migrated to PHPUnit 10
- Adding Benchmark test with PHPBench
- Improve Collection immutability with method changes
- **[BC Break]** `ParameterAccess` interface signature updated to use the `Value` interface instead of the `Item` implementation.
- **[BC Break]** `MemberList::remove`, `MemberOrderedMap::remove` and `MemberOrderedMap::keys` methods are moved to their parent interface.
Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"phpstan/phpstan-strict-rules": "^1.5.0",
"phpstan/phpstan-phpunit": "^1.3.10",
"phpstan/phpstan-deprecation-rules": "^1.1.2",
"phpunit/phpunit": "^10.0.15"
"phpunit/phpunit": "^10.0.15",
"phpbench/phpbench": "^1.2"
},
"autoload": {
"psr-4": {
Expand All @@ -46,14 +47,16 @@
]
},
"scripts": {
"benchmark": "phpbench run --report=default",
"phpcs": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix --dry-run --diff -vvv --allow-risky=yes --ansi",
"phpcs:fix": "php-cs-fixer fix -vvv --allow-risky=yes --ansi",
"phpstan": "phpstan analyse -c phpstan.neon --ansi --xdebug --memory-limit 192M",
"phpunit": "XDEBUG_MODE=coverage phpunit --coverage-text",
"test": [
"@phpunit",
"@phpstan",
"@phpcs"
"@phpcs",
"@benchmark"
]
},
"scripts-descriptions": {
Expand Down
7 changes: 7 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php",
"runner.path": ["src"],
"runner.file_pattern": "*Bench.php",
"runner.php_disable_ini": true
}
40 changes: 40 additions & 0 deletions src/ParserBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Bakame\Http\StructuredFields;

use PhpBench\Attributes as Bench;

final class ParserBench
{
#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) < 2097152'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchParsingAListFormAnHTTPHeaderValue(): void
{
$httpValue = '("lang" "en-US"); expires=@1623233894; samesite=Strict; secure';
for ($i = 0; $i < 100_000; $i++) {
Parser::parseList($httpValue);
}
}

#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) < 2097152'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchParsingAnItemFormAnHTTPHeaderValue(): void
{
$httpValue = '"lang"; expires=@1623233894; samesite=Strict; secure';
for ($i = 0; $i < 100_000; $i++) {
Item::fromHttpValue($httpValue);
}
}

#[Bench\OutputTimeUnit('seconds')]
#[Bench\Assert('mode(variant.mem.peak) < 2097152'), Bench\Assert('mode(variant.time.avg) < 10000000')]
public function benchParsingAnDictionaryFormAnHTTPHeaderValue(): void
{
$httpValue = 'lang="en-US", type=42.0; expires=@1623233894; samesite=Strict; secure';
for ($i = 0; $i < 100_000; $i++) {
Parser::parseDictionary($httpValue);
}
}
}

0 comments on commit 2924e86

Please sign in to comment.