XHProf是Facebook开发的性能调试工具,帮助我们的PHP程序性能调优,更加健壮。XHProf安装和使用方法将在本章讲解。XHProf是PHP的PECL扩展。没有XDeBug那些耗费资源,更加的小巧。
流程:程序开头打点,结尾打点。那么XHProf机会记录在两个点之间的所有代码响应时所耗费的时间、内存、CPU等各项指标,我们也可以知道一次请求调用了多少次MySQL,多少次Memcache,更加直观的指明优化道路。
安装:
<precourier new white-space:pre-wrap padding:9.5px margin-top:0px margin-bottom:10px line-height:1.42857 word-break:break-all word-wrap:break-word border:1px solid rgb background-color:rgb>------------下载并编译PHP-XHProf源码------------
wget http://pecl.php.net/get/xhprof-0.9.4.tgz
tar -zxvf xhprof-0.9.4.tgz
cd xhprof-0.9.4
cd extension
phpize
./configure --enable-xhprof
make
make test
sudo make install
------------修改php.ini---------------
sudo vim /etc/php.ini
#在php.ini最下方加入以下:
extension=xhprof.so
xhprof.output_dir="/var/www/xhprof"
-----------重启Apache--------------
sudo apache restart
Enter the installation package folder you just decompressed, and copy xhprof_lib and xhprof_html to the project directory. Next, create a header file head.php, which is the beginning of the two dots: <precourier new white-space:pre-wrap padding:9.5px margin-top:0px margin-bottom:10px line-height:1.42857 word-break:break-all word-wrap:break-word border:1px solid rgb background-color:rgb>//head.php
<?php
if(extension_loaded('xhprof')){
//载入下载的XHPROF包中的2个文件夹
include_once 'xhprof_lib/utils/xhprof_lib.php';
include_once 'xhprof_lib/utils/xhprof_runs.php';
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}
Copy after login
. Then create a bottom file foot.php, which is the last of the two dots:
//foot.php
save_run($xhprofData, $ns);
//前端展示库的URL
$url = 'http://localhost/xhprof_html/index.php';
$url .= '?run=%s&source=%s';
//变量替换
$url = sprintf($url, $runId, $ns);
//输入URL
echo '查看结果';
}
. The last step: dotting. Now we create a test file index.php. Test my big Hello World. //index.php
auto_prepend_file = /var/www/head.php
auto_append_file = /var/www/foot.php
in php.ini or add php_value auto_prepend_file = /var/www/head.php
php_value auto_append_file = /var/www/foot.php
in .htaccess. Error: 1. When clicking [View Full Callgraph] to view the picture, an error is reported: failed to execute cmd: "dot -Tpng". stderr: `sh: dot: command not found`. Reason: Reason: The graphical tool is not installed Solution: //红帽系列
yum install graphviz
//Ununtu
apt-get install graphviz
//OS X
brew install graphviz
The above introduces the installation and use of XHProf (PHP performance testing artifact), including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.