Skip to content

XHProf — Performance Profiling ​

Your app is slow and you don't know why. Memory grows and you can't find the leak. XHProf profiling with Buggregator lets you see exactly which functions consume CPU, wall time, and memory — without setting up Blackfire or any paid profiling service. Profile data goes to the same Buggregator where you already see your exceptions and logs.

Watch introduction video on YouTube.

Use cases ​

  • Find slow functions — the Call Graph and Top Functions table show exactly where time is spent.
  • Detect memory leaks — track memory allocation per function in long-running workers and daemons.
  • Measure optimizations — compare two profiler runs side-by-side to verify your changes actually helped.
  • Profile any request — works with web requests, CLI commands, queue jobs, and any PHP code.
  • All in one place — profiling data next to exceptions, logs, and dumps in the same dashboard.

Analysis modes ​

Call Graph ​

Function calls as a tree. Nodes colored from white to dark red — the darker, the more resources consumed.

XHProf call graph

Collapse recursion ​

Since server v2.16.0. XHProf records each level of a recursive function as its own function: foo, foo@1, foo@2, … A deep recursion then fills the graph with a long chain of copies of one function.

Click Collapse recursion in the call graph toolbar to show one node per function instead. Example: foo that calls itself two levels deep becomes one node foo (3x, 2 recursive):

  • 3x counts the calls of all levels; 2 recursive is how many of them came from deeper levels.
  • The node costs what its outermost call costs. That cost already includes the deeper levels, so nothing is counted twice.
  • A function that calls itself gets an arrow from the node back to itself (a loop).
  • A cycle through other functions (a calls b, b calls a again) gets one arrow back up, drawn beside the nodes.

Click the button again to see the levels as XHProf wrote them. The choice is remembered in your browser and applies to every profile you open.

The same in the API: GET /api/profiler/{uuid}/call-graph?recursion=collapsed. Without it, or with any other value, the graph has a node per level (recursion=levels). The answer says which one it is in the field recursion: "levels" or "collapsed".

Flame Graph ​

Stacked function calls showing where time is spent. Useful for spotting repetitive or time-consuming operations.

XHProf flame graph

Top Functions ​

Ranked table of the most expensive function calls — CPU time, wall time, memory, and call count.

XHProf top functions

Profile Comparison ​

Compare two profiler runs side-by-side. For each function, see wall time, memory and calls in both runs and the change, as a value and in percent: green is faster or less, red is slower or more.

Setup ​

1. Install the XHProf extension ​

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

2. Install the profiler package ​

Laravel ​

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

Package documentation

Spiral Framework ​

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

Package documentation

Symfony Framework ​

Please mind, bundle in early development stage - feedback much appreciated

bash
composer require --dev velpl/buggregator-profiler-bundle:^0.3

Package documentation

Any PHP project ​

bash
composer require --dev spiral-packages/profiler
php
use SpiralPackages\Profiler\Profiler;
use SpiralPackages\Profiler\DriverFactory;
use SpiralPackages\Profiler\Storage\WebStorage;
use Symfony\Component\HttpClient\NativeHttpClient;

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

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

$profiler->start();

// Your code here

$profiler->end();

3. Turn profiling on and set the endpoint ​

The packages differ here. The Laravel package and the Symfony bundle send nothing until you turn them on.

Laravel ​

Profiling is off by default (XHPROF_ENABLED defaults to false). Turn it on and set the endpoint:

dotenv
XHPROF_ENABLED=true
PROFILER_ENDPOINT=http://127.0.0.1:8000/api/profiler/store

The profile is named after your app's APP_NAME. Turn profiling off when you do not need it: it slows every request down. The package can also switch it per request with the X-Xhprof-Enabled header, see its documentation.

Spiral Framework ​

Profiling is on by default (PROFILER_ENABLE=true). Set the endpoint and the app name:

dotenv
PROFILER_ENDPOINT=http://127.0.0.1:8000/api/profiler/store
PROFILER_APP_NAME="My app"

Symfony Framework ​

The bundle is off by default. Turn it on in config/packages/buggregator_profiler.yaml:

yaml
buggregator_profiler:
    enabled: true
    application_name: 'My app'
    profiler_url: 'http://127.0.0.1:8000'

Any PHP project ​

The endpoint is the second argument of WebStorage in the example above.

In Docker Compose, replace 127.0.0.1 with the Buggregator service name.

Custom client integration ​

If you're building a custom profiler client, there are three ways to send data:

  • HTTP auth — add profiler to the URL: http://profiler@127.0.0.1:8000
  • Header — send X-Buggregator-Event: profiler or X-Profiler-Dump: true
  • Endpoint — POST to /api/profiler/store