# justbetter/magento2-xhprof-profiler

> Xhprof profiler integration for Magento

`composer require justbetter/magento2-xhprof-profiler`

Canonical URL: https://packagento.com/justbetter/magento2-xhprof-profiler

## At a glance

- **Vendor**: justbetter (https://packagento.com/justbetter.md)
- **Latest version**: 0.2.0 — released 2024-11-13
- **Pricing**: Free
- **Package type**: Magento 2 module
- **Status**: active, accepting new buyers

## Installation

Packagento is licence-gated, so even free packages need a licence on a project before Composer can resolve them.

1. **Sign in or create an account** at https://packagento.com/customer/account/.

2. **Add the package to your account.** Open https://packagento.com/justbetter/magento2-xhprof-profiler and complete the free checkout. A licence is minted automatically.

3. **Create or pick a project, then activate the licence on it.**
   - Projects represent the Magento installs you deploy to. Manage them at https://packagento.com/projects/.
   - Activate the new licence on the project you'll deploy this package to. Activation is what generates the Composer credentials scoped to that project.

4. **Add the project credentials to your Magento codebase.**

   Grab the project's public + private key from https://packagento.com/projects/ (open the project, then its Credentials tab), and add them to `auth.json`:

   ```json
   {
     "http-basic": {
       "packagento.com": {
         "username": "ppk_live_...",
         "password": "psk_live_..."
       }
     }
   }
   ```

   Add the Packagento Composer repository to `composer.json`:

   ```json
   {
     "repositories": [
       { "type": "composer", "url": "https://packagento.com" }
     ]
   }
   ```

5. **Install and apply.**

   ```bash
   composer require justbetter/magento2-xhprof-profiler:*
   bin/magento setup:upgrade
   bin/magento setup:di:compile
   bin/magento cache:flush
   ```

## What it does

Xhprof profiler integration for Magento

## README

### 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 XHGui
- 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 for Buggregator

By default this module uses XHGui for processing the profiling data. We can use the Buggregator driver by overriding the default driver via xml:
```
<type name="JustBetter\XhprofProfiler\Model\Profiler\XhprofProfiler">
    <arguments>
        <argument name="driver" xsi:type="object">JustBetter\XhprofProfiler\Model\Profiler\Driver\Buggregator</argument>
    </arguments>
</type>
```

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.

### Compatibility

This module is compatible with [XHGui](https://github.com/perftools/xhgui) and [Buggregator](https://buggregator.dev/). These are graphical interfaces for viewing XHProf profiling data.

#### To integrate with XHGui:

1. Follow the installation guide of XHGui at [XHGui](https://github.com/perftools/xhgui)
2. By default we use the default xhprof profiler and the results are getting uploaded to XHGui.
3. Update default configuration via di.xml, check [config.default.php](https://github.com/perftools/xhgui/blob/0.23.x/config/config.default.php) for the possible options. 

##### Default XHGui configuration 
You can pass custom configuration to the XHGui driver via the arguments like the default configuration.
```
<type name="JustBetter\XhprofProfiler\Model\Profiler\Driver\XHGui">
    <arguments>
        <argument name="config" xsi:type="array">
            <item name="profiler" xsi:type="string">xhprof</item>
            <item name="save.handler" xsi:type="string">upload</item>
            <item name="save.handler.upload" xsi:type="array">
                <item name="url" xsi:type="string">http://xhgui.xhgui.orb.local/run/import</item>
                <item name="timeout" xsi:type="number">3</item>
                <item name="token" xsi:type="string">token</item>
            </item>
        </argument>
    </arguments>
</type>

```

#### 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'
    ],
];
```

### 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`**
    - **Methods:**
        - `__construct()`: Initializes the profiler with the given driver 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.
      
### 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!

## Recent Versions

| Version | Released |
|---|---|
| 0.2.0 | 2024-11-13 |
| 0.1.0 | 2024-10-23 |

## Dependencies

### Require

| Package | Constraint |
|---|---|
| magento/framework | * |
| magento/module-config | ^101.2 |
| perftools/php-profiler | ^1.1 |
| php | >=8.2 |
| spiral-packages/profiler | ^1.2 |

### Require (dev)

| Package | Constraint |
|---|---|
| bitexpert/phpstan-magento | ^0.30.1 |
| phpstan/phpstan | ^1.10 |

## Quality

Latest release (0.2.0) fails the Packagento QA pipeline. Verdicts below are per-cell (Magento line × PHP version) for the matrixed tools, and run-once for the static / security tiers.


### Compatibility

Each Magento line is installed on its supported PHP versions, then the module is built (DI compile + static-content deploy). Cells show passed / failed / untested; staircase gaps render as `–`.

| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | Pass | Pass | – | – |
| 2.4.8 | – | Pass | Pass | – |
| 2.4.9 | – | – | Pass | Pass |


### Code Quality

Advisory checks against the module's source. Never affect the Compatibility verdict — a phpcs finding can't make a module incompatible.

#### Static Analysis

Coding standards (phpcs), mess detection (phpmd), copy-pasted code (cpd), PHP cross-version compatibility, composer.json validity. Each runs once for the whole module.

| Tool | Status | Findings | Summary |
|---|---|---|---|
| PHPCS | Fail | 10 | 2 errors, 8 warnings (ruleset: Magento2) — 7 auto-fixable with phpcbf |
| PHPMD | Warning | 2 | 2 rule violations (MissingImport:1, UnusedFormalParameter:1) |
| Cpd | Pass | 0 |  |
| Composer validate | Info | 1 | valid; 1 advisory note (composer validate --strict) |

#### PHPStan

Type-checks the module against a real Magento install. Re-runs per Magento + PHP version because resolvable symbols differ between releases.

| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | 1 | 1 | – | – |
| 2.4.8 | – | 1 | 1 | – |
| 2.4.9 | – | – | 1 | 1 |


### Tests

Unit and integration suites run per Magento + PHP cell. Test failures speak to the module's behaviour, not its compatibility with a line, so they're reported here separately.

#### Unit Tests

| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | N/A | N/A | – | – |
| 2.4.8 | – | N/A | N/A | – |
| 2.4.9 | – | – | N/A | N/A |

#### Integration Tests

| Magento | PHP 8.2 | PHP 8.3 | PHP 8.4 | PHP 8.5 |
|---|---|---|---|---|
| 2.4.7 | N/A | N/A | – | – |
| 2.4.8 | – | N/A | N/A | – |
| 2.4.9 | – | – | N/A | N/A |


### Security

Dependency-advisory audit (composer audit) plus a source malware scan. A malware detection fails the version outright.

| Tool | Status | Findings | Summary |
|---|---|---|---|
| Composer audit | N/A | 0 | no resolvable dependency tree to audit — Your requirements could not be resolved to an installable set of packages. Problem 1 |
| Malware scan | Pass | 0 |  |

## Licence and pricing

Free. A licence is still minted on checkout and bound to your project for Composer access — no payment step.

Refundable within 14 days of first purchase via https://packagento.com/account/refunds/.

## Install via Claude Code or any MCP client

The Packagento MCP server can run the licence + project + Composer steps above in one tool call:

```
purchase_and_install_packages(
  composer_names=["justbetter/magento2-xhprof-profiler"],
  project_id="proj_xxx"
)
```

This handles cart, checkout, licence minting, project activation, and writes auth.json credentials. Connect a client with `claude mcp add packagento https://mcp.packagento.com`. Full setup at https://packagento.com/docs/mcp-setup.

## Vendor

justbetter is a Magento 2 vendor on Packagento. See https://packagento.com/justbetter.md for their full catalogue.

