-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from TheDragonCode/2.x
Fixed getting the amount of consumed memory
- Loading branch information
Showing
7 changed files
with
118 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' : ''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |