Skip to content

Commit

Permalink
added memory limit functions
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Dec 26, 2023
1 parent 8a97fc3 commit 4ff8d3b
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 59 deletions.
30 changes: 0 additions & 30 deletions src/PhPease/Conf/functions.php

This file was deleted.

36 changes: 36 additions & 0 deletions src/PhPease/Php/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ function get_file_upload_max_size_human_readable(): string
return bytes_to_human_readable(get_file_upload_max_size());
}

/**
* helper function to work with file sizes in ini file format
* @param $size
* @return float
*/
function parse_ini_filesize($size): float
{
// Remove the non-unit characters from the size.
Expand All @@ -58,4 +63,35 @@ function parse_ini_filesize($size): float
} else {
return round($size);
}
}

/**
* get memory limit in kilobytes
* @return false|float|int|string
*/
function get_memory_limit() {
$memory_limit = ini_get('memory_limit');
if (preg_match('/^(\d+)(.)$/', $memory_limit, $matches)) {
if ($matches[2] == 'M') {
$memory_limit = $matches[1] * 1024; // nnnM -> nnn MB
} else if ($matches[2] == 'K') {
$memory_limit = $matches[1]; // nnnK -> nnn KB
} else if ($matches[2] == 'G') {
$memory_limit = $matches[1] * 1024 * 1024; // nnnG -> nnn GB
}
}
return $memory_limit;
}

/**
* get remaining memory in kilobytes
* @return float|int
*/
function get_remaining_memory()
{
$current_memory_usage = memory_get_usage(); // in bytes
$remaining_memory = get_memory_limit() * 1024 - $current_memory_usage; // in bytes

// convert remaining memory from bytes to kilobytes
return $remaining_memory / 1024;
}
1 change: 0 additions & 1 deletion src/PhPease/functions_include.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/

// for manual functions include
require_once __DIR__ . '/Conf/functions.php';
require_once __DIR__ . '/Convert/functions.php';
require_once __DIR__ . '/Request/functions.php';
require_once __DIR__ . '/String/functions.php';
Expand Down
27 changes: 0 additions & 27 deletions tests/ConfTest.php

This file was deleted.

24 changes: 23 additions & 1 deletion tests/PhpFunctionsTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use function PhPease\Convert\kilobytes_to_human_readable;
use function PhPease\Php\get_remaining_memory;
use function PhPease\Php\get_memory_limit;
use function PhPease\Php\parse_ini_filesize;

final class PhpFunctionsTest extends TestCase
Expand All @@ -12,4 +15,23 @@ public function testParseIniFilesize()
$this->assertEquals(2147483648, parse_ini_filesize('2G'));
$this->assertEquals(400, parse_ini_filesize('400'));
}
}

public function test_get_memory_limit_128M()
{
ini_set('memory_limit', '128M');

$memLimit = get_memory_limit();

$this->assertEquals(131072, $memLimit);
$this->assertEquals('128.00 MB', kilobytes_to_human_readable($memLimit));
}

public function test_get_memory_limit_64M()
{
ini_set('memory_limit', '64M');

$memLimit = get_memory_limit();

$this->assertEquals(65536, $memLimit);
$this->assertEquals('64.00 MB', kilobytes_to_human_readable($memLimit));
}}

0 comments on commit 4ff8d3b

Please sign in to comment.