Skip to content

Profiling

Profiling PHP scripts is a great way to find out what needs to be done to reduce the required time and memory for a particular process.

Xdebug

To profile PHP script we use xdebug.

Read the full docs on Xdebug profiling here

Enabling

To enable profiling, you need to edit the xdebug config

For example:

The file: /etc/php.d/xdebug.ini should look like:

zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable=1
xdebug.max_nesting_level = 2000
xdebug.file_link_format="phpstorm://open?file=/var/lib/lxc/ballicom-akeneo/rootfs/%f&line=%l"

xdebug.profiler_enable=1
xdebug.profiler_output_dir=/tmp
xdebug.profiler_output_name=xdebug.cachegrind.out.%p

Once you have it enabled, you need to make sure you also restart php-fpm if you want to profile web requests: systemctl restart php-fpm

Warning

Do not leave profiling enabled. It can generate a large number of huge files very quickly.

Generating

To generate your profile, simply run the command line script you are profiling, or hit the web page you want to profile.

Each run will generate a standalone profile. Based on the above config, the file will be in /tmp and will be called something like xdebug.cachegrind.out.123

To double check the file has been created, you can run ll /tmp/xdebug*. If this is a long running process, you should see the size of the file increasing.

Tip

As soon as you have your profile, it is sensible to immediately go and disable profiling

Examining

Once you have generated the profile, the next thing to do is view it so that you can see what is going on.

kcachegrind

To do this you need to install kcachegrind

sudo dnf -y install kcachegrind

Then you can open your generated file, for example

kcachegrind /var/lib/lxc/ec-akeneo/rootfs/tmp/xdebug.cachegrind.out.3857

If your profile is very big then this might take a while to load up, so be patient

webgrind

Alternatively to kcachegrind, there is a pure PHP solution called webgrind which provides a different visualisation and you might find it easier.

On the other hand, it only seems to support legacy PHP versions.#

Note

This solution has not yet been tested. Please update this documentation and remove this notice once we have confirmed it is a good solution

PHP SPX

Alternatively to Xdebug, there is a PHP extension called SPX, which stands for Simple Profiling eXtension

This solution can easily provide instantaneous feedback, especially on CLI scripts.

It can also create callgrind files which can then be viewed in kcachegrind as with Xdebug.

Note

This solution has not yet been tested. Please update this documentation and remove this notice once we have confirmed it is a good solution