Profiling PHP with Xdebug and Webgrind

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.

Webgrind Interface


Before using webgrind, you must first install Xdebug.

Installing webgrind is pretty simple:

  1. Download webgrind
  2. Unzip webgrind in your web server’s document root
  3. Configure Xdebug and webgrind
  4. 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 1 in 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 XDEBUG_PROFILE, e.g., http://localhost/script.php?XDEBUG_PROFILE. You must not enable xdebug.profiler_enable if 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 cachegrind.out.%p, where %p will 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_name using a format that will ensure that each successive page load uses the same profiler output name, e.g., cachegrind.out.%s.

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!

Chris Abernethy
PHP Wrangler, MySQL DBA, Linux SysAdmin and all around computer guy, developing LAMP applications since Slackware came on 10 floppy disks.

4 Comments on "Profiling PHP with Xdebug and Webgrind"

  1. RobertoniBuh says:

    Просмотров: 594 Опубликовано: 6 июля 2015 Гороскоп на 6 июля 2015 года для знака Водолей: комментарии Поделитесь своим мнением о гороскопе на 6 июля 2015 года для знака зодиака Водолей, для этого просто оставьте комментарий!
    Вас протолкнёт сквозь стену.
    И, следовательно, каких-либо больших покупок совершенно не предвидится.
    Но универсальная сила сказки воплощена в фильме Александра Птушко с подлинной экспрессией.
    Весы, склонные к рефлексии и нерешительности, найдут в нем много полезного: ответы на все вопросы.


  2. CSGO says:

    Good Web-site, Continue the useful job. Thanks!.


Trackbacks for this post

  1. Time

Got something to say? Go for it!