If you are not familiar with Xdebug it is an extension that allows you to profile your application for various things like performance and memory usage. One of its more usefull features is the ability to profile your application to find out what method are taking the longest so you can clean up those bottlenecks in your application.
This was pretty simple in Xdebug 1.x as it provided a method call for outputting all the profile information in a nice table format up to that point. Since Xdebug 2.x this option has given way to cachegrind files.
Cachegrind provides a nice graphical display of your profiling information but I run Mac OSX and due to my local development environment Fink and Ports are out of the question as they tend to step all over my local development environment so getting KCacheGrind up and running was not going to be fun so much fun I have not even bothered to try and tackle it yet.
What I needed was a way to replicate the old Xdebug 1.x functionality. From this the idea of “PHPGrind” was born. Right now its just a proof of concept but I am hoping with some more understanding of the cachegrind format I can make it into something as useful as the graphical versions.
Xdebug outputs cachegrind files into your temp directory based on the pid of the running process so what we can do is grab the pid of the current page and pass it on to a pop-up window that loads PHPGrind.
PHPGrind then does a very rough parsing of the cachegrind file and compiles and array of data, sorts it by time per function and provides a nice tabled format like what you may of been used to with Xdebug 1.x
You can see both a demo and access the source code at http://solardemo.phpjack.com/phpgrind.phps
The code currently available was thrown together over lunch and regex is not my strong point so its not the prettiest nor the fastest solution for parsing cachegrind files but it works.




October 22, 2008 at 3:15 pm
Or use Webgrind (http://code.google.com/p/webgrind/) a web frontend for cachegrind files
October 23, 2008 at 12:27 am
Or use MacCallGrind
http://www.maccallgrind.com/
October 23, 2008 at 1:25 pm
Thanks for this. I’ll use it in my writeup of the XDebug profiling features. I’ve been doing a series on my blog (www.brandonsavage.net) about XDebug, and this dovetails nicely with that.
Thanks for putting this together!
October 28, 2008 at 8:32 pm
Those solutions require an extra step of importing the cachegrind file, This method provides a quick way to access the basic information needed while doing performance tuning.
I am looking into a way to import/direct webgrind to load the info instead of using my own messy code though.
October 30, 2008 at 6:32 pm
@cyberlot
As one of the developers of webgrind I would be very interested in knowing what extra step you are referring to.
When I use webgrind i leave the dropdown profile selector on "auto" which will cause it to display the profile of the latest file when the update button is clicked. That is only one step.
Alternatively it is possible to specify what file to display using the url. Fx:
http://localhost/webgrind/#cachegrind.out.2345
If you have suggestions for improvements of webgrind don’t hesitate to add an issue to the issuetracker on http://code.google.com/p/webgrind/issues/list
November 26, 2008 at 5:32 am
I’m the developer of MacCallGrind, and yes, with MacCallGrind (and WindCacheGrind and Cachegrind) you need to open the file before you can parse it. I don’t think that’s really a big issue though, considering how often you need to spit out and analyse profiler data.
However, if this is annoying for anyone using MacCallGrind, I’m more than happy to look at other ways to analyse the data without opening a file. Polling a directory perhaps? I don’t know, just drop me a line and let me know what you’d like to see, as it’s definitely an ongoing project.