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
pear channel-update pear.php.net
pecl install xhprof2. Install the Spiral package
composer require --dev spiral/profiler:^3.03. Add the bootloader
Add it to the LOAD list of your kernel.
protected const LOAD = [
// ...
\Spiral\Profiler\ProfilerBootloader::class,
];4. Profile every request
Add the middleware first in the global middleware list of your routes bootloader.
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.
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.
PROFILER_MIDDLEWARE_DEFAULT_ENABLED=false7. 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.
Related tips
- Profiler
Find the slow function in a Laravel page
Install XHProf and one Laravel package, point it at Buggregator, and read the flamechart and top functions of every request.
- Sentry
Spiral errors through the Sentry bridge, locally
Install spiral/sentry-bridge, register SentryReporterBootloader and set SENTRY_DSN to Buggregator. Spiral exceptions show up locally with stack traces.
- Xdebug
Record every call of one request and find the slow one
Record an Xdebug function trace, send the file to Buggregator, and see the slowest calls, the call tree and a search over every call.
Checked on with Buggregator v2.13.0