Profiling PHP with Xdebug and Webgrind

Standard

Using Webgrind and Xdebug, you to tack on ?XDEBUG_PROFILE=true to any URL and view profiling information for that particular URL instantly.

One of the main criticisms of profiling PHP applications has been how difficult it is to manage different kcachegrind or wincachegrind windows — assuming you’re a pro at pointing them to your Xdebug output directory and all that good stuff. I am excited about how easy webgrind makes things because easier profiling will help prevent a lot of stupid performance mistakes (for those of us not using the Zend IDE and its sexy profiler, which is a lot of people).

Webgrind screenshot.

This is really quite simple to set up, and is best used on a dev box behind a firewall with port 80 closed. People can file surf your web server if you leave webgrind on an open port, don’t do it.

So, you’ve read the last paragraph, right? Ok, good. Let’s go.

Use pecl to install the json and xdebug packages

pecl install json
pecl install xdebug

You’ll run into a possible missing phpize issue, in which case you’d need the php-devel package for building PHP extensions.

Configure Xdebug

A simple configuration to get you what you need is below. Read the Xdebug docs if you want to get crazy.

; Enable xdebug extension module
zend_extension=xdebug.so

; Turns it off by default
xdebug.profiler_enable=0 

; Turns xdebug on when ?XDEBUG_PROFILE=true is in GET or POST
xdebug.profiler_enable_trigger=1

; Your output directory - you'll eventually point webgrind at this
xdebug.profiler_output_dir=/tmp/xdebug

If you’re not on PHP 5.2.x, you’ll also need the json extension.

; Enable json extension module
extension=json.so

Restart Apache.

Download and install webgrind

Webgrind is easy to setup, download it and follow the instructions. The main thing you’ll want to do is make sure your Xdebug directories are the same. In this case, it’s /tmp/xdebug

Load any PHP app with ?XDEBUG_PROFILE=true

Now you’ll want to hit your web server with the appropriate GET argument set up. So, you could hit localhost/helloworld?XDEBUG_PROFILE=true and it’d create a new cachgrind.out for that request.

Open up a tab with webgrind in it and enjoy

Webgrind will do a find on your Xdebug output directory and have a list of all your cachegrind.out files up on the top right. Now all you have to do is choose one. Webgrind’s use of jQuery and AJAX makes the app a great example of what you can do with JavaScript and a little motivation. Check it out.

Update: You should use “zend_extension” in your .ini file, not extension. I had a typo above, but it’s corrected.

6 thoughts on “Profiling PHP with Xdebug and Webgrind

  1. Sweet! I hadn’t heard of webgrind, I’d been using the old windows grind app which was a bit of a pain. Thanks for the post.

  2. ant

    Wow, sounds like a lot of fun. Well, compared to flipping back and forth to kcachegrind anyway. I’ve lost count of how many times that thing’s crashed on me because I asked it to F5…

  3. Richard Lynch

    WOW!

    I finally have a working profiler for PHP that doesn’t require a CS degree to install and use!

    I’ve used KCacheGrind, but am stuck on Windows desktop at work, and never got anything useful on Doze.

    ‘Til now.

    This just totally rocks.

    Right in time for launch, too, so I can focus on the performance issues sensibly.

  4. Pingback: Antelope Love Fan

Comments are closed.