From cef92d05b25cdcedf127561781169380b1e42527 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Mon, 1 Aug 2022 01:24:44 +0200 Subject: [PATCH 1/3] overwrite profiling methods to get a more accurate profiling --- Application/Component/DebugBarComponent.php | 32 +++++------ .../Models/TimeDataCollectorHandler.php | 35 ++++++++++++ .../blocks/layout/footer_main_debugbar.tpl | 5 -- Modules/Core/ShopControl_DebugBar.php | 21 ++++--- Modules/functions.php | 57 +++++++++++++++++++ README.md | 4 ++ metadata.php | 8 +-- 7 files changed, 123 insertions(+), 39 deletions(-) create mode 100644 Application/Models/TimeDataCollectorHandler.php delete mode 100644 Application/views/blocks/layout/footer_main_debugbar.tpl create mode 100644 Modules/functions.php diff --git a/Application/Component/DebugBarComponent.php b/Application/Component/DebugBarComponent.php index c0e4530..63886b9 100644 --- a/Application/Component/DebugBarComponent.php +++ b/Application/Component/DebugBarComponent.php @@ -16,9 +16,9 @@ declare(strict_types=1); namespace D3\DebugBar\Application\Component; use D3\DebugBar\Application\Models\Collectors\SmartyCollector; +use D3\DebugBar\Application\Models\TimeDataCollectorHandler; use DebugBar\Bridge\DoctrineCollector; use DebugBar\Bridge\MonologCollector; -use DebugBar\DataCollector\TimeDataCollector; use DebugBar\DebugBarException; use DebugBar\JavascriptRenderer; use DebugBar\StandardDebugBar; @@ -26,7 +26,6 @@ use Doctrine\DBAL\Logging\DebugStack; use OxidEsales\Eshop\Core\Controller\BaseController; use OxidEsales\Eshop\Core\DatabaseProvider; use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; -use OxidEsales\Eshop\Core\Exception\StandardException; use OxidEsales\Eshop\Core\Registry; use ReflectionClass; use ReflectionException; @@ -34,9 +33,9 @@ use ReflectionException; class DebugBarComponent extends BaseController { /** @var StandardDebugBar */ - protected $debugBar; + public $debugBar; /** @var JavascriptRenderer */ - protected $debugBarRenderer; + public $debugBarRenderer; /** * Marking object as component @@ -137,22 +136,17 @@ class DebugBarComponent extends BaseController $debugbar->addCollector($this->getSmartyCollector()); } - public function addTimelineMessures() + /** + * @return void + */ + public function addTimelineMessures(): void { - /** @var TimeDataCollector $tCollector */ - $tCollector = $this->debugBar['time']; + $collectors = $this->debugBar->getCollectors(); + $collectors['time'] = TimeDataCollectorHandler::getInstance(); - global $aStartTimes; - global $aProfileTimes; - global $executionCounts; - foreach ($aProfileTimes as $label => $recordedTime) { - for ($i = 0; $i < $executionCounts[$label]; $i++) { - $tCollector->addMeasure( - $label, - $aStartTimes[$label], - $aStartTimes[$label] + $aProfileTimes[$label] / $executionCounts[$label] - ); - } - } + $reflection = new ReflectionClass($this->debugBar); + $property = $reflection->getProperty('collectors'); + $property->setAccessible(true); + $property->setValue($this->debugBar, $collectors); } } \ No newline at end of file diff --git a/Application/Models/TimeDataCollectorHandler.php b/Application/Models/TimeDataCollectorHandler.php new file mode 100644 index 0000000..995bb23 --- /dev/null +++ b/Application/Models/TimeDataCollectorHandler.php @@ -0,0 +1,35 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\DebugBar\Application\Models; + +use DebugBar\DataCollector\TimeDataCollector; + +class TimeDataCollectorHandler +{ + /** @var TimeDataCollector */ + private static $instance = null; + + /** + * @return TimeDataCollector + */ + public static function getInstance(): TimeDataCollector + { + if (self::$instance === null) { + self::$instance = new TimeDataCollector(); + } + return self::$instance; + } +} \ No newline at end of file diff --git a/Application/views/blocks/layout/footer_main_debugbar.tpl b/Application/views/blocks/layout/footer_main_debugbar.tpl deleted file mode 100644 index b883a91..0000000 --- a/Application/views/blocks/layout/footer_main_debugbar.tpl +++ /dev/null @@ -1,5 +0,0 @@ -[{$smarty.block.parent}] - -[{$debugBarRenderer->renderHead()}] -[{$debugBarComponent->addTimelineMessures()}] -[{$debugBarRenderer->render()}] \ No newline at end of file diff --git a/Modules/Core/ShopControl_DebugBar.php b/Modules/Core/ShopControl_DebugBar.php index a698243..f9a3e60 100644 --- a/Modules/Core/ShopControl_DebugBar.php +++ b/Modules/Core/ShopControl_DebugBar.php @@ -20,17 +20,11 @@ use OxidEsales\Eshop\Core\Registry; class ShopControl_DebugBar extends ShopControl_DebugBar_parent { - /** - * @param null $controllerKey - * @param null $function - * @param null $parameters - * @param null $viewsChain - */ - public function start ($controllerKey = null, $function = null, $parameters = null, $viewsChain = null) + public function __construct() { $this->_d3AddDebugBarComponent(); - parent::start( $controllerKey, $function, $parameters, $viewsChain); + parent::__construct(); } /** @@ -51,4 +45,15 @@ class ShopControl_DebugBar extends ShopControl_DebugBar_parent Registry::getConfig()->setConfigParam('aUserComponentNames', $userComponentNames); } } + + public function __destruct() + { + if (!isAdmin()) { + /** @var DebugBarComponent $debugBarComponent */ + $debugBarComponent = Registry::getConfig()->getActiveView()->getComponent(DebugBarComponent::class); + echo $debugBarComponent->debugBarRenderer->renderHead(); + $debugBarComponent->addTimelineMessures(); + echo $debugBarComponent->debugBarRenderer->render(); + } + } } \ No newline at end of file diff --git a/Modules/functions.php b/Modules/functions.php new file mode 100644 index 0000000..d6df5d8 --- /dev/null +++ b/Modules/functions.php @@ -0,0 +1,57 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +function startProfile($sProfileName) +{ + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $trace[0] = $sProfileName; + $hash = md5(serialize($trace)).'-'.$sProfileName; + + $timeDataCollector = \D3\DebugBar\Application\Models\TimeDataCollectorHandler::getInstance(); + $timeDataCollector->startMeasure($hash, $sProfileName); + + global $aStartTimes; + global $executionCounts; + if (!isset($executionCounts[$sProfileName])) { + $executionCounts[$sProfileName] = 0; + } + if (!isset($aStartTimes[$sProfileName])) { + $aStartTimes[$sProfileName] = 0; + } + $executionCounts[$sProfileName]++; + $aStartTimes[$sProfileName] = microtime(true); +} + +function stopProfile($sProfileName) +{ + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $trace[0] = $sProfileName; + $hash = md5(serialize($trace)).'-'.$sProfileName; + + $timeDataCollector = \D3\DebugBar\Application\Models\TimeDataCollectorHandler::getInstance(); + $timeDataCollector->stopMeasure($hash); + + + global $aStartTimes; + global $executionCounts; + if (!isset($executionCounts[$sProfileName])) { + $executionCounts[$sProfileName] = 0; + } + if (!isset($aStartTimes[$sProfileName])) { + $aStartTimes[$sProfileName] = 0; + } + $executionCounts[$sProfileName]++; + $aStartTimes[$sProfileName] = microtime(true); +} \ No newline at end of file diff --git a/README.md b/README.md index f7321af..c001259 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,10 @@ php composer require d3/modulename:^2.0 Aktivieren Sie das Modul im Shopadmin unter "Erweiterungen -> Module". +``` +include './modules/d3/debugbar/Modules/functions.php'; +``` + ## Changelog Siehe [CHANGELOG](CHANGELOG.md) für weitere Informationen. diff --git a/metadata.php b/metadata.php index c8b7546..6ea2b16 100644 --- a/metadata.php +++ b/metadata.php @@ -42,11 +42,5 @@ $aModule = [ 'events' => [], 'templates' => [], 'settings' => [], - 'blocks' => [ - [ - 'template' => 'layout/footer.tpl', - 'block' => 'footer_main', - 'file' => 'Application/views/blocks/layout/footer_main_debugbar.tpl' - ] - ], + 'blocks' => [], ]; From be7cab1481aca8845531915603fa465db062de26 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Mon, 1 Aug 2022 01:24:44 +0200 Subject: [PATCH 2/3] overwrite profiling methods to get a more accurate profiling --- Application/Component/DebugBarComponent.php | 46 +++++++-------- .../Models/TimeDataCollectorHandler.php | 35 ++++++++++++ .../blocks/layout/footer_main_debugbar.tpl | 5 -- Modules/Core/ShopControl_DebugBar.php | 21 ++++--- Modules/functions.php | 57 +++++++++++++++++++ README.md | 4 ++ metadata.php | 8 +-- 7 files changed, 129 insertions(+), 47 deletions(-) create mode 100644 Application/Models/TimeDataCollectorHandler.php delete mode 100644 Application/views/blocks/layout/footer_main_debugbar.tpl create mode 100644 Modules/functions.php diff --git a/Application/Component/DebugBarComponent.php b/Application/Component/DebugBarComponent.php index c0e4530..b003ab4 100644 --- a/Application/Component/DebugBarComponent.php +++ b/Application/Component/DebugBarComponent.php @@ -16,9 +16,9 @@ declare(strict_types=1); namespace D3\DebugBar\Application\Component; use D3\DebugBar\Application\Models\Collectors\SmartyCollector; +use D3\DebugBar\Application\Models\TimeDataCollectorHandler; use DebugBar\Bridge\DoctrineCollector; use DebugBar\Bridge\MonologCollector; -use DebugBar\DataCollector\TimeDataCollector; use DebugBar\DebugBarException; use DebugBar\JavascriptRenderer; use DebugBar\StandardDebugBar; @@ -26,7 +26,6 @@ use Doctrine\DBAL\Logging\DebugStack; use OxidEsales\Eshop\Core\Controller\BaseController; use OxidEsales\Eshop\Core\DatabaseProvider; use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; -use OxidEsales\Eshop\Core\Exception\StandardException; use OxidEsales\Eshop\Core\Registry; use ReflectionClass; use ReflectionException; @@ -99,16 +98,6 @@ class DebugBarComponent extends BaseController return new SmartyCollector(Registry::getUtilsView()->getSmarty()); } - /** - * @return string|null - */ - public function render() - { - $this->getParent()->addTplParam('debugBarRenderer', $this->debugBarRenderer); - $this->getParent()->addTplParam('debugBarComponent', $this); - return parent::render(); - } - /** * @param $object * @param $propName @@ -137,22 +126,25 @@ class DebugBarComponent extends BaseController $debugbar->addCollector($this->getSmartyCollector()); } - public function addTimelineMessures() + /** + * @return void + */ + public function addTimelineMessures(): void { - /** @var TimeDataCollector $tCollector */ - $tCollector = $this->debugBar['time']; + $collectors = $this->debugBar->getCollectors(); + $collectors['time'] = TimeDataCollectorHandler::getInstance(); - global $aStartTimes; - global $aProfileTimes; - global $executionCounts; - foreach ($aProfileTimes as $label => $recordedTime) { - for ($i = 0; $i < $executionCounts[$label]; $i++) { - $tCollector->addMeasure( - $label, - $aStartTimes[$label], - $aStartTimes[$label] + $aProfileTimes[$label] / $executionCounts[$label] - ); - } - } + $reflection = new ReflectionClass($this->debugBar); + $property = $reflection->getProperty('collectors'); + $property->setAccessible(true); + $property->setValue($this->debugBar, $collectors); + } + + /** + * @return JavascriptRenderer + */ + public function getRenderer(): JavascriptRenderer + { + return $this->debugBarRenderer; } } \ No newline at end of file diff --git a/Application/Models/TimeDataCollectorHandler.php b/Application/Models/TimeDataCollectorHandler.php new file mode 100644 index 0000000..995bb23 --- /dev/null +++ b/Application/Models/TimeDataCollectorHandler.php @@ -0,0 +1,35 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\DebugBar\Application\Models; + +use DebugBar\DataCollector\TimeDataCollector; + +class TimeDataCollectorHandler +{ + /** @var TimeDataCollector */ + private static $instance = null; + + /** + * @return TimeDataCollector + */ + public static function getInstance(): TimeDataCollector + { + if (self::$instance === null) { + self::$instance = new TimeDataCollector(); + } + return self::$instance; + } +} \ No newline at end of file diff --git a/Application/views/blocks/layout/footer_main_debugbar.tpl b/Application/views/blocks/layout/footer_main_debugbar.tpl deleted file mode 100644 index b883a91..0000000 --- a/Application/views/blocks/layout/footer_main_debugbar.tpl +++ /dev/null @@ -1,5 +0,0 @@ -[{$smarty.block.parent}] - -[{$debugBarRenderer->renderHead()}] -[{$debugBarComponent->addTimelineMessures()}] -[{$debugBarRenderer->render()}] \ No newline at end of file diff --git a/Modules/Core/ShopControl_DebugBar.php b/Modules/Core/ShopControl_DebugBar.php index a698243..e1be43e 100644 --- a/Modules/Core/ShopControl_DebugBar.php +++ b/Modules/Core/ShopControl_DebugBar.php @@ -20,17 +20,11 @@ use OxidEsales\Eshop\Core\Registry; class ShopControl_DebugBar extends ShopControl_DebugBar_parent { - /** - * @param null $controllerKey - * @param null $function - * @param null $parameters - * @param null $viewsChain - */ - public function start ($controllerKey = null, $function = null, $parameters = null, $viewsChain = null) + public function __construct() { $this->_d3AddDebugBarComponent(); - parent::start( $controllerKey, $function, $parameters, $viewsChain); + parent::__construct(); } /** @@ -51,4 +45,15 @@ class ShopControl_DebugBar extends ShopControl_DebugBar_parent Registry::getConfig()->setConfigParam('aUserComponentNames', $userComponentNames); } } + + public function __destruct() + { + if (!isAdmin()) { + /** @var DebugBarComponent $debugBarComponent */ + $debugBarComponent = Registry::getConfig()->getActiveView()->getComponent(DebugBarComponent::class); + echo $debugBarComponent->getRenderer()->renderHead(); + $debugBarComponent->addTimelineMessures(); + echo $debugBarComponent->getRenderer()->render(); + } + } } \ No newline at end of file diff --git a/Modules/functions.php b/Modules/functions.php new file mode 100644 index 0000000..d6df5d8 --- /dev/null +++ b/Modules/functions.php @@ -0,0 +1,57 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +function startProfile($sProfileName) +{ + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $trace[0] = $sProfileName; + $hash = md5(serialize($trace)).'-'.$sProfileName; + + $timeDataCollector = \D3\DebugBar\Application\Models\TimeDataCollectorHandler::getInstance(); + $timeDataCollector->startMeasure($hash, $sProfileName); + + global $aStartTimes; + global $executionCounts; + if (!isset($executionCounts[$sProfileName])) { + $executionCounts[$sProfileName] = 0; + } + if (!isset($aStartTimes[$sProfileName])) { + $aStartTimes[$sProfileName] = 0; + } + $executionCounts[$sProfileName]++; + $aStartTimes[$sProfileName] = microtime(true); +} + +function stopProfile($sProfileName) +{ + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $trace[0] = $sProfileName; + $hash = md5(serialize($trace)).'-'.$sProfileName; + + $timeDataCollector = \D3\DebugBar\Application\Models\TimeDataCollectorHandler::getInstance(); + $timeDataCollector->stopMeasure($hash); + + + global $aStartTimes; + global $executionCounts; + if (!isset($executionCounts[$sProfileName])) { + $executionCounts[$sProfileName] = 0; + } + if (!isset($aStartTimes[$sProfileName])) { + $aStartTimes[$sProfileName] = 0; + } + $executionCounts[$sProfileName]++; + $aStartTimes[$sProfileName] = microtime(true); +} \ No newline at end of file diff --git a/README.md b/README.md index f7321af..c001259 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,10 @@ php composer require d3/modulename:^2.0 Aktivieren Sie das Modul im Shopadmin unter "Erweiterungen -> Module". +``` +include './modules/d3/debugbar/Modules/functions.php'; +``` + ## Changelog Siehe [CHANGELOG](CHANGELOG.md) für weitere Informationen. diff --git a/metadata.php b/metadata.php index c8b7546..6ea2b16 100644 --- a/metadata.php +++ b/metadata.php @@ -42,11 +42,5 @@ $aModule = [ 'events' => [], 'templates' => [], 'settings' => [], - 'blocks' => [ - [ - 'template' => 'layout/footer.tpl', - 'block' => 'footer_main', - 'file' => 'Application/views/blocks/layout/footer_main_debugbar.tpl' - ] - ], + 'blocks' => [], ]; From ba46258bbc82a0edae2c5fd677e64fe89660afeb Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Mon, 1 Aug 2022 22:44:05 +0200 Subject: [PATCH 3/3] add patch instructions to create overridable functions by automatic --- Application/Component/DebugBarComponent.php | 4 ++-- README.en.md | 8 +++++++- README.md | 6 ++++++ composer.json | 3 ++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Application/Component/DebugBarComponent.php b/Application/Component/DebugBarComponent.php index 733b5e3..8e24095 100644 --- a/Application/Component/DebugBarComponent.php +++ b/Application/Component/DebugBarComponent.php @@ -33,9 +33,9 @@ use ReflectionException; class DebugBarComponent extends BaseController { /** @var StandardDebugBar */ - public $debugBar; + protected $debugBar; /** @var JavascriptRenderer */ - public $debugBarRenderer; + protected $debugBarRenderer; /** * Marking object as component diff --git a/README.en.md b/README.en.md index 9316ba3..c850c11 100644 --- a/README.en.md +++ b/README.en.md @@ -23,7 +23,13 @@ Please enter the following section in the `composer.json` of your project: "extra": { "ajgl-symlinks": { "maximebf/debugbar": { - "src/DebugBar/Resources": { "source/out/debugbar". + "src/DebugBar/Resources": "source/out/debugbar" + } + }, + "enable-patching": "true", + "patches": { + "oxid-esales/oxideshop-ce": { + "Add overridable functions for advanced profiling in Debug Bar": "https://git.d3data.de/D3Public/DebugBar/raw/branch/patches/overridablefunctions.patch" } } } diff --git a/README.md b/README.md index c001259..e29fd2c 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,12 @@ Bitte tragen Sie den folgenden Abschnitt in die `composer.json` Ihres Projektes "maximebf/debugbar": { "src/DebugBar/Resources": "source/out/debugbar" } + }, + "enable-patching": "true", + "patches": { + "oxid-esales/oxideshop-ce": { + "Add overridable functions for advanced profiling in Debug Bar": "https://git.d3data.de/D3Public/DebugBar/raw/branch/patches/overridablefunctions.patch" + } } } ``` diff --git a/composer.json b/composer.json index 7fd42e8..a7e0d6c 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,8 @@ "php": ">=7.3", "oxid-esales/oxideshop-ce": "6.8 - 6.10", "maximebf/debugbar": "^1.18", - "ajgl/composer-symlinker": "^0.3.1" + "ajgl/composer-symlinker": "^0.3.1", + "cweagans/composer-patches": "^1.7.2" }, "require-dev": { "php": "^7.4",