xhgui基於xhprof,以圖形化方式顯示結果。簡單點就是更加直觀。 採用tideways + xhgui
組合的緣由是個人PHP版本是7,而xhprof不支持,(雖然我安裝了一個支持php7的xhprof,可是沒有數據,猜想和xhgui不兼容。)php
個人PHP版本是PHP7:html
MongoDB的版本是V3.6.2:nginx
1.安裝mongodb(mac下面安裝mongodb,這個本身google或者baidu吧)git
brew install mongodb
複製代碼
2.安裝php的mongodb的擴展github
brew install php70-mongo
複製代碼
查看是否安裝成功(記得重啓php-fpm)web
3.安裝php的tideways擴展mongodb
brew install php70-tideways
cd /usr/local/etc/php/7.0/conf.d
vim ext-tideways_xhprof.ini
複製代碼
添加內容(這個就是編譯成功以後的路徑)vim
[tideways_xhprof]
extension="/usr/local/Cellar/php70/7.0.14_7/lib/php/extensions/no-debug-non-zts-20151012/tideways_xhprof.so"
;不須要自動加載,在程序中控制就行
tideways.auto_prepend_library=0
;頻率設置爲100,在程序調用時能改
tideways.sample_rate=100
複製代碼
查看是否安裝成功(記得重啓php-fpm)segmentfault
4.安裝 xhgui瀏覽器
cd /Users/birjemin/Developer/Php
git clone https://github.com/laynefyc/xhgui-branch.git
// 我這裏把xhgui-branch目錄重命名爲xhprof_gui
cd xhprof_gui/extension
php install.php
複製代碼
配置xhgui(extension, profiler.enable, db.host, db.db參數)
cd /Users/birjemin/Developer/Php/xhprof_gui/config
cp config.default.php config.php
vim config.php
複製代碼
恭喜安裝成功!
1.mongodb須要新建相應的db(上面的第四點confi.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 } )
複製代碼
2.在監聽的網站nginx配置加上
3.能夠在須要監聽的接口中代碼片斷前面引入header.php
include "/Users/birjemin/Developer/Php/xhprof_gui/external/header.php";
複製代碼
4.將克隆的xprof_gui配置虛擬主機,這個和你的項目同樣的,就把xprof也當作一個項目,配置成瀏覽器可訪問。好比個人配置:
server {
listen 80;
server_name local.xhprof-gui.com;
# root directive should be global
root /Users/birjemin/Developer/Php/xhprof_gui/webroot;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
複製代碼
5.重啓nginx,瀏覽器訪問local.xhprof-gui.com看看能不能訪問(沒有目錄??你是否是瀏覽器沒法訪問目錄??權限沒開。。本身配置一下)
6.在postman或者瀏覽器訪問接口,轉啊轉,好了以後就能夠去 http://local.xhprof-gui.com/
查看了。(圖我就不截了。。)
1.安裝完了,跑起來數據爲空
我猜想我安裝的xhprof雖然支持PHP7,可是和xhgui不兼容,把config.php裏面的 extension
參數改爲 tideways_xhprof
而不是 xhprof
2.mongoDb報錯
這是一個bug(issue),請按照這個方法修改相應文件。
3.爲啥不在nginx裏面配置
fastcgi_param PHP_VALUE "auto_prepend_file=/Users/birjemin/Developer/Php/xhprof_gui/external/header.php";
複製代碼
而是
include "/Users/birjemin/Developer/Php/xhprof_gui/external/header.php";
複製代碼
由於我這是本地調試,重在分析某一個接口,而不是觀測線上的正式環境。還有這個緣由issues
重啓php-fpm(視我的重啓方式而定,個人重啓方式是這樣的)
cd /usr/local/etc/php/7.0/
sudo killall php-fpm
sudo /usr/local/Cellar/php70/7.0.14_7/sbin/php-fpm -D
複製代碼