Using Kcachegrind to profile your code

1.)  Install xdebug -- I'm running MAMP so it's already installed and in php.ini I just needed to turn it on here are my settings:

[xdebug]

zend_extension="/Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"

;xdebug.profiler_output_dir = "/tmp/xdebug/"

;xdebug.profiler_enable = On

xdebug.remote_enable=On

xdebug.remote_host="belldev"

xdebug.remote_port=9000

;xdebug.remote_handler="dbgp"

;xdebug.idekey=ECLIPSE_DBGP

;xdebug.remote_mode=jit

2.) Install QT, download is actually here:  http://qt-project.org/downloads

3.) Put these settings in your php.ini file (works with MAMP as I said):

 ;profiler settings

xdebug.profiler_append=1

;xdebug.profiler_append=0

xdebug.profiler_enable=0

xdebug.profiler_enable_trigger=1

xdebug.profiler_output_name = cachegrind.out.%s

xdebug.profiler_output_dir=/Users/barnettech/kcachegrind/xdebug/

 

You can see i installed kcachegrand under my home directory.  I manually created the directory xdebug here:  /Users/barnettech/kcachegrind/xdebug/

(You can see where i put kcachegrind as well in the path above)

You'll have to make sure your actual xdebug.so file gets installed here:  /Applications/MAMP/bin/php/php5.3.6/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so or just change whats in the php.ini to point to it correctly.

I believe this line:  xdebug.remote_host="belldev"  means that to profile a different code base (MAMP virtual host) you have to change this  (to profile spdev:8888 instead of belldev:8888 for instance change belldev to spdev -- yes these are my own virtual host names and replace them with yours) and restart apache.

Make sure xdebug is running by checking your phpinfo page and if it's listed on the page then it's enabled.

I then went to firefox extensions and searched for xdebug and find an extension which allows you to turn xdebug on and off with a click on the lower right of the firefox browser.

When you start profiling the output will be placed in the directory you listed in php.ini, above you'll see it's /Users/barnettech/kcachegrind/xdebug/

  1. Install pyprof2calltree:
    • pip install pyprof2calltree
  2. Install qcachegrind:
    • brew install qcachegrind
    • (on linux: sudo apt-get install kcachegrind)
    • brew install graphviz

 

Now you'll find a file "qcachegrind.app" has been created and you can type: open qcachegrind to start up qcachehgrind.  You can also move this file to your mac's applications folder to more easily open it in the future.

4.)  Now that qcachegrind is open click file->open and open the file you dumped to xdebug.profiler_output_dir=/Users/barnettech/kcachegrind/xdebug/  when you turned on xdebug.  You should turn off xdebug when you're done getting some profile data, it will slow down your app a bit.