Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,26 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [ "8.2", "8.3", "8.4" ]
symfony: [ "6.0", "7.0" ]
php: [ "8.2", "8.3", "8.4", "8.5" ]

name: PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }} ${{ matrix.prefer }}
name: PHP ${{ matrix.php }}

steps:
- name: Checkout code
uses: actions/checkout@v6
- uses: actions/checkout@v6

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: curl, mbstring, zip, pcntl, pdo, pdo_sqlite, iconv
coverage: none

- name: Install dependencies
run: |
composer require --no-interaction \
symfony/console:^${{ matrix.symfony }} \
symfony/var-dumper:^${{ matrix.symfony }}
composer update \
--no-audit \
--no-suggest \
--no-interaction


- name: Execute tests
run: composer test
1 change: 1 addition & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
- Метод `prepare` переименован в `beforeEach`
- Метод `start` переименован в `make`
- Исправлена типизация входящих колбэков с супер-класса `callable` на `Closure`
- Класс `ValueIsNotCallableException` теперь принимает объект вместо строки типа.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@
],
"require": {
"php": "^8.2",
"dragon-code/support": "^6.10",
"symfony/console": "^6.0 || ^7.0"
"dragon-code/support": "^6.10"
},
"require-dev": {
"pestphp/pest": "^4.3",
"symfony/var-dumper": "^6.0 || ^7.0"
"symfony/var-dumper": "^7.0 || ^8.0"
},
"minimum-stability": "stable",
"prefer-stable": true,
Expand Down
59 changes: 23 additions & 36 deletions src/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

use Closure;
use DragonCode\Benchmark\Exceptions\ValueIsNotCallableException;
use DragonCode\Benchmark\Services\Callbacks;
use DragonCode\Benchmark\Services\Runner;
use DragonCode\Benchmark\Services\View;
use DragonCode\Benchmark\Transformers\Transformer;
use Symfony\Component\Console\Helper\ProgressBar as ProgressBarService;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use DragonCode\Benchmark\View\ProgressBarView;

use function count;
use function func_get_args;
Expand All @@ -23,30 +21,19 @@

class Benchmark
{
protected View $view;

protected int $iterations = 100;

protected ?Closure $beforeEach = null;

protected ?Closure $afterEach = null;

protected array $result = [
'each' => [],
'total' => [],
];

public function __construct(
protected Runner $runner = new Runner,
protected Transformer $transformer = new Transformer
) {
$this->view = new View(
new SymfonyStyle(
new ArgvInput,
new ConsoleOutput
)
);
}
protected Transformer $transformer = new Transformer,
protected View $view = new View,
protected Callbacks $callbacks = new Callbacks,
) {}

public static function make(): static
{
Expand All @@ -55,14 +42,14 @@ public static function make(): static

public function beforeEach(Closure $callback): self
{
$this->beforeEach = $callback;
$this->callbacks->beforeEach = $callback;

return $this;
}

public function afterEach(Closure $callback): self
{
$this->afterEach = $callback;
$this->callbacks->afterEach = $callback;

return $this;
}
Expand All @@ -81,14 +68,23 @@ public function round(?int $precision): self
return $this;
}

public function compare(array|Closure ...$callbacks): void
public function compare(array|Closure ...$callbacks): static
{
$values = is_array($callbacks[0]) ? $callbacks[0] : func_get_args();

$this->withProgress($values, $this->stepsCount($values));
$this->show();

return $this;
}

/**
* @return \DragonCode\Benchmark\Data\ResultData[]
*/
public function toData(): array {}

public function toConsole(): void {}

protected function withProgress(array $callbacks, int $count): void
{
$bar = $this->view->progressBar()->create($count);
Expand All @@ -104,7 +100,7 @@ protected function stepsCount(array $callbacks): int
return count($callbacks) * $this->iterations;
}

protected function chunks(array $callbacks, ProgressBarService $progressBar): void
protected function chunks(array $callbacks, ProgressBarView $progressBar): void
{
foreach ($callbacks as $name => $callback) {
$this->validate($callback);
Expand All @@ -113,37 +109,28 @@ protected function chunks(array $callbacks, ProgressBarService $progressBar): vo
}
}

