PHP性能被動(dòng)分析工具之xhgui加tideways的安裝實(shí)踐
By:0x584A Date:2016-11-23 17:55:42
前言
最近一直想做個(gè)接口信能分析康栈,但是手動(dòng)打log實(shí)在能把人給累死薪夕。怎么辦呢股耽?想到之前有寫過一篇我所知道的PHP相關(guān)調(diào)優(yōu)匯總巧婶,里面有一個(gè)Xdebug + kcachegrind
的調(diào)優(yōu)方式贪染。 但是呢越走,每次都需要將它產(chǎn)生的cachegrind.out.*
文件下到本地棚品,再用kcachegrind
打開做分析,而且體驗(yàn)感也不是特別好(原諒我英語(yǔ)不過三的渣渣...)
性能分析的UI組合
-
uprofiler
點(diǎn)擊下載 -
xhprof
+xhprof.io
【年久失修用uprofiler
替換即可或用修復(fù)板廊敌,但支持保存分析數(shù)據(jù)至mysql铜跑,函數(shù)調(diào)用記錄完整,內(nèi)核級(jí)別函數(shù)都能顯示骡澈,支持域名索引】修復(fù)板下載 -
xhprof
oruprofiler
ortideways
+xhgui
【推薦锅纺,保存數(shù)據(jù)至MongoDB,UI界面友好肋殴,有中文UI版伞广,不支持域名索引對(duì)于線上調(diào)試支持較差】中文版下載拣帽,英文原版下載 -
tideways
【推薦,這個(gè)最絢的而且一直在持續(xù)維護(hù)嚼锄。但是使用它酷炫的UI需要付費(fèi)减拭,擴(kuò)展則不需要∏螅】tideways下載地址
安裝
-
環(huán)境
-
tideways
+xhgui
php>5.5
mongodb
php5-mcrypt
apt-get install libcurl4-openssl-dev libpcre3-dev
-
- 安裝mongodb
前置需安裝php-devsudo apt-get install php5-dev
$ sudo pecl install mongodb
$ cd /etc/php5/mods-available
$ sudo sh -c "echo 'extension=mongodb.so' > /etc/php5/mods-available/mongodb.ini"
[sudo] 0x584A 的密碼:
$ cd ../fpm/conf.d
$ sudo ln -s ../../mods-available/mongodb.ini 20-mongodb.ini
$ sudo service php5-fpm restart
$ sudo apt-get install mongodb -y
- 安裝xhgui
$ git clone https://github.com/maxincai/xhgui.git
$ cd xhgui
$ php install.php
加索引
$ mongo
> use xhprof
> db.results.ensureIndex( { 'meta.SERVER.REQUEST_TIME' : -1 } )
> db.results.ensureIndex( { 'profile.main().wt' : -1 } )
> db.results.ensureIndex( { 'profile.main().mu' : -1 } )
> db.results.ensureIndex( { 'profile.main().cpu' : -1 } )
> db.results.ensureIndex( { 'meta.url' : 1 } )
- 安裝tideways
見該安裝地址拧粪,選擇系統(tǒng)安裝方式https://tideways.io/profiler/docs/setup/installation
- 需要在nginx應(yīng)用中加入
fastcgi_param TIDEWAYS_SAMPLERATE "25";
- 需要在nginx應(yīng)用中加入
fastcgi_param PHP_VALUE "auto_prepend_file=/home/0x584A/www/xhgui/external/header.php";
-
ngxin應(yīng)用配置
應(yīng)用
server { listen 127.0.10.1:80; server_name app.com; root /home/0x584A/www/app; index index.html index.htm index.php; location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?$1 last ; break; } } location ~ ^(.+\.php)(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; fastcgi_param TIDEWAYS_SAMPLERATE "25"; fastcgi_param PHP_VALUE "auto_prepend_file=/home/0x584A/www/xhgui/external/header.php"; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $DOCUMENT_ROOT$fastcgi_path_info; fastcgi_param SCRIPT_FILENAME $DOCUMENT_ROOT/$fastcgi_script_name; include fastcgi_params; } }
xhgui
server { listen 127.0.10.2:80; server_name debug.com; root /home/0x584A/www/xhgui/webroot; index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php?$uri&$args; } location ~ \.php$ { try_files $uri =404; include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } }
訪問配置好的頁(yè)面即可。
注意
-
分析方式請(qǐng)自行更具url設(shè)置
'profiler.enable' => function() { // url 中包含debug=1則百分百捕獲 if(!empty($_GET['debug'])){ return True; }else{ // 1%采樣 return rand(1, 100) === 42; } },
-
在xhgui的
config/config.default.php
中沧侥,可設(shè)置采樣命中次數(shù)可霎;-
return rand(1, 100) === 42;
為1%
的采樣率,改成return True;
則標(biāo)識(shí)每次都采樣
-
-
分析參數(shù)過多則清除mongodb數(shù)據(jù)
$ mongo $ use xhprof; $ db.dropDatabase();
終極特效
1.png
2.png
3.png