Add Class for Memory Usage tracking

This commit is contained in:
Clemens Schwaighofer
2022-06-02 16:26:55 +09:00
parent 3085b52714
commit d64d5f081c
5 changed files with 341 additions and 0 deletions

View File

@@ -0,0 +1,119 @@
<?php
declare(strict_types=1);
namespace tests;
use PHPUnit\Framework\TestCase;
use CoreLibs\Debug\MemoryUsage;
/**
* Test class for Debug\MemoryUsage
* @coversDefaultClass \CoreLibs\Debug\MemoryUsage
* @testdox \CoreLibs\Debug\MemoryUsage method tests
*/
final class CoreLibsDebugMemoryUsageTest extends TestCase
{
/**
* Undocumented function
*
* @return array
*/
public function memoryUsageProvider(): array
{
$regex_raw_off = '/^\[[\w\s_-]+\] Peak\/Curr\/Change: \d+(\.\d+)? ?\w{1,2}\/'
. '\d+(\.\d+)? ?\w{1,2}\/'
. 'Since Start: \d+(\.\d+)? ?\w{1,2} \| '
. 'Since Last: \d+(\.\d+)? ?\w{1,2} \| '
. 'Since Set: \d+(\.\d+)? ?\w{1,2}$/';
$regex_raw_on = '/^\[[\w\s_-]+\] Peak\/Curr\/'
// . 'Change: \d+(\.\d+)? ?\w{1,2}\/\d+(\.\d+)? ?\w{1,2} \[\d+\]\/'
. 'Change: \d+(\.\d+)? ?\w{1,2}\/\d+(\.\d+)? ?\w{1,2}/'
. 'Since Start: \d+(\.\d+)? ?\w{1,2} \[\d+\] \| '
. 'Since Last: \d+(\.\d+)? ?\w{1,2} \[\d+\] \| '
. 'Since Set: \d+(\.\d+)? ?\w{1,2} \[\d+\]$/';
$regex_array = [
'prefix' => '/^[\w\s_-]+$/',
'peak' => '/^\d+$/',
'usage' => '/^\d+$/',
'start' => '/^\d+$/',
'last' => '/^\d+$/',
'set' => '/^\d+$/',
];
// 0: prefix
// 1: raw flag
// 2: set flags array
// 3: array output expected (as regex)
// 4: string output expected (as regex)
return [
'test normal' => [
'test',
null,
[],
$regex_array,
$regex_raw_off,
]
];
}
/**
* Undocumented function
*
* @cover ::resetMemory
* @cover ::debugMemoryFlag
* @cover ::setStartMemory
* @cover ::setMemory
* @cover ::memoryUsage
* @cover ::printMemoryUsage
* @dataProvider memoryUsageProvider
* @testdox memoryUsage with $prefix, raw memory $raw [$_dataName]
*
* @param string $prefix
* @param bool|null $raw
* @param array $set_flags
* @param array $expected_array
* @param string $expected_string
* @return void
*/
public function testMemoryUsage(
string $prefix,
?bool $raw,
array $settings,
array $expected_array,
string $expected_string
): void {
// always reeset to null
MemoryUsage::resetMemory();
MemoryUsage::debugMemoryFlag(true);
MemoryUsage::setStartMemory();
MemoryUsage::setMemory();
// run collector
$memory = MemoryUsage::memoryUsage($prefix);
if ($raw === null) {
$string = MemoryUsage::printMemoryUsage($memory);
} else {
$string = MemoryUsage::printMemoryUsage($memory, $raw);
}
// expected_array for each
foreach ($expected_array as $name => $regex) {
$this->assertMatchesRegularExpression(
$regex,
(string)$memory[$name],
'assert memory usage array ' . $name
);
}
// regex match string
$this->assertMatchesRegularExpression(
$expected_string,
$string,
'assert memory usage string as regex'
);
// TODO additional tests with use more memory and check diff matching
// TODO reset memory usage test
}
}
// __END__

View File

