<< Spring MVC: Null or an Empty String? | Home | From Layers to Bus >>

Profiling PHP Applications

Using the Xdebug Extension for PHP

Ever wondered where your PHP applications spend their time?

For simple PHP scripts you can easily do basic profiling by modifying your script, take the time at critical points and log the data to a file.
As applications become more complex or make use of third party frameworks like the Zend Framework this approach does no longer work well without introducing a lot of tracing code.

We wanted to gain performance insights of a newly developed application based on Zend Framework to spot areas worth receiving some fine tuning. We did not want to modify the application for that purpose so I had a look at the options available for PHP. I finally gave Xdebug a try. Xdebug is a debugger and profiler for PHP that works as an extension that you register in your php.ini:

zend_extension="/usr/local/lib/php5/modules/xdebug.so"
xdebug.profiler_enable = 1

Restart Apache and access a few pages of your application. Look at the /tmp directory on your webserver and watch for cachegrind.out files. These files contain the profiling information that we are interested in. Copy the files to your workstation for further inspection.

To analyze the profiling data you can use KCachegrind that allows you to identify the performance hogs visually:

or inspect the trace as a list:

The combination of Xdebug and KCachegrind allows you to collect and analyze profiling information from your PHP applications while they are running in their real environment without changing a single line of code.

References



Re: Profiling PHP Applications

In case you are using Windows, you can use WinCacheGrind for visualizing the profile data.

Add a comment Send a TrackBack