该 xhprof 版本是从 https://github.com/longxinh/xhprof 获取
git clone https://github.com/longxinh/xhprof
安装 xhprofcd xhprof/extension/phpize./configure makemake install
然后在/etc/php.ini中根据情况加入
extension=xhprof.so
执行
php -m | grep xhprof
可以看见输出,说明php扩展安装成功,然后重启apache或者php-fpm
运行可以直接运行从github上clone下来的文件里面example目录下的那个例子
输出如下
array( [main()] => array ( [ct] => 1 [wt] => 9 ))---------------assuming you have set up the http based ui for xhprof at some address, you can view run at http://<xhprof-ui-address>/index.php?run=592567308784c&source=xhprof_foo---------------
然后复制index.php后面的?run=592567308784c&source=xhprof_foo
访问
xhprof_html/index.php?run=592567308784c&source=xhprof_foo
可看见输出
点击中间的 view full callgraph 即可看见性能分析图片
报错failed to execute cmd:" dot -tpng". stderr:sh: dot:command not found。
//解决方案yum install graphviz
随机应变比如想测试自己的项目,例如一款框架的性能分析。
复制xhprof_lib/utils/下的两个文件
xhprof_lib.php和xhprof_runs.php到入口文件同级目录,然后在入口文件起始位置添加
// start profilingxhprof_enable();
结束位置添加
// stop profiler$xhprof_data = xhprof_disable();// display raw xhprof data for the profiler runprint_r($xhprof_data);include_once "xhprof_lib.php";include_once "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";
即可得到如上所示的那个url,然后再次去访问
http://***/xhprof_html/index.php?run=*****&source=xhprof_foo
得到如下所示页面
查看图片
图中红色的部分为性能比较低,耗时比较长的部分,我们可以根据根据哪些函数被标记为红色对系统的代码进行优化
补充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峰值百分比。
推荐学习:php视频教程
以上就是php7下如何安装并使用xhprof性能分析工具的详细内容。
