PHP profiling with XHprof

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by brianmercer on July 26, 2010 at 9:58pm

If you're looking to track down the module which is killing your page load times, you may need profiling. Most people have used xdebug for this in the past with something like webgrind to display the information. Things have gotten easier.

Facebook has developed a PHP extension called XHprof and released it under the Apache license. It comes with a nice sample web interface.

While this article formerly described the use of a module specifically for xhprof, the devs of the devel module have integrated XHprof into both the versions for Drupal 6.x and 7.x and there's no longer any need to use a separate module.

The XHprof extension is available through PECL, but I made an Ubuntu package for ease of installation. So for those Ubuntu users, add my repo with

aptitude install python-software-properties
add-apt-repository ppa:brianmercer/php5-xhprof
aptitude update
aptitude install php5-xhprof graphviz

The graphviz package is required to generate the "callgraphs". Restart your php-fpm or apache or whatever you use for php.

Enable the devel module and enter the following values:

XHprof directory: /usr/share/php5-xhprof/

XHProf URL: /xhprof_html

and for nginx place the following locations at the top of your server config:
location ^~ /xhprof_html/ {
root /usr/share/php5-xhprof/;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/php5-xhprof$fastcgi_script_name;
fastcgi_pass php;

location ~ ^.*\.(css|js|gif)$ {}
}

By default, XHprof is configured to place the dump files in /tmp. Feel free to delete them after you're done. To disable XHprof you can remove the package through apt or just comment out the lines in /etc/php5/conf.d/xhprof.ini with semicolons and restart php.

Categories: , ,

Comments

If you're using Drupal 7...

Posted by brianmercer on January 17, 2011 at 7:59pm

EDIT: Now using the same configuration with D6 and D7 with devel module.

Thanks Brian, it sounds very

Posted by omega8cc on August 3, 2010 at 5:22pm

Thanks Brian, it sounds very interesting. Definitely something to be added as a must have to my performance toolbox.

Thanks for sharing this!

Posted by ximo on November 18, 2010 at 2:48am

Tried your Nginx configuration, but it would only give me 404s. Not sure why, maybe it clashed with the rest of my server configuration. Anyway, I rewrote it into a separate server definition for XHProf, and lo and behold, it worked!

upstream php {
server 127.0.0.1:9000; # Or wherever your PHP-FPM service runs
}
server {
server_name xhprof;

root /usr/share/php5-xhprof/xhprof_html;
index index.php;

location ~ .php$ {
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_pass php;
}
}

That makes the URL for viewing XHProf reports http://xhprof (you obviously need an entry for this in /etc/hosts), which you need to specify in Devel's settings without a trailing slash.

XHProf URL:
http://xhprof

I also had problems installing XHProf using Pecl in the first place, as its directory structure is incompatible with how Pecl does things. As I had already started with Pecl and didn't want to mess things up by trying Brian's (probably great) Ubuntu package (I'm on Debian Lenny), I followed the instructions by skwirblies on this bug report and completed the Pecl installation.

That left me without the /usr/share/php5-xhprof directory though, so I simply downloaded it from pecl.php.net and unpacked it to /usr/share/php5-xhprof. Not sure this is as debianized as Brian's solution, but it works for me.

Hope this can help someone out there.

thanks

Posted by candelas on March 28, 2011 at 11:53am

Update for maverick?

Posted by rfay on March 31, 2011 at 7:41pm

Could you update the PPA for Maverick? (And Natty shortly?)

I get

maybe a connection problem?

Posted by candelas on March 31, 2011 at 9:55pm

the other day i had that error when i tried to install but 15min later i could.

Doesn't work on my nginx server。

Posted by kurtzhong on July 20, 2011 at 11:19am

I am running drupal 7 on my Ubuntu Natty with Nginx, and you really help me a lot. Thanks a lot for your work.

The only problem is that after I add the Nginx configuration to my sites conf file, I couldn't restart my nginx server and it give me some error like .

Restarting nginx: nginx: [emerg] no port in upstream "php" in /etc/nginx/sites-enabled/mysite.dev:11
nginx: configuration file /etc/nginx/nginx.conf test failed

Any clue about this?

In place offastcgi_pass

Posted by brianmercer on July 20, 2011 at 1:02pm

In place of

fastcgi_pass php;

you should put whatever you use for fastcgi_pass. That might be
fastcgi_pass 127.0.0.1:9000;

or
fastcgi_pass unix:/var/run/php5-fpm.sock;

You can also define an upstream in your nginx.conf that reads like this:
upstream anyname {
server unix:/var/run/php5-fpm.sock;
# or
#server 127.0.0.1:9000;
}

}
and then use
fastcgi_pass anyname;

which can make it easier to make global changes when you need to.

Thank you. I've changed my

Posted by kurtzhong on August 1, 2011 at 2:28am

Thank you. I've changed my conf to following, and it works for me.

location ~ ^/xhprof_html/ {
root /usr/share/php5-xhprof/;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /usr/share/php5-xhprof/$fastcgi_script_name;
}

But I don't know why it works...

couldn't find add-apt-repository

Posted by cdoyle on November 4, 2011 at 10:28pm

I didn't have add-apt-repository on my Ubuntu 10.04 box so I had to run the following to get it to work

sudo apt-get install --reinstall python-software-properties && sudo dpkg-reconfigure python-software-properties

Fyi, I added this to my apache2 config inside the virtualhost directive:

Alias /xhprof_html /usr/share/php5-xhprof/xhprof_html/
<Directory "/usr/share/php5-xhprof/xhprof_html/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>

