Pair program with me! profile for carousel at Stack Overflow, Q&A for professional and enthusiast programmers

4/10/2014

How to use Xdebug profiler and Kcachegrind tools in PHP project?

I use more and more awesome Xdebug profiler in combination with Vdebug plugin and Kcachegrind.

Xdebug is standard PHP debugger. Vdebug is Vim plugin that let's you easily debug and trace PHP (and not just PHP) code from Vim. Kcachegrind is amazing piece of software which let's you visually represent whole application calling stack, and do a memory profile and discover bottlenecks.

You need some time and slight tweaks to enable all this tools to work.
But when you do that, you have awesome toolset that let's you dissect you code in a number of different ways. I mostly use it for learning new API-s, writing my extensions and looking at architecture and design of existing code (these days mostly Laravel).

Fist step is to install and enable xdebug. Here is how to do that on ubuntu:

    suto apt-get install php5-xdebug

And check in your php.ini that xdebug is enabled.

Here is my Xdebug setup from /etc/php5/apache2/php.ini file:

    zend_extension=/usr/lib/php5/20121212/xdebug.so
    xdebug.remote_enable=on
    xdebug.remote_handler=dbgp
    xdebug.remote_autostart=1
    xdebug.remote_host=localhost
    xdebug.remote_port=9000
    xdebug.profiler_output_dir="/var/log/xprofile/"
    xdebug.profiler_append=On
    xdebug.profiler_enable_trigger=On
    xdebug.profiler_output_name="%R-%u.trace"
    xdebug.trace_options=1
    xdebug.collect_params=4
    xdebug.collect_return=1
    xdebug.collect_vars=0  
    xdebug.profiler_enable=1
     ;xdebug.auto_trace=Off

As you can see it is divided into xdebug settings that let's you debug, and setting that allows profiling. Here you can find some advanced settings, which let's you save file names based on function call. By default profiler saves all files with some random prefix, and it is hard to distinguish between them

To install Vdebug on Vim, just install this plugin
https://github.com/joonty/vdebug and follow installation process. After that you wil be able to dubug and set breakpoints with builtin vim mappings.

In order to save and use output from profiles, you need to create default location for files. This is my location:

    xdebug.profiler_output_dir="/var/log/xprofile/"

Now, every time you start a project in the browser, a new profile file will be created.

Next tool is Kcachegrind. This is project home:

    http://kcachegrind.sourceforge.net/html/Home.html

But it can be easily installed through CLI of software center. Kcachegrind allows you to have visually insight into project calling stack. For me it was  far better learning experience then reading tons and tons of books. With this, I am actually "in the source code", without external explanation about that code. I can see how authors were designing whole application and have some advanced insight into whole process of app creation.

There is one gotcha with Xprofile. If you don't delete generated files, they can easily accumulate and consume lot of memory. You won't be always  using xdebug but profiler will always generate those files. So you need to manually delete them. It can be also done with some kind of deamon process and backround script.

This is how I solve this problem, with one command. I created alias like this:

     alias xd="sudo find /var/log/xprofile/* -mmin +0 -exec rm {} \;"

And placed it into my .zshrc file. With this alias command, all files inside xprofile directory older then 0 minutes will be deleted - which means all files will be deleted.

Alternatively, you can create new bash script/file, with this command:

    #!/usr/bin/sh
    sudo find /var/log/xprofile/* -mmin +0 -exec rm {} \;

Place it somewhere on your path, provide permissions and you will have globally executable command that will clean xprofile folder.

Thanks for reading.


No comments:

Post a Comment