Profile Symfony APIs and workers
Install the XHProf extension and the velpl/buggregator-profiler-bundle package, then turn the bundle on. It profiles each HTTP request and each console command of your Symfony app and sends the profile to Buggregator. You get a call graph, a flame graph and a top functions table for JSON API calls, commands and Messenger workers. It works next to the Symfony web profiler.
Steps
1. Install the XHProf extension
pear channel-update pear.php.net
pecl install xhprof2. Install the bundle
Add it as a dev dependency.
composer require --dev velpl/buggregator-profiler-bundle:^0.33. Turn it on
The bundle sends nothing until enabled is true. In Docker Compose, replace 127.0.0.1 with the Buggregator service name.
# config/packages/buggregator_profiler.yaml
buggregator_profiler:
enabled: true
application_name: 'My app'
profiler_url: 'http://127.0.0.1:8000'4. Call your API or run a command
Each main HTTP request is one profile, from the start of the request to its end. Each console command is one profile, from the start of the command to its end.
curl http://127.0.0.1/api/orders
bin/console app:import-products5. Give a worker an end
A worker is a console command too, so its profile is sent when it stops. Let it handle a few messages and exit.
bin/console messenger:consume async --limit=106. Read the profile
Open http://127.0.0.1:8000 and pick the profile. Call graph colors nodes from white to dark red: the darker, the more it costs. Flame graph shows where the time goes. 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. The bundle starts the profiler when a request or a command begins and sends the data to Buggregator when it ends. That is why it also covers code with no page to look at: JSON APIs, commands and workers.
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.
- 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.
- Sentry
Symfony Messenger failures with their stack trace
Turn sentry-symfony on in dev and point SENTRY_DSN at Buggregator. A failing Messenger handler shows up as an error, tagged with the message class.
Checked on with Buggregator v2.13.0