PHP性能被動分析工具之xhgui加tideways的安裝實踐
By:0x584A Date:2016-11-23 17:55:42
前言
最近一直想做個接口信能分析,但是手動打log實在能把人給累死。怎么辦呢?想到之前有寫過一篇我所知道的PHP相關(guān)調(diào)優(yōu)匯總,里面有一個Xdebug + kcachegrind的調(diào)優(yōu)方式。 但是呢,每次都需要將它產(chǎn)生的cachegrind.out.*文件下到本地,再用kcachegrind打開做分析,而且體驗感也不是特別好(原諒我英語不過三的渣渣...)
性能分析的UI組合
-
uprofiler點擊下載 -
xhprof+xhprof.io【年久失修用uprofiler替換即可或用修復(fù)板,但支持保存分析數(shù)據(jù)至mysql,函數(shù)調(diào)用記錄完整,內(nèi)核級別函數(shù)都能顯示,支持域名索引】修復(fù)板下載 -
xhproforuprofilerortideways+xhgui【推薦,保存數(shù)據(jù)至MongoDB,UI界面友好,有中文UI版,不支持域名索引對于線上調(diào)試支持較差】中文版下載,英文原版下載 -
tideways【推薦,這個最絢的而且一直在持續(xù)維護(hù)。但是使用它酷炫的UI需要付費,擴(kuò)展則不需要?!?a target="_blank" rel="nofollow">tideways下載地址
安裝
-
環(huán)境
-
tideways+xhgui php>5.5mongodbphp5-mcryptapt-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; } }訪問配置好的頁面即可。
注意
-
分析方式請自行更具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ù)過多則清除mongodb數(shù)據(jù)
$ mongo $ use xhprof; $ db.dropDatabase();
終極特效

1.png

2.png

3.png