Install Webgrind to profile Drupal and other PHP applications on OS X

Screenshot of Webgrind

A coworker recently clued me in on Webgrind, a platform independent, browser-based PHP profiling application. I was impressed with it, and thought I'd contribute to the installation and usage instructions that are floating around the web with my specific setup.

Assumes you're using MAMP on OS X, but you can probably adapt.

  1. Install XDebug: I like the instructions at since they come with info on how to use XDebug for, well, debugging. (Note my comment on that post).
  2. Install Webgrind: Download, unpack and copy the webgrind directory to your web root.
  3. Add the following lines to the end of your php.ini (/Applications/MAMP/conf/php5/php.ini if you use PHP 5 in MAMP):
    xdebug.profiler_enable = 1
    xdebug.profiler_output_dir = /path/to/htdocs/webgrind/tmp
    xdebug.profiler_output_name = cachegrind.out.%t.%p

    Be sure to edit the path to match the location of your web root. Additionally, create the tmp directory under your webgrind directory.
  4. Edit Webgrind's config.php to use your /path/to/htdocs/webgrind/tmp:
    static $storageDir = '/path/to/htdocs/webgrind/tmp';
    static $profilerDir = '/path/to/htdocs/webgrind/tmp';
  5. Restart Apache for your php.ini settings to take effect.

(For another take on installation, see

Once it's up and running, make some localhost requests. Then visit http://localhost/webgrind (or wherever you've installed Webgrind).

Select the profile file you'd like to use.
Webgrind's select file UI

If you're using a CMS such as Drupal, I recommend setting the "Show" percentage to 100% (the default is 90%), as it's likely the code you're wanting to profile is your own, which tends to drop off into the last 10% when you're dealing with CMS profiling.

When the profiler finishes processing the profile data you selected, you'll get a report similar to that depicted below.
Detail view of Webgrind report

Worth noting is the blue, lavender (here it's just a sliver), green and orange bar. These colors represent the distribution of built-in PHP functions (blue), include/require functions (lavender), class methods (green) and procedural functions (orange). Down the left side of the list of functions profiled, the color of the dots maps to these categories.

Now, click the arrow next to a function and you'll get a listing of function invocations, where each call occurred and their costs. This listing corresponds to the invocation count column.

Note also the Total Self Cost and Total Inclusive Cost columns. The former indicates the processing time for only the code in the function, while the latter also includes processing for calls to other functions made from that code.

The paragraph icons service as a nice added feature. Clicking them opens a new tab or browser window with the contents of the file that function is defined in.

When you're done using Webgrind, either update your php.ini to set

xdebug.profiler_enable = 0

Or if you'd rather, leave the profiler enabled. It'll only create profile files for localhost requests. These can eat up drive space and slow Webgrind though, so I prefer to turn it off when not I'm not using Webgrind.

In summary, there are other, heavier-weight, and more fully featured web profiling applications out there (such as kcachegrind) but for ease of use, platform independence, and a succinct but effective feature set, Webgrind is a great addition to any PHP dev's toolbox.

Find Me Around

User login