SPX is a profiling extension for PHP that allows you to profile your PHP scripts and get detailed insights into their performance. It differentiates itself from other similar extensions by being totally free, simple to use, and capable of collecting a wide range of metrics. With SPX, you can easily profile your scripts by setting an environment variable or using a web UI, without the need for manually instrumenting your code or using a dedicated browser extension.
It also offers a comprehensive web UI that allows you to enable and configure profiling, view profiled script reports, and perform in-depth analysis using interactive visualizations such as timelines, flat profiles, and Flamegraphs.
SPX is a powerful tool for optimizing the performance of your PHP scripts and ensuring they run smoothly. It works without any adjustments in the PHP Scripts themselves.
Installation on Mac OS
Install PHP-SPX in the following way on Mac OS (taken from the official docs, and slightly adapted to use zlib from Homebrew):
Then, you need to set the following three settings in php.ini:
This enables the HTTP UI, sets the "login token" to dev, and allows only connections from Localhost.
Creating and analyzing profiles in web context
Now, you can use any PHP server (such as the built-in one, or Nginx, or ...) to start the PHP-SPX UI. Simply go to http://your-server-url/?SPX_KEY=dev&SPY_UI_URI=/ to open the web UI:
Now, you can enable profiling for your browser session by switching the "Enabled" toggle (and you can configure lots of other things as well):
After you have executed the web requests you want to profile in your application, you see the list at the bottom of the Profiler UI.
The Profile Analyzer
When you select one request flow, you get the profile Analyzer UI which looks like the following screenshot.
At the top, you get the timeline sequence of all method calls, at the bottom left a summary table sorted by metrics, and at the bottom right a flame graph for the selected time frame.
Make sure to get acquainted with the Analyzer UI, as it is really powerful :)
Creating and analyzing profiles in CLI
You can also analyze CLI requests, by setting SPX_ENABLED=1 - and optionally, for a live-refreshing mode, set additionally SPX_FP_LIVE=1. Then, after the CLI execution, you directly get a profile printed:
By setting SPX_REPORT=full, the report will appear in the web UI and can be analyzed in detail:
We want to use PHP-SPX as profiler in all of our PHP based projects. Right now, this post explains how this is done for locally-executed scripts. As a next step, we will figure out how to run this inside our Docker environments easily. There will be a blog post about this as well, as soon as it's ready :)