Skip to content

Commit

Permalink
PHPBench (#8)
Browse files Browse the repository at this point in the history
* Benchmark

* Add limit set

* Bench react http client
  • Loading branch information
jenky authored Sep 29, 2023
1 parent 750262e commit 76a961b
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"friendsofphp/php-cs-fixer": "^3.15",
"guzzlehttp/guzzle": "^7.5",
"jenky/atlas": "^0.5",
"phpbench/phpbench": "^1.2",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^9.0",
"react/async": "^4.1",
Expand All @@ -53,6 +54,9 @@
"analyse": [
"vendor/bin/phpstan analyse"
],
"bench": [
"./vendor/bin/phpbench run --report=aggregate"
],
"cs": [
"vendor/bin/php-cs-fixer fix"
],
Expand Down
6 changes: 6 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema":"./vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "tests/Benchmark",
"runner.file_pattern": "*Bench.php"
}
15 changes: 15 additions & 0 deletions tests/Benchmark/BenchTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Fansipan\Peak\Tests\Benchmark;

trait BenchTrait
{
public function provideLimits(): \Generator
{
yield '50 rqs' => ['limit' => 50];
yield '100 rqs' => ['limit' => 100];
yield '200 rqs' => ['limit' => 200];
}
}
28 changes: 28 additions & 0 deletions tests/Benchmark/DefaultBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Fansipan\Peak\Tests\Benchmark;

use Fansipan\Peak\Tests\TestRequestTrait;
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use GuzzleHttp\RequestOptions;
use PhpBench\Attributes\ParamProviders;

final class DefaultBench
{
use BenchTrait;
use TestRequestTrait;

#[ParamProviders(['provideLimits'])]
public function benchGuzzlePool(array $params): void
{
$options = [
RequestOptions::ALLOW_REDIRECTS => false,
RequestOptions::HTTP_ERRORS => false,
];

Pool::batch(new Client($options), $this->createPsrRequests($params['limit']));
}
}
34 changes: 34 additions & 0 deletions tests/Benchmark/PslBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Fansipan\Peak\Tests\Benchmark;

use Fansipan\Peak\Client\GuzzleClient;
use Fansipan\Peak\Client\SymfonyClient;
use Fansipan\Peak\Concurrency\PslDeferred;
use Fansipan\Peak\PoolFactory;
use Fansipan\Peak\Tests\TestRequestTrait;
use PhpBench\Attributes\ParamProviders;

final class PslBench
{
use BenchTrait;
use TestRequestTrait;

#[ParamProviders(['provideLimits'])]
public function benchPslPoolUsingGuzzle(array $params): void
{
PoolFactory::createFromClient(
new GuzzleClient(new PslDeferred())
)->send($this->createPsrRequests($params['limit']));
}

#[ParamProviders(['provideLimits'])]
public function benchPslPoolUsingSymfony(array $params): void
{
PoolFactory::createFromClient(
new SymfonyClient(new PslDeferred())
)->send($this->createPsrRequests($params['limit']));
}
}
42 changes: 42 additions & 0 deletions tests/Benchmark/ReactBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Fansipan\Peak\Tests\Benchmark;

use Fansipan\Peak\Client\GuzzleClient;
use Fansipan\Peak\Client\ReactClient;
use Fansipan\Peak\Client\SymfonyClient;
use Fansipan\Peak\Concurrency\ReactDeferred;
use Fansipan\Peak\PoolFactory;
use Fansipan\Peak\Tests\TestRequestTrait;
use PhpBench\Attributes\ParamProviders;

final class ReactBench
{
use BenchTrait;
use TestRequestTrait;

#[ParamProviders(['provideLimits'])]
public function benchReactPoolUsingGuzzle(array $params): void
{
PoolFactory::createFromClient(
new GuzzleClient(new ReactDeferred())
)->send($this->createPsrRequests($params['limit']));
}

#[ParamProviders(['provideLimits'])]
public function benchReactPoolUsingSymfony(array $params): void
{
PoolFactory::createFromClient(
new SymfonyClient(new ReactDeferred())
)->send($this->createPsrRequests($params['limit']));
}

#[ParamProviders(['provideLimits'])]
public function benchReactPoolUsingHttpClient(array $params): void
{
PoolFactory::createFromClient(new ReactClient())
->send($this->createPsrRequests($params['limit']));
}
}

0 comments on commit 76a961b

Please sign in to comment.