Skip to content

Commit

Permalink
Merge pull request #5 from myaaghubi/v1.4
Browse files Browse the repository at this point in the history
V1.4
  • Loading branch information
myaaghubi authored Apr 22, 2024
2 parents 7f569f0 + e76895c commit 242e1e0
Show file tree
Hide file tree
Showing 13 changed files with 349 additions and 34 deletions.
37 changes: 28 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
{
"name": "myaaghubi/debench",
"name": "myaaghubi/debench",
"description": "A small debug/benchmark helper for PHP",
"keywords": ["benchmark", "debug", "tool", "debench"],
"homepage": "https://github.com/myaaghubi/Debench",
"license": "MIT",
"authors": [
{
"name": "Mohammad Yaaghubi"
}
],
"homepage": "https://github.com/myaaghubi/Debench",
"support": {
"issues": "https://github.com/myaaghubi/debench/issues",
"source": "https://github.com/myaaghubi/debench"
},
"keywords": [
"benchmark",
"debug",
"tool",
"debench"
],
"autoload": {
"classmap": ["lib/"]
"classmap": [
"lib/"
]
},
"require-dev": {
"phpunit/phpunit": "^9.0"
},
"config": {
"platform": {
"php": "8.0"
}
}
"config": {
"platform": {
"php": "8.0"
}
}
}
57 changes: 46 additions & 11 deletions lib/Debench/Debench.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,20 @@ public function __construct(private bool $enable = true, private string $ui = 't
// check for UI
$this->checkUI();


if (PHP_SAPI !== 'cli' && session_status() != PHP_SESSION_ACTIVE) {
@session_start();
}

register_shutdown_function(function () {
if (!$this->enable) {
return;
}

if (Utils::isInTestMode()) {
return;
}

$this->calculateExecutionTime();
print $this->makeOutput();
});
Expand Down Expand Up @@ -123,17 +133,22 @@ public function newPoint(string $tag = ''): void
// to avoid duplicate tags(keys)
$tag .= '#' . ($this->lastCheckPointNumber + 1);

$dbc = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$dbc = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$dbcIndex = 0;

// specify calls from self class
if (strrpos(($dbc[$dbcIndex])['file'], __FILE__) !== false) {
$dbcIndex = 1;
while (count($dbc) > $dbcIndex && strrpos(($dbc[$dbcIndex])['file'], __FILE__) !== false) {
$dbcIndex += 1;
}

$file = ($dbc[$dbcIndex])['file'];
$line = ($dbc[$dbcIndex])['line'];

if (strrpos($file, __FILE__) !== false) {
$file = '-';
$line = '-';
}

$checkPoint = new CheckPoint($currentTime, $ramUsage, $file, $line);
$this->checkPoints[$tag] = $checkPoint;

Expand Down Expand Up @@ -167,6 +182,17 @@ private function calculateExecutionTime(): void
}


/**
* Is Debench in minimal mode
*
* @return bool
*/
public function isMinimal(): bool
{
return $this->minimal;
}


/**
* Set Debench to only minimal mode
*
Expand Down Expand Up @@ -229,7 +255,7 @@ private function getLastCheckPointNumber(): int
*
* @return array<string,object>
*/
private function getCheckPoints(): array
public function getCheckPoints(): array
{
if (!$this->checkPoints) {
return [];
Expand Down Expand Up @@ -353,12 +379,20 @@ private function makeOutput(): string
]);
}

// ------- logTime
$logTime = '';
// ------- infoLog
$infoLog = Template::render($this->path . '/' . $this->ui . '/debench/widget.log.info.htm', [
"phpVersion" => SystemInfo::getPHPVersion(),
"opcache" => SystemInfo::getOPCacheStatus() ? 'On' : 'Off',
"systemAPI" => SystemInfo::getSystemAPI(),
]);

