diff --git a/composer.json b/composer.json index 8f0ca51..83702dc 100644 --- a/composer.json +++ b/composer.json @@ -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" + } + } } \ No newline at end of file diff --git a/lib/Debench/Debench.php b/lib/Debench/Debench.php index 7e73fd1..25c4acf 100644 --- a/lib/Debench/Debench.php +++ b/lib/Debench/Debench.php @@ -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(); }); @@ -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; @@ -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 * @@ -229,7 +255,7 @@ private function getLastCheckPointNumber(): int * * @return array */ - private function getCheckPoints(): array + public function getCheckPoints(): array { if (!$this->checkPoints) { return []; @@ -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()), @@ -380,8 +414,8 @@ private function makeOutput(): string } // ------- logSession - if (session_status() != PHP_SESSION_ACTIVE) { - session_start(); + if (PHP_SAPI === 'cli') { + $_SESSION = array(); } $logSession = ''; @@ -407,7 +441,8 @@ private function makeOutput(): string 'requestLog' => $logRequest, 'session' => count($_SESSION ?? []), 'sessionLog' => $logSession, - 'logTime' => $logTime, + 'infoLog' => $infoLog, + 'timeLog' => $timeLog, 'fullExecTime' => $eTime ]); } @@ -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); diff --git a/lib/Debench/SystemInfo.php b/lib/Debench/SystemInfo.php new file mode 100644 index 0000000..6f25010 --- /dev/null +++ b/lib/Debench/SystemInfo.php @@ -0,0 +1,48 @@ + + * @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; + } +} \ No newline at end of file diff --git a/lib/Debench/Utils.php b/lib/Debench/Utils.php index bb7019b..9437359 100644 --- a/lib/Debench/Utils.php +++ b/lib/Debench/Utils.php @@ -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)) { @@ -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; + } } \ No newline at end of file diff --git a/lib/Debench/ui/assets/css/debench.css b/lib/Debench/ui/assets/css/debench.css index e21b648..c80df2c 100644 --- a/lib/Debench/ui/assets/css/debench.css +++ b/lib/Debench/ui/assets/css/debench.css @@ -1,10 +1,16 @@ 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; @@ -12,20 +18,16 @@ debench ul.debench-main-block { } 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; @@ -33,14 +35,18 @@ debench .debench-panel { 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; } diff --git a/lib/Debench/ui/assets/img/info.png b/lib/Debench/ui/assets/img/info.png new file mode 100644 index 0000000..8bd2e3c Binary files /dev/null and b/lib/Debench/ui/assets/img/info.png differ diff --git a/lib/Debench/ui/widget.htm b/lib/Debench/ui/widget.htm index 0bfabd0..7e17fb1 100644 --- a/lib/Debench/ui/widget.htm +++ b/lib/Debench/ui/widget.htm @@ -2,9 +2,18 @@
+