Andy Gale

Freelance Web Developer, DevOps, Sys Admin, Bristol, UK

Before You Implement Caching On Your Website

An introduction to optimising your code with Xdebug

Ideally you would have designed your website or web app from the ground up to take advantage of caching. That is not always possible in the real world so before you implement caching on the site its useful to actually find out exactly what is causing the site to be slow. This is called profiling.

What is Profiling?

When we profile our application we are trying to find the functions, methods or sections which are taking too long. Nobody writes perfect code first time so it is useful to investigate what happens in your code, when it happens and how long that takes. It could be one loop is happening too often or maybe there's an SQL query that is really killing the performance of your website. It could of course be anything and that is why we need to profile.

If you want more background, have a look at the profiling Wikipedia article.

Some languages have a built in methods of profiling, we're going to profile a PHP script with Xdebug's built in profiler. You are going to need to install XDebug.

How Do I Install Xdebug?

I've included basic instructions below for Mac OS X and MacPorts, FreeBSD, Ubuntu or Windows (which you use at your own risk) but if you're using something else Google "install xdebug [platform]" and you'll probably find something. But you already knew how to use Google anyway, didn't you?

MacPorts

Kevin Hallmark's instructions Getting xDebug Working on Mac OS X with MacPorts.

FreeBSD

Do this as root (or sudo)

# cd /usr/ports/devel/php-xdebug
# make install

The instructions the port gives you after it installs didn't work for me. I had to put:

zend_extension=/usr/local/lib/php/20060613/xdebug.so
in /usr/local/etc/php/extensions.ini which worked.

Ubuntu

HOWTO: Install xdebug for PHP5 at the Ubuntu forums.

Windows

If your doing your PHP development on Windows you should consider using a development environment that resembles the live environment you are deploying to. See my previous post about developing in virtual machines. However you can use Xdebug with PHP running on Windows by installing the .dll extension. Instructions here.

Finally...

If you haven't already done so, put the following in php.ini and alter the path of xdebug.profiler_output_dir to something you can access from your desktop development environment.

xdebug.profiler_enable=1
xdebug.profiler_output_dir=/tmp

Remember to restart PHP and/or your web server to ensure your configuration changes have taken effect.

Using the Xdebug Profiler

The Xdebug profiler outputs files in cachegrind format. You now need something to read these files.

The interface of each is slightly different but they essentially show in a tree format the execution of each function and method and how long it takes. It is then possible to find the bottlenecks in your code.

Lets try an example. Run the following script:

for ($i = 0; $i < 1000; $i++) {
    echo "$i\n";
}

Now open WinCacheGrind, MacCallGrind or KCacheGrind and open the cachegrind file from wherever your specified as xdebug.profiler_output_dir. You will see that the execution hardly takes any time at all. Now alter your test script to the following:

function echo_i($i)
{
   echo "$i\n";
}

for ($i = 0; $i < 1000; $i++) {
    echo_i($i);
}

The cachegrind files are named with the process id so the later dated one with (probably) the higher number at the end will be the most recently executed script. The above change is a simple example but when you load the second cachegrind file you will see that calling a function has a considerable overhead. Now I'd recommend executing a more complicated script, maybe the homepage of your website. You can follow exactly what happens, what takes time and see what can be improved.