// ------- timeLog
$timeLog = '';
foreach ($this->checkPoints as $key => $cp) {
$logTime .= Template::render($this->path . '/' . $this->ui . '/debench/widget.log.checkpoint.htm', [
$timeLog .= Template::render($this->path . '/' . $this->ui . '/debench/widget.log.checkpoint.htm', [
"name" => $this->getTagName($key),
"path" => $cp->getPath(),
"fileName" => basename($cp->getPath()),
"lineNumber" => $cp->getLineNumber(),
"timestamp" => $cp->getTimestamp(),
"memory" => Utils::toFormattedBytes($cp->getMemory()),
Expand All @@ -380,8 +414,8 @@ private function makeOutput(): string
}

// ------- logSession
if (session_status() != PHP_SESSION_ACTIVE) {
session_start();
if (PHP_SAPI === 'cli') {
$_SESSION = array();
}

$logSession = '';
Expand All @@ -407,7 +441,8 @@ private function makeOutput(): string
'requestLog' => $logRequest,
'session' => count($_SESSION ?? []),
'sessionLog' => $logSession,
'logTime' => $logTime,
'infoLog' => $infoLog,
'timeLog' => $timeLog,
'fullExecTime' => $eTime
]);
}
Expand Down Expand Up @@ -435,7 +470,7 @@ public static function point(string $tag = ''): void
public static function getInstance($enable = true, string $ui = 'theme'): Debench
{
if (self::$instance === null) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
$path = dirname(($backtrace[0])['file']);

self::$instance = new self($enable, $ui, $path);
Expand Down
48 changes: 48 additions & 0 deletions lib/Debench/SystemInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/**
* @package Debench, SystemInfo
* @link http://github.com/myaaghubi/debench Github
* @author Mohammad Yaaghubi <[email protected]>
* @copyright Copyright (c) 2024, Mohammad Yaaghubi
* @license MIT License
*/

namespace DEBENCH;

class SystemInfo
{
/**
* Get the php version
*
* @return string
*/
public static function getPHPVersion(): string
{
return PHP_VERSION;
}


/**
* Get the system api
*
* @return string
*/
public static function getSystemAPI(): string
{
return php_sapi_name();
}


/**
* Get OPCache status
*
* @return string
*/
public static function getOPCacheStatus(): bool
{
return function_exists('opcache_get_status') && is_array(opcache_get_status()) && opcache_get_status()['opcache_enabled'] == true;
}
}
25 changes: 24 additions & 1 deletion lib/Debench/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Utils
* @param string $to
* @return void
*/
public static function copyDir(string $from, string $to, bool $checkForError=true): void
public static function copyDir(string $from, string $to, bool $checkForError = true): void
{
if ($checkForError) {
if (!file_exists($from)) {
Expand Down Expand Up @@ -71,4 +71,27 @@ public static function toFormattedBytes(int $size = 0): string

return round(pow(1024, $base - floor($base))) . ' ' . $suffixes[floor($base)];
}


/**
* Check if PHPUnit test is running
*
* @return bool
*/
public static function isInTestMode(): bool
{
if (PHP_SAPI != 'cli') {
return false;
}

if (defined('PHPUNIT_COMPOSER_INSTALL') && defined('__PHPUNIT_PHAR__')) {
return true;
}

if (strpos($_SERVER['argv'][0], 'phpunit') !== false) {
return true;
}

return false;
}
}
22 changes: 14 additions & 8 deletions lib/Debench/ui/assets/css/debench.css
Original file line number Diff line number Diff line change
@@ -1,46 +1,52 @@
debench {
color: #555!important;
margin:0 0 -1px 0;
position: fixed!important;
z-index: 1000000!important;
bottom: 0px!important;
left: 0px!important;
}
debench * {
font-family: tahoma!important;
direction: ltr!important;
text-align: left!important;
font-size: 13px!important;
}
debench ul.debench-main-block {
margin: 0px!important;
padding: 0!important;
list-style-type: none!important;
}
debench ul.debench-main-block > li:not(:last-child) {
padding:0px 0 0 0;
margin:1px 0 -1px 0;
margin:0 0 -1px 0;
line-height: 0%!important;
}
debench ul.debench-main-block > li:not(:last-child) {
display: none;
}
debench .debench-panel {
font-family: tahoma!important;
direction: ltr!important;
text-align: left!important;
border: 1px solid #dee2e6!important;
border-width: 1px 1px 0 0!important;
border-radius: 0 5px 0 0!important;
font-size: 13px!important;
background: #f8f9fa!important;
color: #555!important;
padding: 5px 7px;
line-height: 150%!important;
display: inline-block;
overflow: hidden;
}
debench .debench-panel td {
color: #555!important;
line-height: 150%!important;
}
debench .debench-panel span {
font-style:italic!important;
text-decoration:none!important;
}
debench .debench-panel span[title] {
debench .debench-panel span[title], debench .debench-panel td[title] {
text-decoration: underline!important;
}
debench .debench-panel-title span {
debench .debench-panel-title span, debench .debench-panel-title td {
font-style:italic!important;
text-decoration: underline!important;
}
Expand Down
Binary file added lib/Debench/ui/assets/img/info.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion lib/Debench/ui/widget.htm
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,18 @@
<link rel="stylesheet" type="text/css" href="{{@base}}/debench/assets/css/debench.css">

<ul class="debench-main-block">
<li class="debench-info-block">
<div class="debench-panel">
<table>
{{@infoLog}}
</table>
</div>
</li>
<li class="debench-checkpoints-block">
<div class="debench-panel">
{{@logTime}}
<table>
{{@timeLog}}
</table>
</div>
</li>
<li class="debench-request-block">
Expand All @@ -28,6 +37,11 @@
</ul>
</div>
<div class="debench-panel debench-main-panel">
<ul>
<li data-target="debench-info-block">
<image width="12px" height="12px" src="{{@base}}/debench/assets/img/info.png" style="margin-bottom:-1px;"></image>
</li>
</ul>
<ul>
<li data-target="debench-checkpoints-block">
Time: <b>{{@fullExecTime}} ms</b> (<span title="Preload Time: {{@preloadTime}} ms">{{@preloadTime}}
Expand Down
13 changes: 9 additions & 4 deletions lib/Debench/ui/widget.log.checkpoint.htm
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<div>
<span title='{{@path}}:{{@lineNumber}}'>{{@name}}</span>
=> Time: <b>{{@timestamp}} ms</b> ({{@percent}}%), Memory: <b>{{@memory}}</b>
</div>
<tr>
<td title="{{@path}}:{{@lineNumber}}">{{@name}}</td>
<td>&nbsp;=>&nbsp;</td>
<td>Time: <b>{{@timestamp}} ms</b> ({{@percent}}%)</td>
<td>&nbsp;|&nbsp;</td>
<td>Memory: <b>{{@memory}}</b></td>
<td>&nbsp;|&nbsp;</td>
<td>{{@fileName}}:{{@lineNumber}}</td>
</tr>
16 changes: 16 additions & 0 deletions lib/Debench/ui/widget.log.info.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<tr>
<td title="PHP Version">PHP</td>
<td>&nbsp;=>&nbsp;</td>
<td><b>{{@phpVersion}}</b></td>
</tr>
<tr>
<td title="OPCache Status">OPCache</td>
<td>&nbsp;=>&nbsp;</td>
<td><b>{{@opcache}}</b></td>
</tr>
<tr>
<td title="System API">System API</td>
<td>&nbsp;=>&nbsp;</td>
<td><b>{{@systemAPI}}</b></td>
</tr>

Loading

0 comments on commit 242e1e0

Please sign in to comment.