protected function each(mixed $name, Closure $callback, ProgressBarService $progressBar): void
protected function each(mixed $name, Closure $callback, ProgressBarView $progressBar): void
{
$this->result['total'][$name] = $this->call(
fn () => $this->run($name, $callback, $progressBar)
);
}

protected function run(mixed $name, Closure $callback, ProgressBarService $progressBar): void
protected function run(mixed $name, Closure $callback, ProgressBarView $progressBar): void
{
for ($i = 1; $i <= $this->iterations; ++$i) {
$result = $this->runCallback($this->beforeEach, $name, $i);
$result = $this->callbacks->performBeforeEach($name, $i);

[$time, $ram] = $this->call($callback, [$i, $result]);

$this->runCallback($this->afterEach, $name, $i, $time, $ram);
$this->callbacks->performAfterEach($name, $i, $time, $ram);

$this->push($name, $i, $time, $ram);

$progressBar->advance();
}
}

protected function runCallback(?Closure $callback, mixed ...$arguments): mixed
{
if (! $callback) {
return null;
}

return $callback(...$arguments);
}

protected function call(Closure $callback, array $parameters = []): array
{
return $this->runner->call($callback, $parameters);
Expand Down
6 changes: 4 additions & 2 deletions src/Exceptions/ValueIsNotCallableException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

use TypeError;

use function gettype;

class ValueIsNotCallableException extends TypeError
{
public function __construct(string $actualType)
public function __construct(mixed $value)
{
parent::__construct(sprintf('The array value must be of type callable, %s given.', $actualType), 500);
parent::__construct(sprintf('The array value must be of type Closure, %s given.', gettype($value)), 500);
}
}
47 changes: 47 additions & 0 deletions src/Services/Callbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace DragonCode\Benchmark\Services;

use Closure;

class Callbacks
{
public ?Closure $before = null;

public ?Closure $beforeEach = null;

public ?Closure $after = null;

public ?Closure $afterEach = null;

public function performBefore(string|int $name): mixed
{
return $this->perform($this->before, $name);
}

public function performBeforeEach(string|int $name, int $iteration): mixed
{
return $this->perform($this->beforeEach, $name, $iteration);
}

public function performAfter(string|int $name): mixed
{
return $this->perform($this->after, $name);
}

public function performAfterEach(string|int $name, int $iteration, float $time, float $ram): mixed
{
return $this->perform($this->afterEach, $name, $iteration, $time, $ram);
}

protected function perform(?Closure $callback, mixed ...$args): mixed
{
if ($callback === null) {
return null;
}

return $callback(...$args);
}
}
23 changes: 8 additions & 15 deletions src/Services/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

namespace DragonCode\Benchmark\Services;

use DragonCode\Benchmark\View\ProgressBar;
use DragonCode\Benchmark\View\Table;
use Symfony\Component\Console\Style\SymfonyStyle;
use DragonCode\Benchmark\View\ProgressBarView;
use DragonCode\Benchmark\View\TableView;

use function is_array;
use function is_numeric;
Expand All @@ -15,19 +14,13 @@

class View
{
protected Table $table;

protected ProgressBar $progressBar;

protected ?int $roundPrecision = null;

public function __construct(
protected SymfonyStyle $io,
protected Memory $memory = new Memory
) {
$this->table = new Table($this->io);
$this->progressBar = new ProgressBar($this->io);
}
protected Memory $memory = new Memory,
protected TableView $table = new TableView,
protected ProgressBarView $progressBar = new ProgressBarView,
) {}

public function setRound(?int $precision): void
{
Expand All @@ -41,14 +34,14 @@ public function table(array $data): void
);
}

public function progressBar(): ProgressBar
public function progressBar(): ProgressBarView
{
return $this->progressBar;
}

public function emptyLine(int $count = 1): void
{
$this->io->newLine($count);
echo "\r";
}

protected function appendMs(array $data): array
Expand Down
2 changes: 0 additions & 2 deletions src/Transformers/Separator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace DragonCode\Benchmark\Transformers;

use Symfony\Component\Console\Helper\TableSeparator;

class Separator extends Base
{
public function transform(array $data): array
Expand Down
20 changes: 0 additions & 20 deletions src/View/ProgressBar.php

This file was deleted.

Loading
Loading