對於本地開發環境來講,進行性能分析xdebug
是夠用了,但若是是線上環境的話,xdebug
消耗較大,配置也不夠靈活,所以線上環境建議使用xhprof進行PHP性能追蹤及分析。php
咱們今天就簡單介紹一下xhprof的簡單安裝與使用html
下載xhprof,咱們這裏選擇的是經過git clone的方式,固然你也能夠從 http://pecl.php.net/package/xhprof 這裏下載。nginx
cd /usr/local/src # 我本身漢化的版本 git clone https://github.com/maxincai/xhgui.git # 你能夠clone原版 git clone https://github.com/phacility/xhprof.git
注意:
php5.4及以上版本不能在pecl中下載,不支持。須要在github上下載hhttps://github.com/phacility/xhprof.git。
另外xhprof已經好久沒有更新過了,截至目前還不支持php7,php7能夠試使用https://github.com/tideways/php-profiler-extension。git
安裝xhporofgithub
cd xhprof/extension /usr/local/php5.6/bin/phpize ./configure --with-php-config=/usr/local/php5.6/bin/php-config --enable-xhprof make make install
最後若是出現相似的提示信息,就表示編譯安裝成功web
stalling shared extensions: /usr/local/php-5.6.14/lib/php/extensions/no-debug-non-zts-20131226/
修改配置文件/etc/php5.6.ini
,在最後增長以下配置mongodb
[xhprof] extension=xhprof.so xhprof.output_dir=/data/www/xhprof/output
重啓php-fpm
後經過phpinfo
查看,或者在命令行經過php -m | grep xhprof
查看是否安裝成功。vim
注意:
須要建立output_dir
mkdir -p /data/www/xhprof/output緩存
將下載的xhprof複製到webroot目錄,我這裏以/data/www/project-xhprof
爲例安全
mkdir /data/www/project-xhprof cp -R /usr/local/src/xhprof/* /data/www/project-xhprof/ cd /data/www/project-xhprof
在nginx中增長站點配置
server { listen 80; server_name xhprof.dev; root /data/www/project-xhprof; index index.php index.html; access_log /var/log/nginx/xhprof.dev.log main; error_log /var/log/nginx/xhprof.dev.log.err debug; rewrite_log on; location ~* \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/var/run/php5.6-fpm.sock; fastcgi_index index.php; } }
重啓nginx,而後使用http://xhprof.dev/examples/sample.php
,能夠看到一些輸出,而且提示經過訪問http://
分析一下示例代碼sample.php
,關健代碼:
<?php // 開始分析 xhprof_enable(); // 運行一些函數 foo(); // 中止分析,獲得分析數據 $xhprof_data = xhprof_disable();
$xhprof_data
中記錄了程序運行過程當中全部的函數調用時間及CPU內存消耗,具體記錄哪些指標能夠經過xhprof_enable
的參數控制,目前支持的參數有:
以後的處理已經與xhprof擴展無關,大體是編寫一個存儲類XHProfRuns_Default
,將$xhprof_data
序列化並保存到某個目錄,能夠經過XHProfRuns_Default(__DIR__)
將結果輸出到當前目錄,若是不指定則會讀取php.ini
配置文件中的xhprof.output_dir
,仍然沒有指定則會輸出到/tmp
。
xhprof_html/index.php
將記錄的結果整理並可視化,默認的UI裏列出了:
在xhprof_html/index.php
中還能夠看到[View Full Callgraph]
連接,點擊後能夠繪製出一張可視化的性能分析圖,若是點擊後報錯的話,多是缺乏依賴graphviz
,
graphviz是一個繪製圖形的工具,能夠更爲直觀的讓你查看性能的瓶頸。
wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz cd graphviz-2.24.0 ./configure make && make install
或者直接使用yum安裝
yum install -y libpng yum install -y graphviz
這時候就能夠看到相似下面的效果
經過上面的這些介紹,咱們其實就已經能夠將xhprof整合到任何咱們已有的項目中,目前大部份的MVC框架都有惟一的入口文件,只須要在入口文件的開始處注入xhprof的代碼:
<?php //開啓xhprof xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU); //在程序結束後收集數據 register_shutdown_function(function() { $xhprof_data = xhprof_disable(); //讓數據收集程序在後臺運行 if (function_exists('fastcgi_finish_request')) { fastcgi_finish_request(); } //保存xhprof數據 ... });
咱們不可能在咱們須要分析的全部地方都加上,可是這樣免不了要修改項目的源代碼,其實php自己就提供了更好的注入方式,好比將上述邏輯保存爲/data/www/xhprof/inject.php
,而後修改php配置文件中的auto_prepend_file
配置
auto_prepend_file = /data/www/xhprof/inject.php
對於 Apache 服務器,添加如下代碼:
php_admin_value auto_prepend_file "/data/www/xhprof/inject.php"
對於 Nginx 服務器,在服務器配置中添加如下代碼:
fastcgi_param PHP_VALUE "auto_prepend_file=/data/www/xhprof/inject.php";
這樣全部的php請求文件都會自動注入/data/www/xhprof/inject.php
這個文件,這樣侵入性更小,而且能夠實現基於站點的注入。
注入代碼後咱們還須要實現保存xhprof數據以及展現數據的UI,現有的兩個比較流行的兩個輪子是 xhprof.io 和 xhgui 兩個項目都差很少,但綜合比較起來xhprof.io
年久失修,xhgui
還比較活躍,UI界面也相對比較美觀,我這裏就選擇xhgui
進行數據展現了。
安裝xhgui
/data/www/project-xhgui
mkdir /data/www/project-xhgui cd /data/www/project-xhgui git clone https://github.com/perftools/xhgui.git ./
chmod -R 777
啓動mongodb
實例,若是mongodb如沒有使用默認的端口和配置,或者有使用安全驗證,剛須要修改config/config.php
中的相關配置,咱們這裏使用的是默認配置,就不做修改了。
爲提升 MongoDB 的性能,你能夠運行如下指令以添加索引:
$ /usr/local/mongodb/bin/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 } )
mongodb
的PHP擴展wget http://pecl.php.net/get/mongodb-1.1.9.tgz tar zxvf mongodb-1.1.9.tgz cd mongodb-1.1.9 /usr/local/php5.6/bin/phpize ./configure --with-php-config=/usr/local/php5.6/bin/php-config make && make install vim /etc/php5.6.ini # 在文件最後增長 [mongo] extension=mongo.so # 查看是否安裝成功 php -m | grep mongo # 重啓php-fpm /etc/init.d/php5.6-fpm restart
php install.php # 若是上面的命令執行報錯,則執行下面的命令 composer install
server { listen 80; server_name xhgui.dev; root /data/www/project-xhgui/webroot; index index.php index.html; access_log /var/log/nginx/xhgui.dev.log main; error_log /var/log/nginx/xhgui.dev.log.err debug; rewrite_log on; location / { try_files $uri $uri/ /index.php?$uri&$args; } location ~* \.php$ { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass unix:/var/run/php5.6-fpm.sock; fastcgi_index index.php; } }
配置完成後重啓nginx,這時候打開http://xhgui.dev/
,就能夠看到相似下面的頁面
修改/etc/php5.6.ini
中的auto_prepend_file
配置完成xhgui的接入
auto_prepend_file = /data/www/project-xhgui/external/header.php
也能夠按時間範圍和請求的地址進行搜索
能夠按執行時間、CPU時間、內存佔用進行排序
能夠點擊一個地址進去看這個地址的最近趨勢