Skip to main content

Find the slow function in a Spiral request

Install the XHProf extension and the spiral/profiler package, add its bootloader and middleware, and set PROFILER_ENDPOINT to Buggregator. Each request of your Spiral app is then profiled, and Buggregator shows its call graph, flame graph and top functions, so you see which function takes the time.

Steps

1. Install the XHProf extension

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

2. Install the Spiral package

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

3. Add the bootloader

Add it to the LOAD list of your kernel.

php
protected const LOAD = [
    // ...
    \Spiral\Profiler\ProfilerBootloader::class,
];

4. Profile every request

Add the middleware first in the global middleware list of your routes bootloader.

php
use Spiral\Bootloader\Http\RoutesBootloader as BaseRoutesBootloader;
use Spiral\Profiler\ProfilerMiddleware;

final class RoutesBootloader extends BaseRoutesBootloader
{
    protected function globalMiddleware(): array
    {
        return [
            ProfilerMiddleware::class,
            // ... your other middleware
        ];
    }
}

To profile one route only, put ProfilerMiddleware::class in its #[Route] attribute's middleware argument instead. For controllers, queue jobs, gRPC or events, the package README shows ProfilerInterceptor (https://github.com/spiral/profiler/tree/3.0).

5. Point it at Buggregator

Profiling is on by default (PROFILER_ENABLE=true). Set the endpoint and the name shown on each profile. In Docker Compose, replace 127.0.0.1 with the Buggregator service name.

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

6. Profile only the request you ask for

Profiling every request slows each one down. Set this, then send the header X-Spiral-Profiler-Enable: 1 with the request you want profiled.

dotenv
PROFILER_MIDDLEWARE_DEFAULT_ENABLED=false

7. Read the profile

Open http://127.0.0.1:8000 and the profile. Call graph colors nodes from white to dark red: the darker, the more it costs. Flame graph shows where time is spent. Top functions ranks functions by CPU time, wall time, memory and call count.

Why it works

XHProf records CPU time, wall time and memory for every function call in the request. The middleware starts it before your code runs and sends the result to Buggregator's /api/profiler/store endpoint on port 8000.

Checked on with Buggregator v2.13.0

Setup in 60 seconds

No registration. No account. Start it and point your app at it.

1

Start Buggregator

One file, installed with one command. The web UI opens on port 8000. Prefer containers? Use the Docker tab.

$ curl -fsSL https://raw.githubusercontent.com/buggregator/server/master/scripts/install.sh | sh
$ HTTP_ADDR=127.0.0.1:8000 \
  SMTP_ADDR=127.0.0.1:1025 \
  VAR_DUMPER_ADDR=127.0.0.1:9912 \
  MONOLOG_ADDR=127.0.0.1:9913 \
  PROXY_ADDR=127.0.0.1:8080 \
  ~/.local/bin/buggregator

The script picks the file for your CPU: Intel/AMD or ARM.

One file for Linux, macOS and Windows, about 40 MB. Nothing else to install.

This run line keeps it on this computer. Without the *_ADDR settings it listens on all network interfaces

Rather not run a script? Download the file yourself

Runs on Windows, macOS and Linux, on Intel/AMD and ARM.

Prefer not to do it yourself? Your AI agent can set Buggregator up for you