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.
- 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.
- 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”
- 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 ******
- 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,
`type` tinyint(4) default NULL,
`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;
- Change Directory to XHGui/xhprof_lib (For me it’s /var/www/tools/XHGui/xhprof_lib)
- Symlink or Copy
- Now Edit config.php with your favourite editor. For me it’s vim config.php
- 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.
- If you like to see what other options are please look at this URL: http://phpadvent.org/2010/profiling-with-xhgui-by-paul-reinheimer
- Change directory to XHGui/xhprof_lib/utils. In case of my example it’s /var/www/tools/XHGui/xhprof_lib/utils
- Copy or Symlink xhprof_runs_mysql.php to xhprof_runs.php
- 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.
- That is it.
Whenever you access a website add ?_profile=1 at the end
Now, you can see a link at the end of page.
Click the link to find out how crappy code you have written. Thanks for reading