Home | Mirror | Search

18. XHProf

XHProf是facebook開源出來的一個php輕量級的性能分析工具,跟Xdebug類似,但性能開銷更低,還可以用在生產環境中,也可以由程序開 關來控制是否進行profile。

https://github.com/facebook/xhprof

安裝依賴工具

		
$ sudo apt-get install graphviz

or

wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz
tar zxf graphviz-2.24.0.tar.gz
cd graphviz-2.24.0
./configure
make && make install
		
		

安裝xhprof

		
pecl install xhprof
mv /srv/php-5.3.8/lib/php/extensions/no-debug-non-zts-20090626/xhprof.so /srv/php-5.3.8/lib/php/extensions/

or

wget http://pecl.php.net/get/xhprof

tar zxf xhprof-0.9.2.tgz
cd xhprof-0.9.2
cp -r xhprof_html xhprof_lib /www/www.example.com/xhprof/
cd extension/
/usr/local/webserver/php/bin/phpize
./configure  –with-php-config=/srv/php/bin/php-config
make && make install
		
		

安裝完提示:

Installing shared extensions:     /srv/php/lib/php/extensions/no-debug-non-zts-20060613/

cp /srv/php/lib/php/extensions/no-debug-non-zts-20060613/* /srv/php/lib/php/extensions/
		

增加xhprof.ini

		
cat >> /srv/php/etc/conf.d/xhprof.ini <<EOF
extension=xhprof.so
xhprof.output_dir=/www/logs/xhprof
EOF
		
		

分析日誌輸出在/www/logs/xhprof目錄

檢查安裝是否成功

# /srv/php/bin/php -m | grep xhprof
xhprof
		

例 19.7. xhprof/sample.php

			
<?php

function bar($x) {
  if ($x > 0) {
    bar($x - 1);
  }
}

function foo() {
  for ($idx = 0; $idx < 5; $idx++) {
    bar($idx);
    $x = strlen("abc");
  }
}

// start profiling
xhprof_enable();

// run program
foo();

// stop profiler
$xhprof_data = xhprof_disable();

// display raw xhprof data for the profiler run
print_r($xhprof_data);


$XHPROF_ROOT = realpath(dirname(__FILE__) .'/..');
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";

// save raw data for this profiler run using default
// implementation of iXHProfRuns.
$xhprof_runs = new XHProfRuns_Default();

// save the run under a namespace "xhprof_foo"
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");

echo "---------------\n".
     "Assuming you have set up the http based UI for \n".
     "XHProf at some address, you can view run at \n".
     "http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".
     "---------------\n";
			
			

comments powered by Disqus