Skip to content

Commit

Permalink
Merge pull request #1 from myaaghubi/v1.1
Browse files Browse the repository at this point in the history
V1.1
  • Loading branch information
myaaghubi authored Apr 2, 2024
2 parents 9915d75 + 014a90b commit 2c7ec6f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 51 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@ 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");
```
For production mode just put a flag:
```php
$debench = new DEBENCH\Debench(false);
// or
$debench->setEnable(false);
```

## License
Expand Down
92 changes: 44 additions & 48 deletions lib/Debench/Debench.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@

class Debench
{
private array $hype;

private array $checkPoints;
private int $ramUsageMax;
private string $path;

private int $initPointMS;
private int $endPointMS;
Expand All @@ -29,32 +27,31 @@ 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;
}

$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();

register_shutdown_function(function () {
// to calculate some stuff
if (!$this->enable) {
return;
}
$this->calculateExecutionTime();
print $this->makeOutput();
});
Expand All @@ -70,9 +67,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
Expand All @@ -99,8 +96,12 @@ public function checkUI(): void
*/
public function newPoint(string $tag = ''): object
{
if (!$this->enable) {
return;
}

$currentTime = $this->getCurrentTime();
$ramUsage = $this->getRamUsagePeak();
$ramUsage = $this->getRamUsage();

if (empty($tag)) {
$tag = 'point ' . ($this->lastCheckPointNumber + 1);
Expand All @@ -123,8 +124,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;

Expand Down Expand Up @@ -164,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;
}


Expand Down Expand Up @@ -205,22 +216,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
Expand All @@ -231,7 +245,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;
}
Expand Down Expand Up @@ -273,25 +287,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
*
Expand Down Expand Up @@ -327,19 +322,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(),
Expand Down
19 changes: 19 additions & 0 deletions lib/Debench/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
}
}
5 changes: 2 additions & 3 deletions lib/Debench/ui/widget.htm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<ul>
<li>
<b>{{@fullExecTime}} ms</b> (<span title="Preload time: {{@preloadTime}} ms">{{@preloadTime}} ms</span>),
<b>{{@ramUsageMax}}</b>
</li>
<li class="debench-toggle" data-target="panel-toggle">
<b>&#8651</b>
Expand All @@ -22,8 +21,8 @@
<div class="panel main-panel">
<ul>
<li data-target="panel-points">
Time: <b>{{@fullExecTime}} ms</b> (<span title="Preload time: {{@preloadTime}} ms">{{@preloadTime}} ms</span>),
Memory: <b>{{@ramUsageMax}}</b>,
Time: <b>{{@fullExecTime}} ms</b> (<span title="Preload Time: {{@preloadTime}} ms">{{@preloadTime}} ms</span>),
Memory: <b>{{@ramUsage}}</b> (<span title="Ram Usage Peak: {{@ramUsagePeak}} ms">{{@ramUsagePeak}}</span>),
Included: <b>{{@includedFilesCount}}</b> files
</li>
<li class="debench-toggle" data-target="panel-main">
Expand Down

0 comments on commit 2c7ec6f

Please sign in to comment.