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

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

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

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

xhprof的安裝

下載xhprof,我們這裏選擇的是通過git clone的方式,當然你也可以從 http://pecl.php.net/package/xhprof 這裏下載。

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。

安裝xhporof

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

最後如果出現類似的提示信息,就表示編譯安裝成功

stalling shared extensions:     /usr/local/php-5.6.14/lib/php/extensions/no-debug-non-zts-20131226/

修改配置文件/etc/php5.6.ini,在最後增加如下配置

[xhprof]
extension=xhprof.so
xhprof.output_dir=/data/www/xhprof/output

重啓php-fpm後通過phpinfo查看,或者在命令行通過php -m | grep xhprof查看是否安裝成功。

注意:
需要創建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://


分析一下示例代碼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時間、內存佔用進行排序

可以點擊一個地址進去看這個地址的最近趨勢

一次運行的詳細

一個函數的運行詳細

一次運行的函數調用圖

一次運行的火焰圖

對比多次請求


添加觀察函數


參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章