PHP性能追蹤及分析工具xhprof的安裝與使用

PHP性能追蹤及分析工具xhprof的安裝與使用

對於本地開發環境來講,進行性能分析xdebug是夠用了,但若是是線上環境的話,xdebug消耗較大,配置也不夠靈活,所以線上環境建議使用xhprof進行PHP性能追蹤及分析。php

咱們今天就簡單介紹一下xhprof的簡單安裝與使用html

xhprof的安裝

下載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的簡單用法

將下載的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:// /index.php?run=XXX&source=xhprof_foo查看結果。接下來訪問 http://xhprof.dev/xhprof_html/ 就能夠看到已經保存的結果,列出了全部函數的調用以及所消耗的時間,如圖:


分析一下示例代碼sample.php,關健代碼:

<?php
// 開始分析
xhprof_enable();

// 運行一些函數
foo();

// 中止分析,獲得分析數據
$xhprof_data = xhprof_disable();

$xhprof_data中記錄了程序運行過程當中全部的函數調用時間及CPU內存消耗,具體記錄哪些指標能夠經過xhprof_enable的參數控制,目前支持的參數有:

  • HPROF_FLAGS_NO_BUILTINS 跳過全部內置(內部)函數。
  • XHPROF_FLAGS_CPU 輸出的性能數據中添加 CPU 數據。
  • XHPROF_FLAGS_MEMORY 輸出的性能數據中添加內存數據。

以後的處理已經與xhprof擴展無關,大體是編寫一個存儲類XHProfRuns_Default,將$xhprof_data序列化並保存到某個目錄,能夠經過XHProfRuns_Default(__DIR__)將結果輸出到當前目錄,若是不指定則會讀取php.ini配置文件中的xhprof.output_dir,仍然沒有指定則會輸出到/tmp

xhprof_html/index.php將記錄的結果整理並可視化,默認的UI裏列出了:

  • funciton name : 函數名
  • calls: 調用次數
  • Incl. Wall Time (microsec): 函數運行時間(包括子函數)
  • IWall%:函數運行時間(包括子函數)佔比
  • Excl. Wall Time(microsec):函數運行時間(不包括子函數)
  • EWall%:函數運行時間(不包括子函數)

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這個文件,這樣侵入性更小,而且能夠實現基於站點的注入。

更漂亮的數據展現xhgui

注入代碼後咱們還須要實現保存xhprof數據以及展現數據的UI,現有的兩個比較流行的兩個輪子是 xhprof.ioxhgui 兩個項目都差很少,但綜合比較起來xhprof.io年久失修,xhgui還比較活躍,UI界面也相對比較美觀,我這裏就選擇xhgui進行數據展現了。

安裝xhgui

  • 從github中clone代碼至站點目錄/data/www/project-xhgui
mkdir /data/www/project-xhgui
cd /data/www/project-xhgui
git clone https://github.com/perftools/xhgui.git ./
  • 設置緩存目錄的權限,容許nginx建立文件
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
  • 運行XHGui的安裝腳本。安裝腳本將經過composer安裝XHGui的相關依賴。
php install.php
# 若是上面的命令執行報錯,則執行下面的命令
composer install
  • 配置web服務器,咱們這裏以nginx爲例
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

xhgui的具體使用

最近運行

也能夠按時間範圍和請求的地址進行搜索

能夠按執行時間、CPU時間、內存佔用進行排序

能夠點擊一個地址進去看這個地址的最近趨勢

一次運行的詳細

一個函數的運行詳細

一次運行的函數調用圖

一次運行的火焰圖

對比屢次請求


添加觀察函數


參考

相關文章
相關標籤/搜索