首頁  >  文章  >  後端開發  >  使用XHProf找出PHP效能瓶頸的實例講解

使用XHProf找出PHP效能瓶頸的實例講解

jacklove
jacklove原創
2018-07-05 18:01:072064瀏覽

下面小編就為大家分享一篇使用XHProf查找PHP效能瓶頸的實例,具有很好的參考價值,希望對大家有幫助。一起跟著小編過來看看吧

XHProf是facebook 開發的一個測試php效能的擴展,本文記錄了在PHP應用中使用XHProf對PHP進行效能最佳化,尋找效能瓶頸的方法。

一、安裝Xhprof擴充

#
//github上下载https://github.com/facebook/xhprof
unzip xhprof-master.zip 
cd xhprof-master/extension/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-xhprof
make && make install

##二、修改php.ini

[xhprof]
extension=xhprof.so
xhprof.output_dir=/tmp

#設定中xhprof.output_dir指定了產生的profile檔案儲存的位置,我們將其指定為/ tmp。

三、將相關文件移動項目中

#

//xhprof下载压缩包中的xhprof_html和xhprof_lib
cp -r xhprof-master/xhprof_html /usr/local/nginx/html/xhprof/
cp -r xhprof-master/xhprof_lib /usr/local/nginx/html/xhprof/

配置一個域名,瀏覽器可以存取到http://will.com/xhprof/xhprof_html/index.php

#

server{
 listen 80;
 server_name will.com;
 location / {
  root /usr/local/nginx/html;
  index index.html;
 }
 location ~ \.php$ {
  root html;
  fastcgi_pass 127.0.0.1:9000;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include  fastcgi_params;
 }
 }

##四、安裝graphivz

//需要安装graphviz否则查看性能图片时候会报failed to execute cmd: " dot -Tpng". stderr: `sh: dot: command not found '
yum -y install graphviz

//入口文件的开始位置
xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);

业务逻辑...

//业务逻辑结束后
$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)

<?php
xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
function show($info)
{
 echo "<pre class="brush:php;toolbar:false">";
 print_r($info);
}

//不作数据校验
$rules = array(
 2=>array(&#39;min&#39;=>1, &#39;max&#39;=>10, &#39;chance&#39;=>30),//金额:分 概率:百分之(默认为100%,不足100%按第一档计算)
 array(&#39;min&#39;=>11, &#39;max&#39;=>25, &#39;chance&#39;=>60),
 array(&#39;min&#39;=>26, &#39;max&#39;=>50, &#39;chance&#39;=>10),
 array(&#39;min&#39;=>50, &#39;max&#39;=>80, &#39;chance&#39;=>0),
 array(&#39;min&#39;=>80, &#39;max&#39;=>100, &#39;chance&#39;=>0),
);
$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 . "<br/>";
//1.先确定档次
function getLevel($rules)
{
 $level = array();
 $chance = 0;
 foreach($rules as $k=>$v)
 {
 if ($v[&#39;chance&#39;]>0)
 {
  $chance += $v[&#39;chance&#39;]*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][&#39;min&#39;]*10000, $rules[$index][&#39;max&#39;]*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,最后我们输出了一个链接地址,使用改地址就可以看到本次请求的分析结果。

#2、檢視分析結果

先執行業務程式碼;

然後瀏覽器開啟http://will.com/xhprof/xhprof_html/index.php, 點擊最後一次產生xhprof檔案

注意到中間的

View Full Callgraph<span style="font-family:NSimsun"></span>鏈接,透過該連結我們可以看到圖形化的分析結果

圖中紅色的部分為效能比較低,耗時比較長的部分,我們可以根據根據哪些函數被標記為紅色對系統的程式碼進行最佳化

另外附上, 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效能瓶頸的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持php中文網。

您可能感興趣的文章:

Laravel框架中自訂範本指令相關總結

PHP遞歸實現快速排序的方法範例講解

PHP實作git部署的方法教學詳解

## ###########

以上是使用XHProf找出PHP效能瓶頸的實例講解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn