主頁 > 知識庫 > 使用XHProf查找PHP性能瓶頸的實例

使用XHProf查找PHP性能瓶頸的實例

熱門標簽:電話機器人危險嗎 深圳外呼系統(tǒng)收費 南寧高頻外呼回撥系統(tǒng)哪家好 長沙crm外呼系統(tǒng)業(yè)務 專業(yè)電話機器人批發(fā)商 400電話申請方法收費 江蘇外呼電銷機器人報價 400電話辦理福州市 離石地圖標注

XHProf是facebook 開發(fā)的一個測試php性能的擴展,本文記錄了在PHP應用中使用XHProf對PHP進行性能優(yōu)化,查找性能瓶頸的方法。

一、安裝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);

業(yè)務邏輯...

//業(yè)務邏輯結束后
$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();//數(shù)據(jù)會保存在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>";
 print_r($info);
}

//不作數(shù)據(jù)校驗
$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, 'chance'=>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['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;//防止出現(xiàn)免單情況
 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();//數(shù)據(jù)會保存在php.ini中xhprof.output_dir設置的目錄去中 
$run_id = $objXhprofRun->save_run($xhprof_data, "test");
echo "http://will.com/xhprof/xhprof_html/index.php?run=$run_idsource=test";//變量$runId是本次請求生成分析結果的id,最后我們輸出了一個鏈接地址,使用改地址就可以看到本次請求的分析結果。

六、查看分析結果

先運行業(yè)務代碼;

然后瀏覽器打開 http://will.com/xhprof/xhprof_html/index.php, 點擊最后一次生成xhprof文件

注意到中間的View Full Callgraph鏈接,通過該鏈接我們可以看到圖形化的分析結果

圖中紅色的部分為性能比較低,耗時比較長的部分,我們可以根據(jù)根據(jù)哪些函數(shù)被標記為紅色對系統(tǒng)的代碼進行優(yōu)化

另外附上, xhprof報告字段含義:

Function Name:方法名稱。

Calls:方法被調(diào)用的次數(shù)。

Calls%:方法調(diào)用次數(shù)在同級方法總數(shù)調(diào)用次數(shù)中所占的百分比。

Incl.Wall Time(microsec):方法執(zhí)行花費的時間,包括子方法的執(zhí)行時間。(單位:微秒)

IWall%:方法執(zhí)行花費的時間百分比。

Excl. Wall Time(microsec):方法本身執(zhí)行花費的時間,不包括子方法的執(zhí)行時間。(單位:微秒)

EWall%:方法本身執(zhí)行花費的時間百分比。

Incl. CPU(microsecs):方法執(zhí)行花費的CPU時間,包括子方法的執(zhí)行時間。(單位:微秒)

ICpu%:方法執(zhí)行花費的CPU時間百分比。

Excl. CPU(microsec):方法本身執(zhí)行花費的CPU時間,不包括子方法的執(zhí)行時間。(單位:微秒)

ECPU%:方法本身執(zhí)行花費的CPU時間百分比。

Incl.MemUse(bytes):方法執(zhí)行占用的內(nèi)存,包括子方法執(zhí)行占用的內(nèi)存。(單位:字節(jié))

IMemUse%:方法執(zhí)行占用的內(nèi)存百分比。

Excl.MemUse(bytes):方法本身執(zhí)行占用的內(nèi)存,不包括子方法執(zhí)行占用的內(nèi)存。(單位:字節(jié))

EMemUse%:方法本身執(zhí)行占用的內(nèi)存百分比。

Incl.PeakMemUse(bytes):Incl.MemUse峰值。(單位:字節(jié))

IPeakMemUse%:Incl.MemUse峰值百分比。

Excl.PeakMemUse(bytes):Excl.MemUse峰值。單位:(字節(jié))

EPeakMemUse%:Excl.MemUse峰值百分比。

以上這篇使用XHProf查找PHP性能瓶頸的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • php xhprof使用實例詳解
  • 利用PHP擴展Xhprof分析項目性能實踐教程
  • PHP性能測試工具xhprof安裝與使用方法詳解
  • PHP性能分析工具xhprof的安裝使用與注意事項
  • Linux系統(tǒng)下使用XHProf和XHGui分析PHP運行性能
  • php輕量級的性能分析工具xhprof的安裝使用
  • PHP性能分析工具XHProf安裝使用教程
  • 基于在生產(chǎn)環(huán)境中使用php性能測試工具xhprof的詳解
  • PHP7下安裝并使用xhprof性能分析工具

標簽:太原 株洲 濱州 南昌 曲靖 白酒營銷 興安盟 南京

巨人網(wǎng)絡通訊聲明:本文標題《使用XHProf查找PHP性能瓶頸的實例》,本文關鍵詞  使用,XHProf,查找,PHP,性能,;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《使用XHProf查找PHP性能瓶頸的實例》相關的同類信息!
  • 本頁收集關于使用XHProf查找PHP性能瓶頸的實例的相關信息資訊供網(wǎng)民參考!
  • 推薦文章