@@ -0,0 +1,91 @@
<?php // phpcs:ignore warning
/**
* @phan-file-suppress PhanTypeSuspiciousStringExpression
*/
declare(strict_types=1);
$DEBUG_ALL_OVERRIDE = 0; // set to 1 to debug on live/remote server locations
$DEBUG_ALL = 1;
$PRINT_ALL = 1;
$DB_DEBUG = 1;
if ($DEBUG_ALL) {
error_reporting(E_ALL | E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
}
ob_start();
// basic class test file
define('USE_DATABASE', false);
// sample config
require 'config.php';
// define log file id
$LOG_FILE_ID = 'classTest-memoryusage';
ob_end_flush();
use CoreLibs\Debug\MemoryUsage;
use CoreLibs\Debug\Support;
$log = new CoreLibs\Debug\Logging([
'log_folder' => BASE . LOG,
'file_id' => $LOG_FILE_ID,
// add file date
'print_file_date' => true,
// set debug and print flags
'debug_all' => $DEBUG_ALL ?? false,
'echo_all' => $ECHO_ALL ?? false,
'print_all' => $PRINT_ALL ?? false,
]);
$PAGE_NAME = 'TEST CLASS: MEMORY USAGE';
print "<!DOCTYPE html>";
print "<html><head><title>" . $PAGE_NAME . "</title><head>";
print "<body>";
print '<div><a href="class_test.php">Class Test Master</a></div>';
print '<div><h1>' . $PAGE_NAME . '</h1></div>';
MemoryUsage::debugMemoryFlag(true);
print "Debug Flag: " . Support::printBool(MemoryUsage::debugMemoryFlag()) . "<br>";
MemoryUsage::setStartMemory();
MemoryUsage::setMemory();
$data = MemoryUsage::memoryUsage('first run');
print "Memory usage 1 array: " . Support::printAr($data) . "<br>";
print "Memory usage 1 string: " . MemoryUsage::printMemoryUsage($data) . "<br>";
print "Memory usage 1 string raw: " . MemoryUsage::printMemoryUsage($data, true) . "<br>";
$var = 'foo';
$out = '';
for ($i = 1; $i <= 100; $i++) {
$out .= $var;
}
$data = MemoryUsage::memoryUsage('second run');
print "Memory usage 2 array: " . Support::printAr($data) . "<br>";
print "Memory usage 2 string: " . MemoryUsage::printMemoryUsage($data) . "<br>";
print "Memory usage 2 string raw: " . MemoryUsage::printMemoryUsage($data, true) . "<br>";
MemoryUsage::setMemory();
$var = 'foasdfasdfasdfasdfasdfo';
$out = '';
for ($i = 1; $i <= 10000; $i++) {
$out .= $var;
}
$data = MemoryUsage::memoryUsage('third run');
print "Memory usage 3 array: " . Support::printAr($data) . "<br>";
print "Memory usage 3 string: " . MemoryUsage::printMemoryUsage($data) . "<br>";
print "Memory usage 3 string raw: " . MemoryUsage::printMemoryUsage($data, true) . "<br>";
$var = 'foasdfasdfasdasdfasdfasdfadfadfasdfasdfo';
$out = '';
for ($i = 1; $i <= 100000; $i++) {
$out .= $var;
}
$data = MemoryUsage::memoryUsage('forth run');
print "Memory usage 4 array: " . Support::printAr($data) . "<br>";
print "Memory usage 4 string: " . MemoryUsage::printMemoryUsage($data) . "<br>";
print "Memory usage 4 string raw: " . MemoryUsage::printMemoryUsage($data, true) . "<br>";
// error message
print $log->printErrorMsg();
print "</body></html>";
// __END__

View File

@@ -0,0 +1,129 @@
<?php
/*
* dump memory usage
*/
declare(strict_types=1);
namespace CoreLibs\Debug;
use CoreLibs\Convert\Byte;
class MemoryUsage
{
/** @var int */
private static $start_memory = 0;
/** @var int */
private static $set_memory = 0;
/** @var int */
private static $previous_memory = 0;
/** @var bool */
private static $debug_memory = false;
/**
* set memory flag, or return set memory flag
*
* @param bool|null $set_debug
* @return bool
*/
public static function debugMemoryFlag(?bool $set_debug = null): bool
{
if ($set_debug === null) {
return self::$debug_memory;
}
self::$debug_memory = $set_debug;
return self::$debug_memory;
}
/**
* Reset all memory variables to 0
*
* @return void
*/
public static function resetMemory(): void
{
self::$start_memory = 0;
self::$set_memory = 0;
self::$previous_memory = 0;
}
/**
* set the start memory velue, or reset to a new start value
*
* @return void
*/
public static function setStartMemory(): void
{
self::$start_memory = memory_get_usage();
}
/**
* set the and independent memory set for a sub tracking outside main
*
* @return void
*/
public static function setMemory(): void
{
self::$set_memory = memory_get_usage();
}
/**
* calculate and set memory usage values
* this will return an array with all the data that can be used in
* printMemoryUsage for human readable output
*
* @param string $prefix A prefix tag
* @return array<string,int|string> return array
*/
public static function memoryUsage(string $prefix): array
{
// skip if DEBUG is off
if (self::$debug_memory === false) {
return [];
}
if (empty(self::$start_memory)) {
self::$start_memory = memory_get_usage();
}
$memory_usage = memory_get_usage();
$data = [
'prefix' => $prefix,
'peak' => memory_get_peak_usage(),
'usage' => $memory_usage,
'start' => $memory_usage - self::$start_memory,
'last' => $memory_usage - self::$previous_memory,
'set' => $memory_usage - self::$set_memory
];
self::$previous_memory = $memory_usage;
return $data;
}
/**
* returns a human readable output from the memoryUsage function
* can be used for logging purpose
*
* @param array<string,int|string> $data Data array from memoryUsage
* @param bool $raw Flag to shaw unconverted memory numbers
* @return string Return debug string with memory usage
*/
public static function printMemoryUsage(array $data, bool $raw = false): string
{
return
'[' . $data['prefix'] . '] Peak/Curr/Change: '
. Byte::humanReadableByteFormat($data['peak'])
. '/'
. Byte::humanReadableByteFormat($data['usage'])
// . ($raw === true ? ' [' . $data['usage'] . ']' : '')
. '/Since Start: '
. Byte::humanReadableByteFormat($data['start'])
. ($raw === true ? ' [' . $data['start'] . ']' : '')
. ' | Since Last: '
. Byte::humanReadableByteFormat($data['last'])
. ($raw === true ? ' [' . $data['last'] . ']' : '')
. ' | Since Set: '
. Byte::humanReadableByteFormat($data['set'])
. ($raw === true ? ' [' . $data['set'] . ']' : '');
}
}
// __END__

View File

@@ -37,6 +37,7 @@ return array(
'CoreLibs\\DB\\SQL\\SqlInterface\\SqlFunctions' => $baseDir . '/lib/CoreLibs/DB/SQL/SqlInterface/SqlFunctions.php',
'CoreLibs\\Debug\\FileWriter' => $baseDir . '/lib/CoreLibs/Debug/FileWriter.php',
'CoreLibs\\Debug\\Logging' => $baseDir . '/lib/CoreLibs/Debug/Logging.php',
'CoreLibs\\Debug\\MemoryUsage' => $baseDir . '/lib/CoreLibs/Debug/MemoryUsage.php',
'CoreLibs\\Debug\\RunningTime' => $baseDir . '/lib/CoreLibs/Debug/RunningTime.php',
'CoreLibs\\Debug\\Support' => $baseDir . '/lib/CoreLibs/Debug/Support.php',
'CoreLibs\\Get\\ReadEnvFile' => $baseDir . '/lib/CoreLibs/Get/ReadEnvFile.php',

View File

@@ -102,6 +102,7 @@ class ComposerStaticInit10fe8fe2ec4017b8644d2b64bcf398b9
'CoreLibs\\DB\\SQL\\SqlInterface\\SqlFunctions' => __DIR__ . '/../..' . '/lib/CoreLibs/DB/SQL/SqlInterface/SqlFunctions.php',
'CoreLibs\\Debug\\FileWriter' => __DIR__ . '/../..' . '/lib/CoreLibs/Debug/FileWriter.php',
'CoreLibs\\Debug\\Logging' => __DIR__ . '/../..' . '/lib/CoreLibs/Debug/Logging.php',
'CoreLibs\\Debug\\MemoryUsage' => __DIR__ . '/../..' . '/lib/CoreLibs/Debug/MemoryUsage.php',
'CoreLibs\\Debug\\RunningTime' => __DIR__ . '/../..' . '/lib/CoreLibs/Debug/RunningTime.php',
'CoreLibs\\Debug\\Support' => __DIR__ . '/../..' . '/lib/CoreLibs/Debug/Support.php',
'CoreLibs\\Get\\ReadEnvFile' => __DIR__ . '/../..' . '/lib/CoreLibs/Get/ReadEnvFile.php',