五、编写测试文件
营业逻辑...
//营业逻辑竣事后
$xhprof_data = xhprof_disable();
include_once "/usr/local/nginx/html/xhprof/xhprof_lib/utils/xhprof_lib.php";
include_once "/usr/local/nginx/html/xhprof/xhprof_lib/utils/xhprof_runs.php";
$objXhprofRun = new XHProfRuns_Default();//数据会生涯在php.ini中xhprof.output_dir配置的目次去中
$run_id = $objXhprofRun->save_run($xhprof_data,"test");
完备代码示例(随机满减红包demo)
";
print_r($info);
}
//不作数据校验
$rules = array(
2=>array('min'=>1,'max'=>10,'chance'=>30),//金额:分 概率:百分之(默以为100%,不敷100%按第一档计较)
array('min'=>11,'max'=>25,'chance'=>60),array('min'=>26,'max'=>50,'chance'=>10),array('min'=>50,'max'=>80,'chance'=>0),array('min'=>80,'max'=>100,);
$total_money = 10000;//红包总金额
$res = array();
while($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)."---".$money;
}
echo show($res);
echo $total_money . "
";
//1.先确定档次
function getLevel($rules)
{
$level = array();
$chance = 0;
foreach($rules as $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;
break;
}
}
return $index;
}
//2.确定档次之后,再确定金额
function setMoney($rules,$index)
{
$money = mt_rand($rules[$index]['min']10000,$rules[$index]['max']*10000)/10000;
$money = ceil($money);
$money > 1 && $money = $money -1;//防备呈现免单环境
return $money;
}
$xhprof_data = xhprof_disable();
include_once "/usr/local/nginx/html/xhprof/xhprof_lib/utils/xhprof_lib.php";
include_once "/usr/local/nginx/html/xhprof/xhprof_lib/utils/xhprof_runs.php";
$objXhprofRun = new XHProfRuns_Default();//数据会生涯在php.ini中xhprof.output_dir配置的目次去中
$run_id = $objXhprofRun->save_run($xhprof_data,"test");
echo "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陈诉字段寄义:
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峰值百分比。
以上这篇行使XHProf查找PHP机能瓶颈的实例就是小编分享给各人的所有内容了,但愿能给各人一个参考,也但愿各人多多支持编程之家。
(编辑:湖南网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!