Skip to content

Integration — Xhprof profiler

Buggregator is not just useful for dumping variables; it’s an essential instrument for deeply analyzing your PHP applications to locate and resolve efficiency issues.

To start, you only need to install the profiling package into your PHP application and begin collecting metrics. The collected metrics will be automatically sent to Buggregator, where you can analyze the PHP function call trace. With a comprehensive set of tools, you can identify slow calls and work on optimizing them. You can keep sending metrics after making changes to ensure that all unoptimized code is streamlined.

Watch introduction video on YouTube.

Buggregator offers three types of analysis to help you enhance the performance of your PHP applications:

  1. Call Graph – This shows the function calls in a tree structure. The nodes in the call tree vary in color from white to dark red. The darker the color, the more resources are being consumed by that call. This visual representation helps you quickly identify which parts of your code are using the most resources, allowing you to pinpoint where optimizations are most needed.

    xhprof-callgraph

  2. FlameGraph – This graphical representation stacks function calls and shows which functions are called by which other functions. It's particularly useful for spotting repetitive or time-consuming operations that might not be obvious. The FlameGraph helps you see how functions interact and where time is being spent in your application, making it easier to target specific areas for improvement.

    xhprof-flamegraph

  3. Top 100 Functions – This table lists the top 100 function calls that consume the most resources. It provides a clear, focused list of potential problem areas, so you can start optimizing the parts of your application that will make the biggest difference to overall performance.

    xhprof-top-func

Installation extension

One of the way to install Xhprof is to use PECL package.

bash
pear channel-update pear.php.net
pecl install xhprof

Spiral Framework

If you are using Spiral Framework you just need to install the spiral/profiler package.

bash
composer require --dev spiral/profiler:^3.0

Note: Read more about package usage in the package documentation.

Laravel

If you are using Laravel Framework you just need to install the maantje/xhprof-buggregator-laravel package.

bash
composer require --dev maantje/xhprof-buggregator-laravel

Note: Read more about package usage in the package documentation.

Vanilla PHP

If you are using Vanilla PHP you just need to install the spiral-packages/profiler package.

bash
composer require --dev spiral-packages/profiler

And then configure a WebStorage client from the package to send data to Buggregator.

Here is an example:

php
use SpiralPackages\Profiler\Profiler;
use SpiralPackages\Profiler\Driver\DriverInterface;
use SpiralPackages\Profiler\DriverFactory;
use SpiralPackages\Profiler\Storage\StorageInterface;
use SpiralPackages\Profiler\Storage\WebStorage;
use Symfony\Component\HttpClient\NativeHttpClient;

$storage = new WebStorage(
    new NativeHttpClient(),
    'http://127.0.0.1/api/profiler/store',
);

$driver = DriverFactory::detect();

$profiler = new Profiler(
    storage: $storage, 
    driver: $driver, 
    appName: 'My super app', 
    tags: [
        // global tags
        'env' => 'local',
    ]
);

$profiler->start(ignoredFunctions: []);

// Here is your code you want to profile

$profiler->end(tags: [
    // Tags for specific requests
]);

Where http://127.0.0.1/api/profiler/store is the endpoint to send data to Buggregator.

Configuration

After installing the package, you need to configure it. The package provides predefined environment variables to configure the profiler.

dotenv
PROFILER_ENDPOINT=http://profiler@127.0.0.1:8000
PROFILER_APP_NAME="My super app"

Client configuration

When integrating with Buggregator, especially if you're developing a custom client, it's essential to understand how to correctly configure the data transmission. This guide outlines various methods to designate your requests as xhprof requests

Using HTTP auth

Add profiler to the host name, e.g. http://profiler@...

Using header

Add a header X-Buggregator-Event with value profiler or just add X-Profiler-Dump with any value.

Special endpoint

You can use special endpoint /profiler/store to send data to Buggregator.