When I am trying to optimize my PHP code, one of my prefered tools is xdebug. It provides me with excellent error messages, outputs code tracing files & insight into memory usage.
By using kcachegrind or webgrind – wincachegrind crashes *all* the time – to parse the cachegrind files created by xdebug, you get a good idea of what your code is doing most of the time. It’ll show you which functions are called, by who, which arguments are passed and tons more. Check it out.
Now, there was one small thing I couldn’t find anywhere. A program to parse the trace files that xdebug generates. True, xdebug already provides the option to output this information into HTML, but it’s not really what I was looking for. I want to get quick information in the blink of an eye! That’s why I have created my own trace file parser, based on PHP of course.
What is it?
This xdebug trace parser will show you, for each function:
- How much memory was used on entry & exit
- How much time it costed to execute the function
- The exact line the function was called from plus insight into the code by clicking a link
- Whether the function is native PHP or not
It also has a basic “alerting” feature, which will allow you to set triggers on memory usage & execution time. You can specifiy that if the memory or time jump is greater than a certain value, to show this in the output.
It should be obvious that you’ll need to install and configure xdebug for your PHP installation. See http://www.xdebug.org for more information on this.
Your xdebug config should look a bit like this, don’t forget to update your paths
[XDebug] ;; Only Zend OR (!) XDebug zend_extension_ts="\php\ext\php_xdebug-2.0.4-5.2.8.dll" xdebug.auto_trace=1 xdebug.trace_format=1 xdebug.profiler_append=1 xdebug.profiler_enable=1 xdebug.profiler_enable_trigger=0 xdebug.collect_params=4 xdebug.collect_return=1 xdebug.trace_output_dir="D:\webserver\xdebug" xdebug.trace_output_name= %t.trace xdebug.profiler_output_name = %s.%t.profile xdebug.profiler_output_dir="D:\webserver\xdebug"
Now, give it a try and downlod the xdebug trace file parser here!
Once you’ve downloaded the trace file parser, extract it to a directory inside your website root and surf to the URL. If everything works out you should be able to select trace files onceyou have ran an other PHP script on your server. This trace file parser will *not* show it’s own traces because that would make things even more complicated!
Comments & suggestions!