Finding and fixing performance bottlenecks in PHP web applications can be both time-consuming and difficult. Fortunately, free tools like Xdebug and Webgrind allow you to easily find and visualize bottlenecks in your PHP scripts.
Webgrind is an Xdebug profiling web frontend in PHP5. It implements a subset of the features of kcachegrind and installs in seconds and works on all platforms.–Webgrind
Anyone who has ever needed to profile code is probably familiar with KcacheGrind (a profile data visualization tool). KcacheGrind provides a way to visualize the profile data from a program execution, allowing you to quickly and easily see where the program is spending its time. Knowing where the execution bottlenecks are allows you to focus your optimization efforts in the correct places.
For a long time, the Xdebug extension for PHP has been able to (among other things) dump execution profile information to disk in a format that is usable by KcacheGrind, which is great if you are running Linux with KDE.
The webgrind project provides a web-based replacement for KcacheGrind that can be installed on any operating system, allowing you to visualize a portion of the Xdebug profiling data through a browser.
Before using webgrind, you must first install Xdebug.
Installing webgrind is pretty simple:
- Download webgrind
- Unzip webgrind in your web server’s document root
- Configure Xdebug and webgrind
- Point your browser to where you installed webgrind
Unfortunately, webgrind does not often work “out-of-the-box”… There are a few configuration details that need to be dealt with in order to start profiling your code. The following PHP configuration directives can be used to control the behavior of Xcebug:
- This directive actually turns on Xdebug’s internal profiler. You must set this to
1in order to have Xdebug actually produce output for webgrind to analyze.
If you enable this option, profiles will only be generated if you pass a GET/POST parameter or cookie with the name
http://localhost/script.php?XDEBUG_PROFILE. You must not enable
xdebug.profiler_enableif you use this option.
- This directive tells Xdebug where to put the profile data. By default, it is set to ‘/tmp’, which is usually fine. If you want, you can probably omit this from your Xdebug configuration.
- This value tells Xdebug how to create filenames for the data it writes to disk. By default, the value is set to
%pwill be replaced with the process ID. This is bad, and you will definitely need to change it. The problem is that Apache processes serve multiple requests, so each request in a given process will overwrite the profiling data from previous requests. Other possible format specifiers can be used.
Enabling this option will cause xdebug to append data to the output file if it already exists (the default behavior is to overwrite the output file). This is a handy option if you need to average out the profiling data over multiple script executions. In that case, be sure to set
xdebug.profiler_output_nameusing a format that will ensure that each successive page load uses the same profiler output name, e.g.,
Assuming you have already loaded the Xdebug extension, the following additional PHP configuration directives should be a good starting point for you:
xdebug.profiler_enable = 1 xdebug.profiler_output_name = cachegrind.out.%t.%p
That’s it, you should be ready to profile your PHP code with Xdebug and Cachegrind!