cdoyle, this is the nginx

Posted by Fidelix on November 4, 2011 at 11:03pm

cdoyle, this is the nginx group. The probability that your comment will be useful to someone is quite low.

Thanks for having the effort, though...

That's good info to have.

Posted by brianmercer on November 5, 2011 at 12:46am

That's good info to have. This page is number two on google rankings for "drupal xhprof" and not everyone has seen the advantages of nginx quite yet.

xhprof in windows

Posted by Bhanuji on November 24, 2011 at 3:57pm

hey please help me out ... here every settings u people told which can be worked in Ubuntu ....
I m using Windows,, with apache server..

can u please tell how to install this xhprof in D6 and D7.

Sorry, I have no experience

Posted by brianmercer on November 24, 2011 at 6:42pm

Sorry, I have no experience setting up xhprof or any php extension on windows.

No windows support

Posted by harry slaughter on August 26, 2012 at 4:31pm

There is no port of xhprof for windows.

You can still use xdebug though and get a lot of the same information.

--
Drupal tips, tricks and services
http://devbee.net/ - Effective Drupal

Does it work under Virtual hosts?

Posted by voipfc on November 27, 2011 at 11:03pm

Does it work under Virtual hosts?

Does it have to be configured separately for each Virtual hosts configuration?

It depends on your setup, but

Posted by brianmercer on November 28, 2011 at 6:58pm

It depends on your setup, but typically the xhprof config will appear inside each server{} definition.

What I do is put the xhprof location in another file and then put an "include /etc/nginx/includes/xhprof" in my server definition when I want it

Getting Fatal error: Class

Posted by ckng on January 9, 2012 at 2:31pm

Getting

Fatal error: Class 'XHProfRuns_Default' not found in /var/www/d7/sites/all/modules/contrib/devel/devel.module on line 1023"

paths and nginx configuration seems correct, what might be the problem?

Found something similar reported here http://drupal.org/node/1204478

CK Ng | myFineJob.com

It's working for me with

Posted by brianmercer on January 9, 2012 at 3:37pm

It's working for me with current versions.

  1. is xhprof installed? does this file exist: /usr/share/php5-xhprof/xhprof_lib/utils/xhprof_runs.php
  2. is xhprof activated? go to /admin/reports/status/php and look for the xhprof entry
  3. did you restart php after installing the xhprof package?

Yes to all 3. xhprof

Posted by ckng on January 9, 2012 at 4:48pm

Yes to all 3.

xhprof 0.9.2
PHP 5.3.2 with php5-fpm
nginx/1.1.11

CK Ng | myFineJob.com

Sorry, I can't reproduce

Posted by brianmercer on January 9, 2012 at 5:33pm

Sorry, I can't reproduce this.

Check to make sure that /usr/share/php5-xhprof/xhprof_lib/utils/xhprof_runs.php and the folders are readable by all, or at least the user that runs your web daemon. Unless you're using some other advanced permissioning system.

Try a location of "/usr/share/php5-xhprof" without the trailing slash.

Disable xhprof and uninstall and then make sure the settings are gone using "drush @yoursite vget devel_" and delete the xhprof ones (or all) using "drush @yoursite vdel devel_" and then install and double check the path again.

Perhaps try the dev version of devel, although I just checked both and both work for me.

Almost out of ideas.

I'm following instruction

Posted by ckng on January 9, 2012 at 6:38pm

I'm following instruction outlined here: http://groups.drupal.org/node/82889
The /usr/share/php5-xhprof/xhprof_lib/utils/xhprof_runs.php is readable by all. Get it to load after chown them to same as nginx user (www-data), that's odd.

My page is having &amp; in the title, now the xhprof can't load the file
"Could not find file /tmp/4f0b313b205d0.Training ..."
where to file a bug report for xhprof?

I can temporarily change the title to solve it.
Thanks for the clue on file access, but it should be readable, tested with other readable files not owned by www-data they are working.

CK Ng | myFineJob.com

Odd indeed. Maybe check the

Posted by brianmercer on January 9, 2012 at 7:42pm

Odd indeed. Maybe check the xhprof files for selinux context with "ls -Z".

The new problem might have to do with spaces or international characters in the site name? Check out this patch. http://drupal.org/node/1345240

I doubt there's any point in filing a bug against xhprof. It hasn't seen any development in years except a small patch to make it compatible with php 5.4.

selinux is not

Posted by ckng on January 10, 2012 at 2:53pm

selinux is not installed.
Will do more tests and maybe reinstall when I got the time.

Thanks.

CK Ng | myFineJob.com

add /tmp to xhprof.ini

Posted by socialnicheguru on August 17, 2012 at 5:26pm

I had to make sure that I added "xhprof.output_dir=/tmp"

==> /etc/php5/conf.d/xhprof.ini <==
extension=xhprof.so
xhprof.output_dir=/tmp

I just wanted to try this out

Posted by kaido.toomingas on October 1, 2012 at 9:51pm

I just wanted to try this out and used http://drupal.org/project/XHProf becouse I couldnt get this installed :) You can use this even with shared hosting! :) good luck!

Getting Reports for Drush but not Drupal calls

Posted by nooysters on November 15, 2012 at 11:12pm

Check permissions on your output directory.
/var/tmp/xhprof
or whatever yours is.

Conf file

Posted by jmoughon on April 5, 2013 at 6:40pm

I had to add a Conf file to /etc/apache2/conf.d:
Create/etc/apache2/conf.d/xhprof.conf with:
alias /xhprof_html "/usr/share/php5-xhprof/xhprof_html/"

Nginx

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /