XHProf vs xDebug running on Apache and OSX
My good friend Peter Chester pointed out to me that I should be trying XHProf. It is a PHP profiler created by Facebook and open sourced a while back. To be honest, the reason I did not do this before is because I’m pretty happy using xDebug as a profiler tool, but let’s give XHProf a test ride and compare the results with a xDebug profile for the same site.
Installing XHProf using homebrew
Dependencies. You probably have most of them already, but in case you’re missing any:
brew install autoconf brew install freetype brew install gettext brew install zlib brew install jpeg brew install libpng brew install pcre
You should also install GraphViz if you want to have a nice Call Graph like the one illustrating this post. You have a link to the full version of that (massive) graph at the end of the post.
brew install graphviz
Check your PHP version doing:
If you are running PHP 5.3.x run:
brew install php53-xhprof
Or, if you’re on PHP 5.4.x run:
brew install php54-xhprof
On the other hand, if you’re in PHP 5.2, you should not.Stop reading this immediately and go fix that.
When the brew install process finishes, it will show you the path where XHProf got installed. It will look something like /usr/local/Cellar/php53-xhprof/0.9.2. The .so file is in that directory, and we need to place it in our Apache extensions directory, doing:
sudo cp /usr/local/Cellar/php53-xhprof/0.9.2/xhprof.so /usr/lib/php/extensions/no-debug-non-zts-20090626/
If you’re not sure where is your apache’s extensions directory, edit your /private/etc/php.ini and look for extendion_dir.
Now edit your /private/etc/php.ini, look where all extensions are being loaded and add:
And restart Apache
sudo apachectl restart
Installing XHProf from source
If you’re not using Homebrew, do yourself a favor and install it. In any case, this should do:
pecl download xhprof-0.9.2 tar -xvf xhprof-0.9.2.tar.gz cd xhprof-0.9.2/extension phpize ./configure make make install sudo cp xhprof.so /usr/lib/php/extensions/no-debug-non-zts-20090626/
Profiling a WordPress install
Install this plugin. You’re done. Really.
You need to have WP_Debug set to true, and of course the plugin activated. You’ll see a link on your footer like this one:
Click there and you’re ready to roll.
If for some reason you don’t want to use the plugin to have more control or if you just want to profile a smaller part of your application, you need to to manually start and stop the profiler data gathering using the module functions: xhprof_enable and xhprof_disable.
Conclusions after playing with it for 30 minutes
I must confess, I started doing this exercise being quite sure I wouldn’t be moving away from xDebug anytime soon.
Boy I was wrong.
For the data that’s in both places, I couldn’t find any big difference that would make me suspicious something is fishy.
But there are 3 points that totally tip the scales in favor of XHProf.
- It’s insanely fast and efficient. I didn’t really notice it was actually saving profile data in each page load until I checked the folder.
- Profile log file size is 10x-15x smaller. Really. For the site I’m testing this on, xDebug gives me a 18Mb file, vs a bit less than 1Mb for XHProf. This is great for performance: opening a 20Mb file on any xDebug viewer takes a while. This is so good to be true, so probably XHProf is missing some info that will come to bite me in the future. Will need to research a bit more about that, because I can’t seem to find anything missing.
- Memory profiling. For doing memory diffs on xDebug you need to do Traces, and it’s a completely separated process. In XHProf you see all the information in the same table, you can order by any col, etc.
I would feel really bad if I don’t give xDebug even one point, so here it is: The table view for XHProf is ugly. Big time ugly. xDebug is older and more popular, and have tons of tools to browse the profile data. I use MacCallGrind, which is pretty. But you have KCachegrind which is super powerful, phpStorm has an integrated viewer, there are lots of browser-based viewers. Etc.
Update: xhprof.io Sorry xDebug. You just lost your point.
Here the full version of that image. It shows a homepage load for a WP 3.6 install with only bbPress activated. Click to expand.
And here is the table view.