diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 73566dc..d046ba9 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -11,7 +11,6 @@ jobs: matrix: php: [ "8.1", "8.2" ] symfony: [ "5.3", "6.0" ] - prefer: [ "stable", "lowest" ] name: PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }} ${{ matrix.prefer }} @@ -24,11 +23,11 @@ jobs: with: php-version: ${{ matrix.php }} extensions: curl, mbstring, zip, pcntl, pdo, pdo_sqlite, iconv - coverage: xdebug + coverage: none - name: Install dependencies run: | - composer require --no-interaction --prefer-${{ matrix.prefer }} \ + composer require --no-interaction \ symfony/console:^${{ matrix.symfony }} \ symfony/var-dumper:^${{ matrix.symfony }} \ diff --git a/phpunit.xml b/phpunit.xml index 833e7c7..76fbd8e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,29 +1,21 @@ - - ./src + src - - - - - - - ./tests + + tests diff --git a/src/Services/Memory.php b/src/Services/Memory.php new file mode 100644 index 0000000..6bfe735 --- /dev/null +++ b/src/Services/Memory.php @@ -0,0 +1,35 @@ + 1024 * 1024 * 1024, + 'MB' => 1024 * 1024, + 'KB' => 1024, + ]; + + public function now(): int + { + return memory_get_usage(true); + } + + public function diff(int $memory): int + { + return memory_get_peak_usage(true) - $memory; + } + + public function format(int $bytes): string + { + foreach ($this->sizes as $unit => $value) { + if ($bytes >= $value) { + return sprintf('%.2f %s', $bytes >= 1024 ? $bytes / $value : $bytes, $unit); + } + } + + return $bytes . ' byte' . ($bytes !== 1 ? 's' : ''); + } +} diff --git a/src/Services/Runner.php b/src/Services/Runner.php index 240d8d8..ed455fd 100644 --- a/src/Services/Runner.php +++ b/src/Services/Runner.php @@ -6,6 +6,11 @@ class Runner { + public function __construct( + protected readonly Memory $memory = new Memory() + ) { + } + public function call(callable $callback): array { $this->clean(); @@ -20,13 +25,13 @@ protected function clean(): void protected function run(callable $callback): array { - $ramFrom = memory_get_usage(); + $ramFrom = $this->memory->now(); $startAt = hrtime(true); $callback(); $time = $this->diff(hrtime(true), $startAt); - $ram = memory_get_peak_usage() - $ramFrom; + $ram = $this->memory->diff($ramFrom); return [$time, $ram]; } diff --git a/src/Services/View.php b/src/Services/View.php index 147ad2c..9b01286 100644 --- a/src/Services/View.php +++ b/src/Services/View.php @@ -6,7 +6,6 @@ use DragonCode\Benchmark\View\ProgressBar; use DragonCode\Benchmark\View\Table; -use DragonCode\Support\Facades\Helpers\Digit; use Symfony\Component\Console\Style\SymfonyStyle; class View @@ -18,7 +17,8 @@ class View protected ?int $roundPrecision = null; public function __construct( - protected SymfonyStyle $io + protected SymfonyStyle $io, + protected Memory $memory = new Memory() ) { $this->table = new Table($this->io); $this->progressBar = new ProgressBar($this->io); @@ -54,7 +54,7 @@ protected function appendMs(array $data): array continue; } - $value = sprintf('%s ms - %sb', $this->roundTime($value['time']), $this->roundRam($value['ram'])); + $value = sprintf('%s ms - %s', $this->roundTime($value['time']), $this->roundRam($value['ram'])); } } @@ -70,8 +70,8 @@ protected function roundTime(float $value): float return $value; } - protected function roundRam(float $value): string + protected function roundRam(int|float $bytes): string { - return Digit::toShort($value); + return $this->memory->format((int) $bytes); } } diff --git a/tests/Benchmark/CoverageTest.php b/tests/Benchmark/CoverageTest.php new file mode 100644 index 0000000..c546988 --- /dev/null +++ b/tests/Benchmark/CoverageTest.php @@ -0,0 +1,23 @@ +benchmark()->iterations(2)->compare( + fn () => $this->work(), + fn () => $this->work(), + fn () => $this->work(), + fn () => $this->work(), + fn () => $this->work(), + ); + + $this->assertTrue(true); + } +} diff --git a/tests/Benchmark/HardTest.php b/tests/Benchmark/HardTest.php new file mode 100644 index 0000000..05ad201 --- /dev/null +++ b/tests/Benchmark/HardTest.php @@ -0,0 +1,35 @@ +benchmark()->iterations(10)->compare( + fn () => $this->process(), + fn () => $this->process() + ); + + $this->assertTrue(true); + } + + protected function process(): array + { + $result = []; + + for ($i = 0; $i < $this->count; ++$i) { + $result[] = $this->lorem; + } + + return $result; + } +}