From 99a8bee3e70065c6ff2e420e1e45e4c6ad198dea Mon Sep 17 00:00:00 2001 From: Robin Mulder Date: Tue, 2 Jul 2024 09:13:10 +0200 Subject: [PATCH 1/2] Refactor xhprofile initialization and termination so it works with every app request --- Model/Profiler/XhprofProfiler.php | 2 +- Observer/StartXhprof.php | 37 -------- Observer/StopXhprof.php | 39 -------- Plugin/AppInterfacePlugin.php | 42 +++++++++ README.md | 149 +++++++++++++++++++++++++++++- etc/di.xml | 6 ++ etc/events.xml | 9 -- etc/module.xml | 5 +- 8 files changed, 198 insertions(+), 91 deletions(-) delete mode 100644 Observer/StartXhprof.php delete mode 100644 Observer/StopXhprof.php create mode 100644 Plugin/AppInterfacePlugin.php create mode 100644 etc/di.xml delete mode 100644 etc/events.xml diff --git a/Model/Profiler/XhprofProfiler.php b/Model/Profiler/XhprofProfiler.php index b465c54..db760e6 100644 --- a/Model/Profiler/XhprofProfiler.php +++ b/Model/Profiler/XhprofProfiler.php @@ -2,12 +2,12 @@ namespace JustBetter\XhprofProfiler\Model\Profiler; +use Magento\Framework\App\DeploymentConfig; use Magento\Framework\Exception\FileSystemException; use Magento\Framework\Exception\RuntimeException; use SpiralPackages\Profiler\Driver\XhprofDriver; use SpiralPackages\Profiler\Storage\WebStorage; use Symfony\Component\HttpClient\NativeHttpClient; -use Magento\Framework\App\DeploymentConfig; class XhprofProfiler { diff --git a/Observer/StartXhprof.php b/Observer/StartXhprof.php deleted file mode 100644 index 812ab1e..0000000 --- a/Observer/StartXhprof.php +++ /dev/null @@ -1,37 +0,0 @@ -isEnabled($observer->getRequest())) { - $this->profiler->handle(); - } - } - - public function isEnabled(Http $request): bool - { - if ($request->getHeader(XhprofProfiler::HEADER)) { - return filter_var($request->getHeader(XhprofProfiler::HEADER), FILTER_VALIDATE_BOOLEAN); - } - - try { - return (bool) getenv('XHPROF_ENABLED'); - } catch (Throwable) { - return false; - } - } -} diff --git a/Observer/StopXhprof.php b/Observer/StopXhprof.php deleted file mode 100644 index da0847f..0000000 --- a/Observer/StopXhprof.php +++ /dev/null @@ -1,39 +0,0 @@ -isEnabled($observer->getRequest())) { - $this->profiler->terminate( - ['route' => $observer->getRequest()->getFullActionName()] - ); - } - } - - public function isEnabled(Http $request): bool - { - if ($request->getHeader(XhprofProfiler::HEADER)) { - return filter_var($request->getHeader(XhprofProfiler::HEADER), FILTER_VALIDATE_BOOLEAN); - } - - try { - return (bool) getenv('XHPROF_ENABLED'); - } catch (Throwable) { - return false; - } - } -} diff --git a/Plugin/AppInterfacePlugin.php b/Plugin/AppInterfacePlugin.php new file mode 100644 index 0000000..89af976 --- /dev/null +++ b/Plugin/AppInterfacePlugin.php @@ -0,0 +1,42 @@ +isEnabled()) { + return $proceed(); + } + + $this->profiler->handle(); + $response = $proceed(); + $this->profiler->terminate(); + return $response; + } + + public function isEnabled(): bool + { + if ($this->request->getHeader(XhprofProfiler::HEADER)) { + return filter_var($this->request->getHeader(XhprofProfiler::HEADER), FILTER_VALIDATE_BOOLEAN); + } + + return isset($_ENV['XHPROF_ENABLED']) && is_bool($_ENV['XHPROF_ENABLED']) ? $_ENV['XHPROF_ENABLED'] : false; + } +} diff --git a/README.md b/README.md index d772ae9..8141698 100644 --- a/README.md +++ b/README.md @@ -1 +1,148 @@ -# magento2-xhprofprofiler \ No newline at end of file +# Xhprof Profiler for Magento 2 + +## Overview + +This module integrates Xhprof profiling capabilities into your Magento 2 application. It provides an easy way to profile and analyze your application's performance by tracking and storing profiling data. + +## Requirements + +- Magento 2.4.7 or higher +- Xhprof PHP extension +- Compatible with Buggregator + +## Installation + +1. **Install via composer:** + + ```bash + composer require justbetter/magento2-xhprof-profiler + ``` + +2. **Enable the module:** + + ```bash + bin/magento module:enable JustBetter_XhprofProfiler + ``` + +3. **Run setup upgrade and di compile:** + + ```bash + bin/magento setup:upgrade + bin/magento setup:di:compile + ``` + +## Configuration + +Configure the module by adding the following configuration to your `app/etc/env.php` file: + +```php +return [ + // ... other configurations ... + 'xhprofprofiler' => [ + 'app_name' => 'Magento 247', + 'endpoint' => 'http://exciting_chatelet.orb.local/profiler/store' + ], +]; +``` + +- **app_name**: The name of your application. +- **endpoint**: The endpoint where the profiling data will be stored. + +## Usage + +The profiling is automatically enabled for all requests. The module uses the `AppInterfacePlugin` to start and terminate the profiler around each request. + +### Key Classes and Methods + +- **`XhprofProfiler`** + - **Constants:** + - `HEADER`: The header key used to enable Xhprof profiling. + - `IGNORED_FUNCTIONS_KEY`: The key for ignored functions in the profiler. + - **Methods:** + - `__construct()`: Initializes the profiler with the given driver, deployment configuration, and optional tags. + - `handle()`: Starts the profiler. + - `terminate()`: Ends the profiler and stores the profiling data. + +- **`AppInterfacePlugin`** + - **Methods:** + - `aroundLaunch()`: Wraps around the application launch to start and stop the profiler. + +## Buggregator Compatibility + +This module is compatible with [Buggregator](https://buggregator.dev/), a debug and profiler tool for PHP applications. Buggregator can collect and visualize profiling data generated by this module, providing a comprehensive debugging and performance analysis experience. + +To integrate with Buggregator: + +1. Ensure Buggregator is installed and configured in your environment. +2. Configure the endpoint in `app/etc/env.php` to point to Buggregator's profiling data endpoint. + +Example configuration: + +```php +return [ + // ... other configurations ... + 'xhprofprofiler' => [ + 'app_name' => 'Magento 247', + 'endpoint' => 'http://your_buggregator_instance/profiler/store' + ], +]; +``` + +## Example + +Here is an example of how the profiler is used in the plugin: + +```php +namespace JustBetter\XhprofProfiler\Plugin; + +use JustBetter\XhprofProfiler\Model\Profiler\XhprofProfiler; +use Magento\Framework\App\ResponseInterface; +use Magento\Framework\AppInterface as Application; + +class AppInterfacePlugin +{ + public function __construct( + protected XhprofProfiler $profiler + ) + { + } + + public function aroundLaunch( + Application $subject, + callable $proceed, + ) : ResponseInterface { + $this->profiler->handle(); + $response = $proceed(); + $this->profiler->terminate(); + return $response; + } +} +``` + +## Exception Handling + +The `terminate` method of the `XhprofProfiler` class can throw the following exceptions: +- **FileSystemException**: If there is an issue with the file system. +- **RuntimeException**: If there is a runtime issue. + +Ensure you have proper exception handling in place when integrating this module. + +## License + +This project is licensed under the MIT License - see the LICENSE file for details. + +## Contributing + +1. Fork the repository. +2. Create your feature branch (`git checkout -b feature/fooBar`). +3. Commit your changes (`git commit -am 'Add some fooBar'`). +4. Push to the branch (`git push origin feature/fooBar`). +5. Create a new Pull Request. + +## Contact + +If you have any questions or need further assistance, please contact [robin@justbetter.nl]. + +--- + +By following this README, you should be able to integrate and use the Xhprof Profiler in your Magento 2 application effectively. Happy profiling! diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..3912ef6 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/etc/events.xml b/etc/events.xml deleted file mode 100644 index 398d359..0000000 --- a/etc/events.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - diff --git a/etc/module.xml b/etc/module.xml index 02176c0..8ecc4dd 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,7 +1,4 @@ - - - - + From e20c00e668ed8a55b47505f0132261c4adb5e3af Mon Sep 17 00:00:00 2001 From: Robin Mulder Date: Tue, 2 Jul 2024 09:50:45 +0200 Subject: [PATCH 2/2] Try actions/checkout@v4 --- .github/workflows/analyse.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/analyse.yml b/.github/workflows/analyse.yml index c67aaa4..06d1e40 100644 --- a/.github/workflows/analyse.yml +++ b/.github/workflows/analyse.yml @@ -9,7 +9,7 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2