The joy of profiling! Profiling is a powerful way to fine tune your PHP applications before releases. It helps you to pinpoint loose code and track down computationally expensive loops that can be easily refactored to speed up your application.

Since PHP has to load and run your entire application in one go, as opposed to storing parts of your application in memory for re-use, profiling is key to speedy performance and ensuring that your application is not a resource hog.

Doing some basic profiling for the upcoming LoadAvg 2.1 release we were able to track down 1 processor intensive inner loop that was not necessary, and refactored 2 core inner functions that were being used for dataset processing and were being called thousands of times. After refactoring we saw a speed increase of up to 5x in application speed.

1. Install xDebug and KCacheGrind

xDebug is easy to install and to configure, but there are a few tricks needed to ensure that its running properly. We will assume that you already have your LAMP stack installed and PHP is up and running. The next step will be to install the PHP developer tools, which includes PECL :

Then use PECL to install Xdebug :

It is important to know the xDebug on its own only generates debugging information, and so you will need another tool to make sense of this information. Kcachegrind is a powerful desktop tool and is easy to install, we will be using this here :

*alternatively you can install webgrind – a web based front end if you dont have direct access to the server and need realtime access to the data, however note that you can copy the xDebug data to a local machine for analysis, and debugging and profiling is really system intensive so shouldn’t be done on production systems.

2. Configure PHP to use xDebug

Next step is to configure PHP to use xDebug. This means edit your php.ini settings file and add support for the xDebug extension. Since we are doing browser based profiling, we will also need to tell xDebug where to store its files so we can access them.

We store our files in /xdebug but you are free to store them wherever you wish – as long as its not in /tmp and is writeable. Best place is really your home directory.

Note that /tmp is the default for Xdebug, however it doesn’t work for browser profiling since due to security restrictions apache wont write to /tmp without making a mess of things.

First make your Xdebug storage directory

Now open up your php settings file with your editor (we use vim here) and add the settings needed to integrate Xdebug with php.

This is the code block you will need to add. The best place to add this is at the bottom of the php.ini file in its own section right before the ;end. Replace “/xdebug/” with the location of your xdebug directory created above.

Now restart apache / php and if you get no errors then you should be up and running

NOTE: At this point everything should work perfectly! However SELiniux can make using Xdebug a pain as it restricts where apache can write, where php can write and where scripts can run. If you are on a development bops then its best to turn it off and reboot before moving ahead.

To do so, edit /etc/sysconfig/selinux and change grin permissive to disabled and restart, as illustrated here.

3. Testing at the command line

You will need a php file to test on, easiest way to to quickly create a quick info.php file with the following contents.

Now lets do a quick command line test, by calling php with xdebug set to on

You should see a cachegrind.out.xxxxx file in your xdebug folder if all went well.

*NOTE you should regularly empty your /xdebug folder to clear out old profiling data

4. Configure your browser to generate profile data

Now that you have xDebug installed and integrated with PHP, the next step is to configure your browser to generate profile data for you on demand. There are plugins available for all major browsers here.

The easiest Xdebug
This extension for Firefox was built to make debugging with an IDE easier.

Xdebug Helper for Chrome
This extension for Chrome will help you to enable/disable debugging and profiling with a single click.

Xdebug Toggler for Safari
This extension for Safari allows you to auto start Xdebug debugging from within Safari.

Xdebug launcher for Opera
This extension for Opera allows you to start an Xdebug session from Opera.

We will cover ‘The easiest Xdebug’ for Firefox since Firefox is the default browser, however the functionality is pretty much the same across the other browsers listed above.

Once installed the plugin allows you easily to turn profiling on and off via the browser. You turn profiling on by clicking on the small flowchart icon in your browser, it should light up and animate. On chrome, you get a drop down menu where you can enable profiling alone.

When profiling is on, every page load of script on your server will generate a cachegrind.out.xxxxx file in your xdebug folder. When its off, no profiling data is created.

If no data is showing up, see my note above about SELinux!

5. Viewing Xdebug profile data

Now comes the fun part. With kcachegrind you can easily load up your profile data and dig into what makes your application tick, and how to optimize it.

To start, fire up kcachegrind via the Applications menu (under Applications->KCacheGrind) or from the command line. Once kcachegrind is running, simply open the profile data you wish to inspect from your debug folder.


You can then start to dig in by looking at the Incl, Self and Called columns, click them to sort accordingly. Sorting by Self lets you see the computationally expensive functions and sorting by Called shows you functions that are called frequently in your app and that should be optimized.

This tutorial is about getting up and running, we can do another one on using KCacheGrind itself later on. In the mean time, here are some great links:

Find out more about xDebug and profiling here

Find out more about KCacheGrind and profiling here

And some good getting started tips are found here