PHP Profiling with SPX

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):

brew install zlib git clone https://github.com/NoiseByNorthwest/php-spx.git cd php-spx git checkout release/latest phpize # specific to work with Homebrew ./configure --with-zlib-dir=$(brew --prefix)/opt/zlib make install

Then, you need to set the following three settings in php.ini:

spx.http_enabled=1 spx.http_key="dev" spx.http_ip_whitelist="127.0.0.1"

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:

./flow server:run open http://127.0.0.1:8081/?SPX_KEY=dev&SPX_UI_URI=/

Now, you can enable profiling for your browser session by switching the "Enabled" toggle (and you can configure lots of other things as well):

Enable the profiler

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.

Select the profiles here

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 :)

Profiling UI

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:

SPX_ENABLED=1 ./flow your-command-here SPX_ENABLED=1 SPX_FP_LIVE=1 ./flow your-command-here
Command Line Profiling

By setting SPX_REPORT=full, the report will appear in the web UI and can be analyzed in detail:

SPX_ENABLED=1 SPX_REPORT=full ./flow your-command-here # then open http://127.0.0.1:8081/?SPX_KEY=dev&SPX_UI_URI=/ and browse profile

Next Steps

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 :)