手机版

使用XHProf查找服务器端编程语言(专业超文本预处理器的缩写)性能瓶颈的实例

时间:2021-09-02 来源:互联网 编辑:宝哥软件园 浏览:

XHProf是脸谱网开发的一个测试服务器端编程语言(专业超文本预处理器的缩写)性能的扩展,本文记录了在服务器端编程语言(专业超文本预处理器的缩写)应用中使用XHProf对服务器端编程语言(专业超文本预处理器的缩写)进行性能优化,查找性能瓶颈的方法。

一、安装Xhprof扩展

//github上下载https://github.com/facebook/xhprofunzip xh教授-硕士。zip CD xh prof-master/extension//usr/local/PHP/bin/phpize ./config-with-PHP-config=/usr/local/PHP/bin/PHP-config-enable-xh profimake make install二、修改php.ini

[xh prof]extension=xh prof . soxh prof . output _ dir=/tmp配置中xhprof.output_dir指定了生成的轮廓文件存储的位置,我们将其指定为/tmp。

三、将相关文件移动项目中

//xhprof下载压缩包中的xhprof_html和xh教授_ libcp-r xh教授-硕士/xh教授_ html/usr/local/nginx/html/xh教授/CP-r xh教授-硕士/xh教授_ lib/usr/local/nginx/html/xh教授/配置一个域名,浏览器可以访问到http://will.com/xhprof/xhprof_html/index.php

服务器{听80服务器名will.com;位置/{ root/usr/local/nginx/html;index.html指数;}位置~ \。php $ { root htmlfastcgi _ pass 127。0 .0 .1:9000;index.php指数;fastcgi _ param SCRIPT _ FILENAME $ document _ root $ fastcgi _ SCRIPT _ name;包括fastcgi _ params} }四、安装图形

//需要安装graphviz否则查看性能图片时候会报未能执行cmd :“点-Tpen ”.stderr: "找不到sh: dot:命令“好吃安装graphviz五、编写测试文件

//入口文件的开始位置xh prof _ enable(xh prof _ FLAGS _ MEMORY | xh prof _ FLAGS _ CPU);业务逻辑.//业务逻辑结束后$ xh教授_ data=xh教授_ disable();include _ once '/usr/local/nginx/html/xh prof/xh prof _ lib/utils/xh prof _ lib。PHP ';include _ once '/usr/local/nginx/html/xh prof/xh prof _ lib/utils/xh prof _ runs。PHP ';$ objxpurrun=new xhpuruns _ Default();//数据会保存在php.ini中xhprof.output_dir设置的目录去中$ run _ id=$ objxhpurun-save _ run($ xh prof _ data,' test ');完整代码示例(随机满减红包演示)

?phpxh prof _ enable(xh prof _ FLAGS _ MEMORY | xh prof _ FLAGS _ CPU);函数show($ info){ echo ' pre ';print _ r($ info);}//不作数据校验$rules=array(2=array('min'=1,' max'=10,' chance'=30),//金额:分概率:百分之(默认为100%,不足100%按第一档计算)数组(“最小”=11,“最大”=25,“机会”=60),数组(“最小”=26,“最大”=50,“机会”=10),数组(“最小”=50,“最大”=80,“机会”=0),数组(' min'=80,' max'=100,' chance'=0),$ total _ money=10000//红包总金额$ RES=array();而($ total _ money 0){ $ index=getLevel($ rules);$money=setMoney($rules,$ index);if ($money $total_money)//金额不足{ $ money=$ total _ money $ total _ money=0;} else { $ total _ money-=$ money;} $res[]=($index 1).' - '.$钱;} echo show($ RES);echo $total_money .br/';//1.先确定档次函数GetLevel($ rules){ $ Level=array();$ chance=0;foreach($规则为$ k=$ v){ if($ v[' chance ']0){ $ chance=$ v[' chance ']* 100;//扩大100倍$ level[$ k]=$ chance;} } $ index=0;$rand_num=mt_rand(1,10000);foreach($ level as $ k=$ v){ if($ rand _ num=$ v){ $ index=$ k;打破;} }返回$ index}//2。确定档次之后,再确定金额函数setMoney($rules,$ index){ $ money=mt _ rand($ rules[$ index][' min ']* 10000,$ rules[$ index][' max ']* 10000)/10000;$ money=ceil($ money);$ money 1 $ money=$ money-1;//防止出现免单情况返还美元;} $ xh教授_ data=xh教授_ disable();include _ once '/usr/local/nginx/html/xh prof/xh prof _ lib/utils/xh prof _ lib。PHP ';include _ once '/usr/local/nginx/html/xh prof/xh prof _ lib/utils/xh prof _ runs。PHP ';$ objxpurrun=new xhpuruns _ Default();//数据会保存在php.ini中xhprof.output_dir设置的目录去中$ run _ id=$ objxhpurun-save _ run($ xh prof _ data,' test ');回声'http://will.com/xhprof/xhprof_html/index.php?run=$ run _ id source=test ';//变量$runId是本次请求生成分析结果的id,最后我们输出了一个链接地址,使用改地址就可以看到本次请求的分析结果。六、查看分析结果

先运行业务代码;

然后,浏览器打开http://will.com/xhprof/xhprof_html/index.php,点击上次生成的xhprof文件

请注意中间的查看完整调用图链接,通过它我们可以看到图形分析结果

图中红色部分是性能低,耗时长的部分。我们可以根据哪些函数用红色标记来优化系统代码

附,xhprof报告字段含义:

函数名:方法名。

调用:方法被调用的次数。

调用百分比:方法调用占同级方法调用总数的百分比。

包含墙时间(microsec):方法执行所花费的时间,包括子方法的执行时间。(以微秒计)

IWall%:执行方法花费的时间百分比。

不包括壁时间(microsec):方法本身执行所花费的时间,不包括子方法的执行时间。(以微秒计)

EWall%:执行方法本身所花费的时间百分比。

包括CPU(微处理器):花费在方法执行上的CPU时间,包括子方法的执行时间。(以微秒计)

ICpu%:花费在方法执行上的Cpu时间百分比。

不包括CPU(微处理器):执行方法本身所花费的CPU时间,不包括子方法的执行时间。(以微秒计)

ECPU%:执行方法本身所花费的CPU时间的百分比。

Incl . MemUse(字节):方法执行占用的内存,包括子方法执行占用的内存。(单位:字节)

IMemUse%:方法执行占用的内存百分比。

Excl . MemUse(字节):方法本身占用的内存,不包括子方法执行占用的内存。(单位:字节)

EMemUse%:方法本身占用的内存百分比。

Incl.peakmemuse(字节):incl.memuse peak。(单位:字节)

Ipeakmemuse %:包括memuse峰值百分比。

不包括峰值(字节):不包括峰值。单位:(字节)

Epeakemuse%:不包括峰值百分比。

以上使用XHProf查找PHP性能瓶颈的例子,都是边肖分享给大家的内容,希望能给大家一个参考和支持。

版权声明:使用XHProf查找服务器端编程语言(专业超文本预处理器的缩写)性能瓶颈的实例是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。