-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from justbetter/feature/refactor-profiling-star…
…t-and-end Refactor xhprofile initialization and termination so it works with ev…
- Loading branch information
Showing
9 changed files
with
199 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
namespace JustBetter\XhprofProfiler\Plugin; | ||
|
||
use JustBetter\XhprofProfiler\Model\Profiler\XhprofProfiler; | ||
use Magento\Framework\App\Request\Http; | ||
use Magento\Framework\App\ResponseInterface; | ||
use Magento\Framework\AppInterface as Application; | ||
use Throwable; | ||
|
||
class AppInterfacePlugin | ||
{ | ||
public function __construct( | ||
protected XhprofProfiler $profiler, | ||
protected Http $request | ||
) | ||
{ | ||
} | ||
|
||
public function aroundLaunch( | ||
Application $subject, | ||
callable $proceed, | ||
) : ResponseInterface { | ||
|
||
if (!$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,148 @@ | ||
# magento2-xhprofprofiler | ||
# 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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<type name="Magento\Framework\AppInterface"> | ||
<plugin name="JustBetter_XhprofProfiler::profile" type="JustBetter\XhprofProfiler\Plugin\AppInterfacePlugin" sortOrder="1" /> | ||
</type> | ||
</config> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="JustBetter_XhprofProfiler" setup_version="1.0.0"> | ||
<sequence> | ||
</sequence> | ||
</module> | ||
<module name="JustBetter_XhprofProfiler" /> | ||
</config> |