Thursday, April 22, 2010

Profiling PHP Applications on Apache Using Xdebug and Xdebugtoolkit

All written applications have certain areas that become bottlenecks or cause performance issues. Usually, it is identified by running a series of tests i.e benchmark tests, and/or load tests. With these methods, it is very easy to pinpoint the file, class or object that is causing the issue but it is up to the developer to manually figure out what's causing the problem.

The Need:
We needed to find a way to figure out which areas of a very complex PHP application is causing performance problems, FAST.
The Solution:
Use of Xdebug and Xdebugtoolkit to produce easily readable graphs.

Xdebug produces a tree of profile data containing language constructs and functions together with how much it took them to process as a whole and individually. This is referred to as the "Cachegrind" files.

While Xdebug already provides insight numerically, Xdebugtoolkit on the other hand provides ease of analysis by producing comprehensive and simple-to-interpret graphs on "dot" format using GraphViz.

Marrying these tools together provides you with the power to exponentially increase how much you can analyze.

See below on how we did it.

How To:

We will be using RHEL / CentOS as operating system for this tutorial. For more information about installing Xdebug, get it here.

1. Assuming that you have PEAR/PECL installed, get Xdebug by,

# pecl install xdebug

The command above will install the module to your php modules directory which is usually located at /usr/lib64/php/modules (part of distribution) or /usr/local/lib/php/modules (user installed).

2. Activate the xdebug module by adding the following entry to php.ini.

zend_extension = /path/to/ or zend_extension =

Note: xdebug is a zend extension, DO NOT use "". This will fail.

You can place the entry above to php.ini by doing any of the following:

# vi /etc/php.ini, copy and paste into a line within the config, then save (ESC + colon + wq)
Or, if your php configuration was installed using dependency structure (/etc/php.d), you can simply do the following,

# cd /etc/php.d
# vi xdebug.ini, copy and paste into a line within the config, then save (ESC + colon + wq)

To configure, you can use the sample below, and paste it after the "zend_extension" module entry.

Xdebug Automatic Mode:

xdebug.profiler_enable = on
xdebug.profiler_output_dir = /tmp
xdebug.profiler_output_name => cachegrind.out.%t-%R

Xdebug Trigger Mode:

xdebug.profiler_enable = off
xdebug.profiler_enable_trigger = on
xdebug.profiler_output_dir = /tmp
xdebug.profiler_output_name => cachegrind.out.%t-%R

3. Restart apache for changes to take effect.

# /etc/init.d/httpd restart
  • Install Xdebugtoolkit

1. Assuming that you have subversion installed, get xdebugtoolkit

# svn co http
:// xdebugtoolkit

2. Make sure that you have installed all dependencies. For more information, go to xdebugtoolkit.

# yum install python
# yum install pygtk2
# yum install graphviz , this will install "dot" on /usr/bin/dot

  • Generate Cachegrind or Application Profile Data
For Xdebug Automatic Mode, just visit the URLs that you want profiled. In our case, we used QAs regression test to fully profile the application.

For Xdebug Trigger Mode, just append XDEBUG_PROFILE=1 after each URL.

  • Generate Graphs
    # cd /tmp
    # /path/to/xdebugtoolkit/ yourcachegrindfile | /path/to/dot -T png -o /path/to/file.png
  • Related Links Widget for Blogspot

No comments: