XHProf是Facebook开发的性能调试工具,帮助我们的PHP程序性能调优,更加健壮。XHProf安装和使用方法将在本章讲解。XHProf是PHP的PECL扩展。没有XDeBug那些耗费资源,更加的小巧。
流程:程序开头打点,结尾打点。那么XHProf机会记录在两个点之间的所有代码响应时所耗费的时间、内存、CPU等各项指标,我们也可以知道一次请求调用了多少次MySQL,多少次Memcache,更加直观的指明优化道路。
安装:
<pre class="brush:php;toolbar:false;"Courier New',monospace; 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(204,204,204); background-color:rgb(245,245,245)">------------下载并编译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
//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);
}
//foot.php
<?php
if(extension_loaded('xhprof')){
$ns = 'myXhprof';
//关闭profiler
$xhprofData = xhprof_disable();
//实例化类
$xhprofRuns = new XHProfRuns_Default();
$runId = $xhprofRuns->save_run($xhprofData, $ns);
//前端展示库的URL
$url = 'http://localhost/xhprof_html/index.php';
$url .= '?run=%s&source=%s';
//变量替换
$url = sprintf($url, $runId, $ns);
//输入URL
echo '<a href="'.$url.'" target="_blank">查看结果</a>';
}
//index.php <?php include_once 'head.php'; echo 'Hello World'; include_once 'foot.php';
auto_prepend_file = /var/www/head.php auto_append_file = /var/www/foot.php
php_value auto_prepend_file = /var/www/head.php php_value auto_append_file = /var/www/foot.php
//红帽系列 yum install graphviz //Ununtu apt-get install graphviz //OS X brew install graphviz
以上就介绍了XHProf的安装和使用(PHP性能测试神器),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号