系统环境
Ubuntu 16.04.4 LTS
PHP 7.1.14
Apache/2.4.29
安装xhprof
#切换目录下载xhprof
cd /usr/local/src/
git clone https://github.com/longxinH/xhprof
cd xhprof/extension/
/usr/local/c/bin/phpize
./configure --with-php-config=/usr/local/c/bin/php-config
make
make install
然后在php.ini中加入xhprof.so
/var/log/xhprof 存放分析日志输出
vim /usr/local/c/lib/php.ini
#加入
extension=xhprof.so
xhprof.output_dir=/var/log/xhprof
执行/usr/local/c/bin/php -m |grep xhprof
可以看见输出,说明php扩展安装成功,然后重启apache
查看phpinfo
站点复制
mkdir -p /home/w/html/xhprof/
cd /home/w/html/xhprof/
cp -rf /usr/local/src/xhprof/xhprof_html/ ./
cp -rf /usr/local/src/xhprof/examples/ ./
cp -rf /usr/local/src/xhprof/xhprof_lib/ ./
测试
cat /home/w/html/xhprof/examples/sample.php
注意:$XHPROF_ROOT = "/home/w/html/xhprof/"; 路径
<?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 要测试的php代码
foo();
// stop profiler 停止监测
$xhprof_data = xhprof_disable();
// display raw xhprof data for the profiler run
print_r($xhprof_data);
//包含工具类,在下载的 tgz 包中可以找到
$XHPROF_ROOT = "/home/w/html/xhprof/";
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" 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";
访问地址http://192.168.15.69:60080/xhprof/examples/sample.php
打开http://192.168.15.69:60080/xhprof/xhprof_html/
清空记录 只要清空日志/var/log/xhprof
安装graphviz
apt-get install graphviz
然后可以查看View Full Callgraph
红色节点是整个php程序执行过程中的瓶颈,黄色路径为整个过程耗时最长的路径
Function Name:方法名称。
Calls:方法被调用的次数。
Calls%:方法调用次数在同级方法总数调用次数中所占的百分比。
Incl.Wall Time(microsec):方法执行花费的时间,包括子方法的执行时间。(单位:微秒)
IWall%:方法执行花费的时间百分比。
Excl. Wall Time(microsec):方法本身执行花费的时间,不包括子方法的执行时间。(单位:微秒)
EWall%:方法本身执行花费的时间百分比。
Incl. CPU(microsecs):方法执行花费的CPU时间,包括子方法的执行时间。(单位:微秒)
ICpu%:方法执行花费的CPU时间百分比。
Excl. CPU(microsec):方法本身执行花费的CPU时间,不包括子方法的执行时间。(单位:微秒)
ECPU%:方法本身执行花费的CPU时间百分比。
Incl.MemUse(bytes):方法执行占用的内存,包括子方法执行占用的内存。(单位:字节)
IMemUse%:方法执行占用的内存百分比。
Excl.MemUse(bytes):方法本身执行占用的内存,不包括子方法执行占用的内存。(单位:字节)
EMemUse%:方法本身执行占用的内存百分比。
Incl.PeakMemUse(bytes):Incl.MemUse峰值。(单位:字节)
IPeakMemUse%:Incl.MemUse峰值百分比。
Excl.PeakMemUse(bytes):Excl.MemUse峰值。单位:(字节)
EPeakMemUse%:Excl.MemUse峰值百分比。