Profiling Your PHP Code
Matt Dunn, Developer 9 Dec 2008
PHP can be great for rapid development of robust, enterprise solutions. However, it’s very easy to overlook relatively easy tweaks that can be made to your code that can make massive improvements to performance. To do this you need to be able to identify bottlenecks in your application. There are many tools that can assist you with this including Advanced PHP Debugger, Benchmark and Xdebug. In this article I am going to look at Xdebug.
Xdebug is a PHP extension that provides many additional useful features such as providing stack traces, memory allocation, code coverage and interactive code debugging.
Installation
Once you have the extension binary (you can download Linux and Windows binaries or you can build it yourself), installation is as simple as adding the following to your php.ini:
zend_extension_ts="C:/php/ext/php_xdebug-2.0.3-5.2.5.dll"
Configuration
The simplest configuration is to turn the profiler on and to define the output path in your php.ini file:
[xdebug] xdebug.profiler_enable = 1 xdebug.profiler_output_dir = c:/temp
Profiling
It’s time to start some profiling. Now that the xdebug profiler is turned on, whenever you execute any part of your application the profile information is saved into the xdebug.profiler_output_dir.
The profile information is stored in cachegrind format that can be analyses in any application that can understand this format. For example:
- KCacheGrind (Linux, KDE)
- MacCallGrind (Mac OSX)
- WinCacheGrind (Windows)
Once the profile output file is opened in a compatible application, there is lots of useful information at your disposal. For example, a breakdown of every call is detailed with the cumulative time as well as a full stack trace.
Now you have a wealth of information at your fingertips you can start to look at optimizing your code in the areas that are causing the biggest performance problems. It’s beyond the scope of this article to go into any detail about code optimization, but I will say it’s important that you test your application in an environment as real as possible to the live environment as there are many factors that can affect performance, such as server load, memory, and web server configuration.





Comments
Be the first to comment.
Add your comment