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";
5.20.1. XHGui
https://github.com/preinheimer/xhgui
原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。