Installing XHProf and Profiling with XHGui

Installing XHPROF

apt-get install php5-common

XHProf can be installed in different ways. Below are two ways and second option is more likely to work.

Try the following (Didn’t work for me):

pecl config-set preferred_state beta

pecl install xhprof

If the above doesn’t work, try the following.

wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar xvf xhprof-0.9.2.tgz
cd ./xhprof-0.9.2/extension/
phpize
./configure --with-php-config=/usr/local/bin/php-config
make
make install
make test

I’ve put all the files under /usr/share/php. But not necessarily should be there.

Once you have XHProf installed, you should enable it. Open your php.ini and add

[xhprof]
extension=xhprof.so
xhprof.output_dir="/dir/where/your/output/will/be"

My Output_dir is /tmp/xhprof, which is the directory that will collect the profile data for each run.

Restart apache, and the XHProf extension should be enabled (check with “php -m” that this is the case).

Setting Up XHGui

XHProf by default has very basic User Interface which is hard to use and filter the data you want to filter. XHGui comes in handy for the same reason.

Lets take a bit of pain (or fun if you like) for the win.

  1. Git Clone : [ git clone https://github.com/preinheimer/xhprof.git XHGui ]  in a folder that needs to be readable from web server. I’ve put them in /var/www/tools/XHGui.
  2. Two files header and footer needs to be appended. You can use apache’s autoloader functionality.I’ve added following lines in my apache configuration for the site.
    php_value auto_prepend_file “/var/www/tools/XHGui/external/header.php” 

    php_value auto_append_file “/var/www/tools/XHGui/external/footer.php”

  3. Now, Create a database. You may be thinking what ? But yes. It supports MySql, SQLite, and SQLServer (beta)
    I’ve chosen to create MySql Database called, xhprof with username ******, password ******
  4. Now Create a table in the database you’ve just created using the command  you can get on xhprof_runs_mysql.php . In case of my example it’s located at /var/www/tools/XHGui/xhprof_lib/utils/xhprof_runs_mysql.php
    Or Just copy from here
    CREATE TABLE `details` ( 

    `id` char(17) NOT NULL,

    `url` varchar(255) default NULL,

    `c_url` varchar(255) default NULL,

    `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

    `server name` varchar(64) default NULL,

    `perfdata` MEDIUMBLOB,

    `type` tinyint(4) default NULL,

    `cookie` BLOB,

    `post` BLOB,

    `get` BLOB,

    `pmu` int(11) unsigned default NULL,

    `wt` int(11) unsigned default NULL,

    `cpu` int(11) unsigned default NULL,

    `server_id` char(3) NOT NULL default ‘t11′,

    `aggregateCalls_include` varchar(255) DEFAULT NULL,

    PRIMARY KEY  (`id`),

    KEY `url` (`url`),

    KEY `c_url` (`c_url`),

    KEY `cpu` (`cpu`),

    KEY `wt` (`wt`),

    KEY `pmu` (`pmu`),

    KEY `timestamp` (`timestamp`)

    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

  5. Change Directory to XHGui/xhprof_lib (For me it’s /var/www/tools/XHGui/xhprof_lib)
  6. Symlink or Copy config.sample.php to config.php
  7. Now Edit config.php with your favourite editor. For me it’s vim config.php
  8. Change at least following options:
    $_xhprof['dbtype'] = ‘mysql’; // Only relevant for PDO
    $_xhprof['dbhost'] = ‘localhost’; //or any host
    $_xhprof['dbuser'] = ‘******’;
    $_xhprof['dbpass'] = ‘******’;
    $_xhprof['dbname'] = ‘xhprof’;
    $_xhprof['servername'] = ‘localhost’;
    $_xhprof['namespace'] = ”;
    $_xhprof['url'] = ‘http://xhprof.local‘;
    Note: $_xhprof['url'] must be accessible via browser.
  9. If you like to see what other options are please look at this URL: http://phpadvent.org/2010/profiling-with-xhgui-by-paul-reinheimer
  10. Change directory to XHGui/xhprof_lib/utils. In case of my example it’s /var/www/tools/XHGui/xhprof_lib/utils
  11. Copy or Symlink xhprof_runs_mysql.php to xhprof_runs.php
  12. Now set up a new website as per the URL (xhprof.local in the case of example) So that you can see the Profiling data on browser.
    Point the site to XHGui/xhprof_html folder. In case of this example, it is : /var/www/tools/XHGui/xhprof_html
    Don’t forget to change your host to point your ip to new url.
  13. That is it.
    Whenever you access a website add ?_profile=1 at the end
    eg: http://localhost/?_profile=1
    Now, you can see a link at the end of page.
    Click the link to find out how crappy code you have written. (wink)Thanks for reading

Resources:


http://techportal.ibuildings.com/2009/12/01/profiling-with-xhprof/

https://github.com/preinheimer/xhprof

http://phpadvent.org/2010/profiling-with-xhgui-by-paul-reinheimer

http://pecl.php.net/package/xhprof

http://uk.php.net/xhprof

This entry was posted in Profiling. Bookmark the permalink.

Comments are closed.