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
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
pecl 安裝
# pecl install xhprof
指定版本
# pecl install xhprof-0.9.4
安裝完提示:
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
例 5.8. 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";