XHProf is a function-level hierarchical profiler for PHP and has a simple HTML based user interface. This article will show you the way to install and test XHProf in Ubuntu 14.04
Install Graphviz:
1 2 |
sudo apt-get install graphviz sudo apt-get install libgv-php5 |
Install XHProf from pecl:
1 |
sudo pecl install xhprof-0.9.4 |
Adding XHProf to PHP5:
1 2 3 4 |
sudo -s echo "extension=xhprof.so" > /etc/php5/mods-available/xhprof.ini php5enmod xhprof exit; |
Test:
1 2 3 4 |
$ php -i|grep xhprof /etc/php5/cli/conf.d/20-xhprof.ini xhprof xhprof => 0.9.4 |
Install XHProf report view:
1 2 |
cd /var/www git clone https://github.com/phacility/xhprof.git |
Create a test script:
1 2 3 4 5 6 7 8 |
xhprof_enable(); // Your code here $XHPROF_ROOT = "/var/www/xhprof/"; include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php"; include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php"; $xhprof_runs = new XHProfRuns_Default(); $run_id = $xhprof_runs->save_run($xhprof_data, "foobar"); |
And view the report:
1 |
http://localhost/xhprof/xhprof_html/index.php |
In the better way, you should create header.php, and footer.php and change apache2 configuration so that you can track your request automatically
1 2 |
php_value auto_prepend_file /var/www/xhprof/header.php php_value auto_append_file /var/www/xhprof/footer.php |