PHP程序性能分析 XHProf

XHProf 用于检测每个函数的调用,运行时间等情况

安装:

wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar zxf xhprof-0.9.2.tgz
cd xhprof-0.9.2
/usr/local/php/bin/phpize
./configure -with-php-config=/usr/local/php/bin/php-config
make && make install
vi /usr/local/php/etc/php.ini 
#添加编译好的入配置
kill -SIGUSR2 `cat /usr/local/php/var/run/php-fpm.pid`
#增加线性图工具
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-0.9.2 到你的项目

使用

<?php
xhprof_enable();
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS); //不记录内置的函数
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); //记录的情况
$xhprof_on = true;

//your are code!!!


if($xhprof_on){
$xhprof_data = xhprof_disable();
$xhprof_root = dirname(__FILE__)."/";
include_once $xhprof_root."xhprof/xhprof_lib/utils/xhprof_lib.php";
include_once $xhprof_root."xhprof/xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new XHProfRuns_Default();
$key="onlykey";
$run_id = $xhprof_runs->save_run($xhprof_data, $key);
echo '<a href="/xhprof/xhprof_html/index.php?run='.$run_id.'&source='.$key.'">统计</a>';
}

据说分析省资源,但生成环境还是别用.测试环境可以加上.找到瓶颈

composer 配置

"facebook/xhprof": "dev-master"

之后 php composer.phar update 即可,更方便

原文地址:https://www.cnblogs.com/liushannet/p/2832054.html