Skip to content

Commit

Permalink
Merge pull request #4 from TheDragonCode/2.x
Browse files Browse the repository at this point in the history
Fixed getting the amount of consumed memory
  • Loading branch information
andrey-helldar authored Feb 15, 2023
2 parents 46fa713 + ad6cfdc commit f351020
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 29 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}

Expand All @@ -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 }} \
Expand Down
30 changes: 11 additions & 19 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticProperties="false"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
colors="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
executionOrder="depends,defects"
colors="true"
failOnRisky="true"
failOnWarning="true"
>
<coverage>
<include>
<directory suffix=".php">./src</directory>
<directory suffix=".php">src</directory>
</include>
<report>
<clover outputFile="build/logs/clover.xml" />
<html outputDirectory="build/logs/coverage" />
<text outputFile="build/logs/coverage.txt" />
</report>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
35 changes: 35 additions & 0 deletions src/Services/Memory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace DragonCode\Benchmark\Services;

class Memory
{
protected array $sizes = [
'GB' => 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' : '');
}
}
9 changes: 7 additions & 2 deletions src/Services/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

class Runner
{
public function __construct(
protected readonly Memory $memory = new Memory()
) {
}

public function call(callable $callback): array
{
$this->clean();
Expand All @@ -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];
}
Expand Down
10 changes: 5 additions & 5 deletions src/Services/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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']));
}
}

Expand All @@ -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);
}
}
23 changes: 23 additions & 0 deletions tests/Benchmark/CoverageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Tests\Benchmark;

use Tests\TestCase;

class CoverageTest extends TestCase
{
public function testDefault(): void
{
$this->benchmark()->iterations(2)->compare(
fn () => $this->work(),
fn () => $this->work(),
fn () => $this->work(),
fn () => $this->work(),
fn () => $this->work(),
);

$this->assertTrue(true);
}
}
35 changes: 35 additions & 0 deletions tests/Benchmark/HardTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Tests\Benchmark;

use Tests\TestCase;

class HardTest extends TestCase
{
protected string $lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras efficitur nisi in scelerisque ultricies.';

protected int $count = 100000;

public function testMemory(): void
{
$this->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;
}
}

0 comments on commit f351020

Please sign in to comment.