Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Commit

Permalink
Split memory string formatting into a separate class (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramchale authored Mar 31, 2022
1 parent f76df3c commit bb7f78f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
composer.lock
vendor
.phpunit.result.cache
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<directory>tests</directory>
</testsuite>
<extensions>
<extension class="MOJ\PHPUnit\MemoryUsageHook">
<extension class="MinistryOfJustice\PHPUnit\MemoryUsageHook">
<arguments>
<integer>500000</integer>
<boolean>true</boolean>
Expand Down
18 changes: 18 additions & 0 deletions src/MemoryStringFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace MinistryOfJustice\PHPUnit;

class MemoryStringFormatter
{
public static function roundToStandardUnits(int $bytes): string
{
$unit = ['B','KB','MB','GB'];

$magnitude = floor(log($bytes, 1024));
$value = round($bytes / pow(1024, $magnitude), 2);

return $value . ' ' . $unit[$magnitude];
}
}
12 changes: 3 additions & 9 deletions src/MemoryUsageHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ public function __construct(
) {
}

static function roundToStandardUnits($size): string
{
$unit=['B','KB','MB','GB'];
return round($size/pow(1024,($i=floor(log($size,1024)))),2) . ' ' . $unit[$i];
}

public function executeBeforeTest(string $test): void
{
$this->previousUsage = memory_get_usage();
Expand All @@ -40,8 +34,8 @@ public function executeAfterTest(string $test, float $time): void
}

if ($this->displayRunningTotal) {
$currentString = self::roundToStandardUnits($currentUsage);
$differenceString = self::roundToStandardUnits($difference);
$currentString = MemoryStringFormatter::roundToStandardUnits($currentUsage);
$differenceString = MemoryStringFormatter::roundToStandardUnits($difference);
print("Test '$test' ended. Current: $currentString, Difference: $differenceString\n");
}
}
Expand All @@ -59,7 +53,7 @@ public function executeAfterLastTest(): void
array_multisort($usageColumn, SORT_DESC, $this->results);

foreach ($this->results as $result) {
$differenceString = self::roundToStandardUnits($result['usage']);
$differenceString = MemoryStringFormatter::roundToStandardUnits($result['usage']);
print("Test '${result['name']}' used: $differenceString\n");
}
}
Expand Down
33 changes: 33 additions & 0 deletions tests/MemoryStringFormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace MinistryOfJustice\PHPUnit\Tests;

use MinistryOfJustice\PHPUnit\MemoryStringFormatter;
use PHPUnit\Framework\TestCase;

class MemoryStringFormatterTest extends TestCase
{
/**
* @dataProvider memoryProvider
*/
public function test_roundToStandardUnits(string $expectedValue, int $bytes): void
{
$this->assertEquals($expectedValue, MemoryStringFormatter::roundToStandardUnits($bytes));
}

public function memoryProvider(): array
{
return [
'1 Byte' => ['1 B', 1],
'Maximum Bytes' => ['1023 B', 1023],
'1 Kilobyte' => ['1 KB', 1024],
'Maximum Kilobytes' => ['1024 KB', 1048575],
'1 Megabyte' => ['1 MB', 1048576],
'Maximum Megabytes' => ['1024 MB', 1073741823],
'1 Gigabyte' => ['1 GB', 1073741824],
'1024 Gigabytes' => ['1024 GB', 1099511627775],
];
}
}

0 comments on commit bb7f78f

Please sign in to comment.