Skip to content

Commit f351020

Browse files
Merge pull request #4 from TheDragonCode/2.x
Fixed getting the amount of consumed memory
2 parents 46fa713 + ad6cfdc commit f351020

File tree

7 files changed

+118
-29
lines changed

7 files changed

+118
-29
lines changed

.github/workflows/phpunit.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ jobs:
1111
matrix:
1212
php: [ "8.1", "8.2" ]
1313
symfony: [ "5.3", "6.0" ]
14-
prefer: [ "stable", "lowest" ]
1514

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

@@ -24,11 +23,11 @@ jobs:
2423
with:
2524
php-version: ${{ matrix.php }}
2625
extensions: curl, mbstring, zip, pcntl, pdo, pdo_sqlite, iconv
27-
coverage: xdebug
26+
coverage: none
2827

2928
- name: Install dependencies
3029
run: |
31-
composer require --no-interaction --prefer-${{ matrix.prefer }} \
30+
composer require --no-interaction \
3231
symfony/console:^${{ matrix.symfony }} \
3332
symfony/var-dumper:^${{ matrix.symfony }} \
3433

phpunit.xml

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit
3-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
4-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5-
backupGlobals="false"
6-
backupStaticProperties="false"
7-
bootstrap="vendor/autoload.php"
8-
cacheDirectory=".phpunit.cache"
9-
colors="true"
10-
processIsolation="false"
11-
stopOnError="false"
12-
stopOnFailure="false"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheDirectory=".phpunit.cache"
6+
executionOrder="depends,defects"
7+
colors="true"
8+
failOnRisky="true"
9+
failOnWarning="true"
1310
>
1411
<coverage>
1512
<include>
16-
<directory suffix=".php">./src</directory>
13+
<directory suffix=".php">src</directory>
1714
</include>
18-
<report>
19-
<clover outputFile="build/logs/clover.xml" />
20-
<html outputDirectory="build/logs/coverage" />
21-
<text outputFile="build/logs/coverage.txt" />
22-
</report>
2315
</coverage>
2416
<testsuites>
25-
<testsuite name="Test Suite">
26-
<directory suffix="Test.php">./tests</directory>
17+
<testsuite name="default">
18+
<directory>tests</directory>
2719
</testsuite>
2820
</testsuites>
2921
</phpunit>

src/Services/Memory.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Benchmark\Services;
6+
7+
class Memory
8+
{
9+
protected array $sizes = [
10+
'GB' => 1024 * 1024 * 1024,
11+
'MB' => 1024 * 1024,
12+
'KB' => 1024,
13+
];
14+
15+
public function now(): int
16+
{
17+
return memory_get_usage(true);
18+
}
19+
20+
public function diff(int $memory): int
21+
{
22+
return memory_get_peak_usage(true) - $memory;
23+
}
24+
25+
public function format(int $bytes): string
26+
{
27+
foreach ($this->sizes as $unit => $value) {
28+
if ($bytes >= $value) {
29+
return sprintf('%.2f %s', $bytes >= 1024 ? $bytes / $value : $bytes, $unit);
30+
}
31+
}
32+
33+
return $bytes . ' byte' . ($bytes !== 1 ? 's' : '');
34+
}
35+
}

src/Services/Runner.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
class Runner
88
{
9+
public function __construct(
10+
protected readonly Memory $memory = new Memory()
11+
) {
12+
}
13+
914
public function call(callable $callback): array
1015
{
1116
$this->clean();
@@ -20,13 +25,13 @@ protected function clean(): void
2025

2126
protected function run(callable $callback): array
2227
{
23-
$ramFrom = memory_get_usage();
28+
$ramFrom = $this->memory->now();
2429
$startAt = hrtime(true);
2530

2631
$callback();
2732

2833
$time = $this->diff(hrtime(true), $startAt);
29-
$ram = memory_get_peak_usage() - $ramFrom;
34+
$ram = $this->memory->diff($ramFrom);
3035

3136
return [$time, $ram];
3237
}

src/Services/View.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use DragonCode\Benchmark\View\ProgressBar;
88
use DragonCode\Benchmark\View\Table;
9-
use DragonCode\Support\Facades\Helpers\Digit;
109
use Symfony\Component\Console\Style\SymfonyStyle;
1110

1211
class View
@@ -18,7 +17,8 @@ class View
1817
protected ?int $roundPrecision = null;
1918

2019
public function __construct(
21-
protected SymfonyStyle $io
20+
protected SymfonyStyle $io,
21+
protected Memory $memory = new Memory()
2222
) {
2323
$this->table = new Table($this->io);
2424
$this->progressBar = new ProgressBar($this->io);
@@ -54,7 +54,7 @@ protected function appendMs(array $data): array
5454
continue;
5555
}
5656

57-
$value = sprintf('%s ms - %sb', $this->roundTime($value['time']), $this->roundRam($value['ram']));
57+
$value = sprintf('%s ms - %s', $this->roundTime($value['time']), $this->roundRam($value['ram']));
5858
}
5959
}
6060

@@ -70,8 +70,8 @@ protected function roundTime(float $value): float
7070
return $value;
7171
}
7272

73-
protected function roundRam(float $value): string
73+
protected function roundRam(int|float $bytes): string
7474
{
75-
return Digit::toShort($value);
75+
return $this->memory->format((int) $bytes);
7676
}
7777
}

tests/Benchmark/CoverageTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Benchmark;
6+
7+
use Tests\TestCase;
8+
9+
class CoverageTest extends TestCase
10+
{
11+
public function testDefault(): void
12+
{
13+
$this->benchmark()->iterations(2)->compare(
14+
fn () => $this->work(),
15+
fn () => $this->work(),
16+
fn () => $this->work(),
17+
fn () => $this->work(),
18+
fn () => $this->work(),
19+
);
20+
21+
$this->assertTrue(true);
22+
}
23+
}

tests/Benchmark/HardTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Benchmark;
6+
7+
use Tests\TestCase;
8+
9+
class HardTest extends TestCase
10+
{
11+
protected string $lorem = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras efficitur nisi in scelerisque ultricies.';
12+
13+
protected int $count = 100000;
14+
15+
public function testMemory(): void
16+
{
17+
$this->benchmark()->iterations(10)->compare(
18+
fn () => $this->process(),
19+
fn () => $this->process()
20+
);
21+
22+
$this->assertTrue(true);
23+
}
24+
25+
protected function process(): array
26+
{
27+
$result = [];
28+
29+
for ($i = 0; $i < $this->count; ++$i) {
30+
$result[] = $this->lorem;
31+
}
32+
33+
return $result;
34+
}
35+
}

0 commit comments

Comments
 (0)