2025-03-03 11:03:59 - No Comments
PHP Profiling
In this write up I compare 5 different tools to profile a PHP application both from the console and web. All of them can gather statistics that you later can review. Some of them are online and some of them off line.
xdebug
Cost: Free
Let's start with the most common one what you use to debug your application.
It can easily be installed on most platforms together with kcachegrind
which is a capable tool to look at he logs.
sudo apt install php-xdebug
sudo apt install kcachegrind
Next up we need to update the configuration file.
sudo vi /etc/php/8.3/cli/conf.d/20-xdebug.ini
The most important part here is to add an output directory for your profile logs but you can also set the debug mode to profiling or development if you want to catch all calls to PHP and log the result.
zend_extension=xdebug.so
xdebug.output_dir="/tmp/xdebug"
Another way to initiate a session is to use a environment flag to start.
XDEBUG_MODE=profile php test.php
Last but not least if you want a web experience to review logs webgrind could be for you. Still I don't think you will see more or less data this way as the logs contain the same information but you might prefer this way of reviewing our logs.
git clone https://github.com/jokkedk/webgrind.git
cd webgrind
php -S localhost:8080
Excimer
Cost: Free
Next tool excimer is a bit of an odd tool, I'd never heard of it but it has ready packages in Ubuntu so I thought I might as well try it.
sudo apt install php-excimer
This tool is readily installed into your system so the only thing you need to do is add code to start the gathering of data and then store if when the application shutdown.
function startExcimer() {
static $excimer;
$excimer = new ExcimerProfiler();
$excimer->setPeriod( 0.001 ); // 1ms
$excimer->setEventType( EXCIMER_REAL );
$excimer->start();
register_shutdown_function( function () use ( $excimer ) {
$excimer->stop();
$data = $excimer->getLog()->getSpeedscopeData();
$data['profiles'][0]['name'] = 'test.php';
file_put_contents('/tmp/speedscope.json', json_encode($data,
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ));
});
}
startExcimer();
More examples could be found behind this link:
https://www.mediawiki.org/wiki/Excimer#Examples
hxprof
Cost: Free
This tool was originally released as source available by Facebook back in 2009 but it seems they never worked on the repository. It have since been maintained by a lot of different peoples. Ten years later it seems there is a regular releases a couple of times per year.
Installing the tool is a little bit more involved that previous tools. We need to clone the repository or download a release source tar archive. Next you add the PHP development tools and run phpize
to add the build files for the repository. We will then configure and make the repository and install the extension into your currently running PHP installation.
git clone https://github.com/longxinH/xhprof
cd xhprof/extension
sudo apt install php-dev
phpize
./configure --with-php-config=/usr/bin/php-config
make
make test
sudo make install
The installation will not activate the extension so we need to edit one of the many configuration files that PHP has. Below I added a configuration file for the CLI version on PHP but it will work for your FPM or other web extension use.
sudo vi /etc/php/8.3/cli/conf.d/20-xhprof.ini
The configuration file below will add the extension to PHP and define the output directory for the files generated by xhprof. We also set a flag for collecting additional information. There are a couple of more flags to set that you be found at the github repository
[xhprof]
extension = xhprof.so
xhprof.output_dir = /tmp/xhprof
xhprof.collect_additional_info = 1
Below we have some code that will extract the information from your application. We set flags to enable CPU and memory profiling. Then you run your code and after that disable and gather the data from the run. Next up we use the tools to generate report data and an URL that we can use with the HTML interface to view the data.
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
/* --------- YOU CODE -------- */
$xhprof_data = xhprof_disable();
$XHPROF_ROOT = "/tmp/xhprof/";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_testing");
echo "http://localhost/xhprof/xhprof_html/index.php?run={$run_id}&source=xhprof_testing\n";
Tideways
Cost: 99 € / month
Tideways is an online platform so you will send your profiling information to them. Their interface is quite nice and quick to find memory leaks and CPU bottlenecks.
To install it you can install APT packages on Debian / Ubuntu based systems. If you want their latest releases you need to add their source package into a new list file. You need to add a key to verify the package from tideways. Next we update our repository and install the PHP extension, the tideways daemon and command line tools.
echo 'deb [signed-by=/usr/share/keyrings/tideways.gpg] https://packages.tideways.com/apt-packages-main any-version main' | sudo tee /etc/apt/sources.list.d/tideways.list > /dev/null
wget -qO - 'https://packages.tideways.com/key.gpg' | gpg --dearmor | sudo tee /usr/share/keyrings/tideways.gpg > /dev/null
sudo apt-get update
sudo apt-get install tideways-php tideways-daemon tideways-cli
The package will add a tideways configuration file but we need to edit it to add our information.
sudo vi /etc/php/8.3/cli/conf.d/20-tideways.ini
Below you see an example of this configuration file where I've added my API key and sample rate. More information about the sample rate can be read at sampling
extension=tideways.so
tideways.api_key=tw_AWUtesttesttesttest86
tideways.sample_rate=25
To use the CLI tool you first import another key to your system then you can run a particular script and store the data to your organization and a project. In my case the organization is DanielPersson and the project I created is called Test.
tideways import kkpM2Rtesttesttesttesttesttesttesttesttesttesttesttesthux
tideways run DanielPersson/Test php test.php
Read more at tidaways profiling page
blackfire.io
Cost: 498,75 € / year
Last we have the blackfire tooling that was suggested as a good option to do profiling on my PHP application. I wanted to test it out and see how it works. Sadly I can't run a test without getting a license. They have a demo environment where you can see general information about how the system works but to get a developer licence it will cost you 30+ euro per month. Another problem is that you can't buy a license for developers per month you need to pay per year. And with taxes and everything you need to pay almost 500 euro to run a test. Not worth it for my review but this tool might be an option for you.
Play around with the demo to decide for yourself.
Be the first to leave a comment!