From 366ad168be77405e671d688da9bf8ba026a2ea9a Mon Sep 17 00:00:00 2001 From: Mohammad Yaaghubi Date: Tue, 2 Apr 2024 10:44:09 +0330 Subject: [PATCH 1/5] Add `toFormattedBytes(): string` --- lib/Debench/Utils.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/Debench/Utils.php b/lib/Debench/Utils.php index 4e4015e..bb7019b 100644 --- a/lib/Debench/Utils.php +++ b/lib/Debench/Utils.php @@ -52,4 +52,23 @@ public static function copyDir(string $from, string $to, bool $checkForError=tru closedir($dir); } + + + /** + * Format bytes with B, KB, MB, 'GB', 'TB' etc. + * + * @param int $size + * @return string + */ + public static function toFormattedBytes(int $size = 0): string + { + if ($size == 0) { + return '0 B'; + } + + $base = log($size, 1024); + $suffixes = array('B', 'KB', 'MB', 'GB', 'TB'); + + return round(pow(1024, $base - floor($base))) . ' ' . $suffixes[floor($base)]; + } } \ No newline at end of file From fd4cb2fcae947079c894cf16c28e3d60dc0253e0 Mon Sep 17 00:00:00 2001 From: Mohammad Yaaghubi Date: Tue, 2 Apr 2024 10:52:59 +0330 Subject: [PATCH 2/5] Memory fixed & some changes --- lib/Debench/Debench.php | 66 ++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 44 deletions(-) diff --git a/lib/Debench/Debench.php b/lib/Debench/Debench.php index 03e8082..159a296 100644 --- a/lib/Debench/Debench.php +++ b/lib/Debench/Debench.php @@ -14,10 +14,8 @@ class Debench { - private array $hype; - private array $checkPoints; - private int $ramUsageMax; + private string $path; private int $initPointMS; private int $endPointMS; @@ -35,20 +33,17 @@ public function __construct(private bool $active = true, private string $ui = 't return; } - $this->hype = []; - $this->checkPoints = []; - $this->ramUsageMax = 0; $this->lastCheckPointInMS = 0; $this->lastCheckPointNumber = 0; - $initCP = $this->newPoint('debench init'); + $initCP = $this->newPoint('debench'); $this->initPointMS = $initCP->getTimestamp(); - $this->hype['ui'] = rtrim($ui, '/'); + $this->ui = rtrim($ui, '/'); $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2); - $this->hype['base'] = dirname(($backtrace[0])['file']); + $this->path = dirname(($backtrace[0])['file']); // check for UI $this->checkUI(); @@ -70,9 +65,9 @@ public function __construct(private bool $active = true, private string $ui = 't public function checkUI(): void { $currentPath = __DIR__; - $basePath = $this->hype['base']; + $basePath = $this->path; - $uiPath = $basePath . '/' . $this->hype['ui']; + $uiPath = $basePath . '/' . $this->ui; $uiPathFull = $uiPath . '/debench'; // for assets @@ -100,7 +95,7 @@ public function checkUI(): void public function newPoint(string $tag = ''): object { $currentTime = $this->getCurrentTime(); - $ramUsage = $this->getRamUsagePeak(); + $ramUsage = $this->getRamUsage(); if (empty($tag)) { $tag = 'point ' . ($this->lastCheckPointNumber + 1); @@ -123,8 +118,6 @@ public function newPoint(string $tag = ''): object $checkPoint = new CheckPoint($currentTime, $ramUsage, $file, $line); $this->checkPoints[$tag] = $checkPoint; - $this->ramUsageMax = max($ramUsage, $this->ramUsageMax); - $this->lastCheckPointInMS = $currentTime; $this->lastCheckPointNumber += 1; @@ -205,22 +198,25 @@ private function getCheckPoints(): array /** - * Get the max value of ram usage happened till now + * Get the ram usage * * @param bool $formatted * @return int|string */ - public function getRamUsageMax(bool $formatted=false): int|string + public function getRamUsage(bool $formatted=false): int|string { + // true => memory_real_usage + $peak = memory_get_usage(); + if ($formatted) - return $this->getFormattedBytes($this->ramUsageMax); + return Utils::toFormattedBytes($peak); - return $this->ramUsageMax; + return $peak; } /** - * Get the real ram usage + * Get the real ram usage (peak) * * @param bool $formatted * @return int|string @@ -231,7 +227,7 @@ public function getRamUsagePeak(bool $formatted=false): int|string $peak = memory_get_peak_usage(true); if ($formatted) - return $this->getFormattedBytes($peak); + return Utils::toFormattedBytes($peak); return $peak; } @@ -273,25 +269,6 @@ public function getCurrentTime(): int } - /** - * format bytes with KB, MB, etc. - * - * @param int $size - * @return string - */ - private function getFormattedBytes(int $size = 0): string - { - if ($size == 0) { - return '0 B'; - } - - $base = log($size, 1024); - $suffixes = array('B', 'KB', 'MB', 'GB', 'TB'); - - return round(pow(1024, $base - floor($base))) . ' ' . $suffixes[floor($base)]; - } - - /** * Get the count of all loaded files * @@ -327,19 +304,20 @@ private function makeOutput(): string $log = ''; foreach ($this->checkPoints as $key => $cp) { - $log .= Template::render($this->hype['base'] . '/' . $this->hype['ui'] . '/debench/widget.log.htm', [ + $log .= Template::render($this->path . '/' . $this->ui . '/debench/widget.log.htm', [ "name" => $this->getTagName($key), "path" => $cp->getPath(), "lineNumber" => $cp->getLineNumber(), "timestamp" => $cp->getTimestamp(), - "memory" => $this->getFormattedBytes($cp->getMemory()), + "memory" => Utils::toFormattedBytes($cp->getMemory()), "percent" => round($cp->getTimestamp() / ($eTime>1?$eTime:1) * 100), ]); } - return Template::render($this->hype['base'] . '/' . $this->hype['ui'] . '/debench/widget.htm', [ - 'base' => $this->hype['ui'], - 'ramUsageMax' => $this->getRamUsageMax(true), + return Template::render($this->path . '/' . $this->ui . '/debench/widget.htm', [ + 'base' => $this->ui, + 'ramUsagePeak' => $this->getRamUsagePeak(true), + 'ramUsage' => $this->getRamUsage(true), 'includedFilesCount' => $this->getLoadedFilesCount(), 'checkPoints' => $this->getLastCheckPointNumber(), 'preloadTime' => $this->initPointMS - $this->getRequestTime(), From 4e109caf4ee84f77ad8a643a1c6f06cd10d49702 Mon Sep 17 00:00:00 2001 From: Mohammad Yaaghubi Date: Tue, 2 Apr 2024 10:53:21 +0330 Subject: [PATCH 3/5] Update widget.htm --- lib/Debench/ui/widget.htm | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Debench/ui/widget.htm b/lib/Debench/ui/widget.htm index 16d2c51..8e12914 100644 --- a/lib/Debench/ui/widget.htm +++ b/lib/Debench/ui/widget.htm @@ -12,7 +12,6 @@
  • {{@fullExecTime}} ms ({{@preloadTime}} ms), - {{@ramUsageMax}}
  • @@ -22,8 +21,8 @@
    • - Time: {{@fullExecTime}} ms ({{@preloadTime}} ms), - Memory: {{@ramUsageMax}}, + Time: {{@fullExecTime}} ms ({{@preloadTime}} ms), + Memory: {{@ramUsage}} ({{@ramUsagePeak}}), Included: {{@includedFilesCount}} files
    • From d9ddc2020e7b1d44447616f1a14202f9fbff2f02 Mon Sep 17 00:00:00 2001 From: Mohammad Yaaghubi Date: Tue, 2 Apr 2024 11:00:38 +0330 Subject: [PATCH 4/5] Fix a bug for disabled mode --- lib/Debench/Debench.php | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/Debench/Debench.php b/lib/Debench/Debench.php index 159a296..0140f64 100644 --- a/lib/Debench/Debench.php +++ b/lib/Debench/Debench.php @@ -27,9 +27,9 @@ class Debench * * @return void */ - public function __construct(private bool $active = true, private string $ui = 'theme') + public function __construct(private bool $enable = true, private string $ui = 'theme') { - if (!$this->active) { + if (!$this->enable) { return; } @@ -49,7 +49,9 @@ public function __construct(private bool $active = true, private string $ui = 't $this->checkUI(); register_shutdown_function(function () { - // to calculate some stuff + if (!$this->enable) { + return; + } $this->calculateExecutionTime(); print $this->makeOutput(); }); @@ -94,6 +96,10 @@ public function checkUI(): void */ public function newPoint(string $tag = ''): object { + if (!$this->enable) { + return; + } + $currentTime = $this->getCurrentTime(); $ramUsage = $this->getRamUsage(); @@ -157,7 +163,19 @@ private function calculateExecutionTime(): void */ public function isEnable(): bool { - return $this->active; + return $this->enable; + } + + + /** + * Set Debench enable + * + * @param bool $enable + * @return void + */ + public function setEnable(bool $enable): void + { + $this->enable = $enable; } From 014a90b13f359d83b47e1127206a9c82ef257ad8 Mon Sep 17 00:00:00 2001 From: Mohammad Yaaghubi Date: Tue, 2 Apr 2024 11:04:23 +0330 Subject: [PATCH 5/5] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 44e2608..3d5b734 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,13 @@ require __DIR__ . '/vendor/autoload.php'; $debench = new DEBENCH\Debench(); sleep(1); +$st = str_repeat("Debench!", 1000); // after a seconds $debench->newPoint("step one"); sleep(2); +$st .= str_repeat("Debench!", 1000); // after two more second $debench->newPoint("step two"); @@ -31,6 +33,8 @@ $debench->newPoint("step two"); For production mode just put a flag: ```php $debench = new DEBENCH\Debench(false); +// or +$debench->setEnable(false); ``` ## License