Here we are, part 4b. By using XDebug, a pecl extension, our php code can be analyzed pinpointing the slower functions.
Installing XDebug is easy:
pecl install xdebug
Now, you’ll have to enable it in the php.ini file:
extension=xdebug.so xdebug.profiler_enable=1 xdebug.profiler_output_name=profiler-%s-%t.cache xdebug.profiler_output_dir=/tmp/xdebug
Don’t forget to create the directory and give apache enough permissions to write in it.
So after restarting apache you’ll see new files showing up under ‘/tmp/xdebug/’.
These are, as the XDebug documentation says ‘cachegrind compatible files’.
As I’m using putty on windows to connect to my webserver and the webserver does not run a GUI, I’ll be using wincachegrind to display the information.
To make it easy for myself, I use winscp and make it so that the cachegrind files are opened with WinCacheGrind.
Setting this up goes beyond the scope of this post and greatly depends on your setup/way of working.
Anyway, up till now we managed to install XDebug, enabling the extension, generate the cachegrind files and now opening them with WinCacheGrind.
Upon opening such a file in wingrind, you’ll see something similar to this:
As this blog post was made while I was profiling one of my own sites, I’ll use a real world example.
In the right hand side, the {main} takes 89ms to process.
Let us expand this to see what function is taking the most time.
When looking at the functons that take the most time. I noticed, as you can see in the screenshot above, the Component->startup and the beforeFilter both take 13ms.
The component is the cakephp cookie class, it seems the startup of this class takes 12ms. To be exact, it’s the decrypting of the cookie that takes 11ms.
The same is true for the beforeFilter, in that method I read the cookie, taking 12ms, because it decrypts again.
After seeing this, I changed the code so that on the first hit on the site the information in the cookie is is copied into the session, as this was super fast(less then 1ms). Saving me 2 times 12ms.
That comes down to 27% faster!
To put things even more in context. This was a base class, all my pages inherited from that class, so it saved me those 24ms on EACH request.
Imagine doing that to all pages, or one of those heavy visited pages on your own site.
Worth the 10 minutes it takes to install XDebug with WinCacheGrind, no?
Optimizing your web server: Part 1 – Gzip
Optimizing your web server: Part 2 – Keep Alives
Optimizing your web server: Part 3 – Opcode Caching
Optimizing your web server: Part 4a – PHP
Optimizing your web server: Part 4b – XDebug Profiler