使用XHProf分析PHP性能瓶頸的方法二

不言
發布: 2023-04-02 20:06:02
原創
1781 人瀏覽過

這篇文章主要介紹了關於使用XHProf分析PHP性能瓶頸的方法二,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

上一篇文章裡,我們介紹如何基於xhprof擴充功能來分析PHP效能,並記錄到日誌裡,最後使用xhprof擴充自帶的UI在web裡展示出來。本篇文章將講述2個知識點:

  • 使用xhgui取代xhprof的預設UI介面,更便於分析

  • 使用tideways擴充替換xhprof擴展

使用更漂亮的UI: xhgui

xhgui支援XHProf, Uprofiler或Tideways 擴展,也就是說,只要安裝了這幾種擴充中的一種即可。

本次測試中,實際使用了tideways擴充(切換為XHProf擴充後web裡看不到數據,原因未知。切換為Uprofiler也沒有數據。)。

xhprof雖然來自facebook但已經很久不更新,官方來源已經顯示This package is abandoned and no longer maintained(此包已廢棄,不再維護)。 tideways剛好相反,一直有商業公司在維護,並且積極的支持了PHP7。兩個擴充都是開源的,綜上所述我建議大家選擇tideways來分析程式碼。

安裝tideways擴充:

wget https://github.com/tideways/php-xhprof-extension/archive/v4.1.5.tar.gz -O php-xhprof-extension-4.1.5.tar.gz
tar xzf /php-xhprof-extension-4.1.5.tar.gz 
cd php-xhprof-extension-4.1.5 
phpize  
./configure
make && make install
登入後複製

安裝xhgui

cd  /work/
git clone https://github.com/perftools/xhgui.git xhgui
登入後複製

如果需要安裝中文介面的,可以:

git clone https://github.com/laynefyc/xhgui-branch.git  xhgui
登入後複製

然後安裝xhgui依賴:

cd xhgui
php install.php
登入後複製

安裝需要等待幾分鐘,請耐心等待。

設定快取目錄的權限,允許nginx建立檔案:

chmod -R 777
登入後複製

xhgui已經把注入入口檔案都寫好了,位於external/header.php,無需我們手動去寫類似上一篇的xhprof.inc.php注入檔。

安裝MongoDB及客戶端

xhgui 把日誌寫到了MongoDB,所以使用xhgui需要安裝MongoDB服務端。此處省略安裝、啟動MongoDB服務端程序。

為提高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 } )
登入後複製

同理,由於xhgui是PHP寫的,還需要讀取MongoDB裡的數據,需要安裝MongoDB php 用戶端:

pecl install mongodb
登入後複製

然後在php.ini檔案中新增設定:

[mongo]
extension=mongo.so
登入後複製

查看擴充功能是否已安裝成功:

php -m | grep mongo
登入後複製

然後重新啟動php-fpm服務。

配置xhgui

xhgui的config目錄有一個config.default.php,複製為config.php,如果mongodb位址不是預設的,修改:

    'db.host' => 'mongodb://127.0.0.1:27017',
登入後複製

還有修改取樣頻率,預設是1/100,測試的話改為true:

    'profiler.enable' => function() {
        //return rand(1, 100) === 42;
        return true;
    },
登入後複製

配置項目注入

上一篇文章中,我們介紹到,注入的入口檔案可以寫入php.ini或nginx,我建議寫在nginx配置,這樣只會影響該nginx對應的項目,而不是所有使用該php環境的項目。入口檔案使用xhgui自帶的注入檔:

jifen.cc.conf

location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param PHP_VALUE "auto_prepend_file=/work/xhgui/external/header.php";
    include        fastcgi_params;
}
登入後複製

設定xhgui web

我們修改xhprof.test.com.conf為:

server {
    listen       80;
    server_name  xhprof.test.com;

    #root /work/xhprof/xhprof_html;
    root /work/xhgui/webroot/;
    index index.php index.html;
    
    location / {
       if (!-e $request_filename) {
            rewrite . /index.php last;
        }
    }

    location ~ \.php$ {
        
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    
}
登入後複製

重啟nginx服務。

我們請求幾次應用的接口,打開瀏覽器輸入http://xhprof.test.com/,可以看到:

使用XHProf分析PHP性能瓶頸的方法二點擊某次請求進去看詳情:
使用XHProf分析PHP性能瓶頸的方法二

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦:

使用XHProf分析PHP效能瓶頸的方法一

以上是使用XHProf分析PHP性能瓶頸的方法二的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!