一、背景
項目即將上線,想通過一些工具來分析代碼的穩(wěn)定性和效率,想起在上個團(tuán)隊時使用過的xhprof擴(kuò)展;因為換了新電腦,所以需要重新編譯此擴(kuò)展,現(xiàn)將安裝與實際排查過程完整記錄下來,方便自己回顧和幫助更多的讀者。
XHProf 是 FaceBook 開發(fā)的一個函數(shù)級別的 PHP 分層分析器。
數(shù)據(jù)收集部分是一個基于 C 的 PHP 擴(kuò)展,分析報告是一系列基于 PHP 的 HTML 導(dǎo)航頁面。
XHProf 能統(tǒng)計每個函數(shù)的調(diào)用次數(shù)、內(nèi)存使用、CPU占用等多項重要的數(shù)據(jù)。
并且 XHProf 還能比較兩個統(tǒng)計樣本,或從多個數(shù)據(jù)樣本中匯總結(jié)果。
XHProf 是分析 PHP 程序執(zhí)行效率的利器,能讓我們得到更底層的的分析數(shù)據(jù)。
下面話不多說了,來一起看看詳細(xì)的介紹吧
二、操作步驟
- 安裝擴(kuò)展
- 配置擴(kuò)展
- 測試分析
三、安裝
xhprof擴(kuò)展PHP并不自帶,需要筆者去單獨(dú)安裝它,安裝之后才能使用,筆者這里采用源碼安裝方式,安裝過程如下
3.1 下載源碼
xhprof在PHP的PECL官方上面已經(jīng)比較老了,筆者的PHP版本為PHP7.1因此,需要在GitHub上下載xhprof上比較新的源碼,參考命令如下
git clone https://github.com/longxinH/xhprof
3.2 檢測環(huán)境
進(jìn)入編譯的文件夾,參考命令
cd xhprof/extension/
現(xiàn)在筆者需要編譯一下源碼,在編譯之前可以使用phpze來探測PHP的環(huán)境,參考命令如下:
phpize
返回結(jié)果如下
Configuring for:
PHP Api Version: 20160303
Zend Module Api No: 20160303
Zend Extension Api No: 320160303
3.3 編譯安裝
生成 Makefile,為下一步的編譯做準(zhǔn)備
./configure
返回結(jié)果如下
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
config.status: config.h is unchanged
開始編譯,并進(jìn)行安裝
make && make install
返回結(jié)果如下
Build complete.
Don't forget to run 'make test'.Installing shared extensions: /usr/local/Cellar/[email protected]/7.1.19/pecl/20160303/
從返回信息中可以看到已經(jīng)安裝完成,并顯示了擴(kuò)展文件存放的位置
四、配置
在編譯安裝源碼之后,筆者還需要對PHP的配置文件夾以及xhprof的進(jìn)行一些簡單的配置,操作過程如下所示
4.1 找出配置文件位置
要修改PHP的配置首先需要知道配置文件在什么位置,這里可以通過PHP的命令來查看配置文件存放位置,參考命令如下:
php --ini
執(zhí)行命令后,返回結(jié)果如下
Configuration File (php.ini) Path: /usr/local/etc/php/7.1
Loaded Configuration File: /usr/local/etc/php/7.1/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.1/conf.d
Additional .ini files parsed: /usr/local/etc/php/7.1/conf.d/ext-opcache.ini
在返回結(jié)果當(dāng)中,可以看到多個配置文件的路徑,筆者所需要的是第二個文件php.ini
查看擴(kuò)展目錄存放位置,參考命令如下
cat /usr/local/etc/php/7.1/php.ini | grep extension_dir
返回結(jié)果如下
extension_dir = "/usr/local/lib/php/pecl/20160303"
; extension_dir = "ext"
; Be sure to appropriately set the extension_dir directive.
;sqlite3.extension_dir =
4.2 修改配置
從返回的結(jié)果當(dāng)中,可以看到擴(kuò)展的存放目錄位置如下
/usr/local/lib/php/pecl/20160303
現(xiàn)在需要將剛才編譯好的xhprof擴(kuò)展復(fù)制到該目錄當(dāng)中,參考命令如下
cp /usr/local/Cellar/[email protected]/7.1.19/pecl/20160303/xhprof.so /usr/local/Cellar/[email protected]/7.1.19/pecl/20160303/
通過vim編輯器編輯配置文件,參考命令如下
vim /usr/local/etc/php/7.1/php.ini
在配置文件尾部增加xhprof的配置,以及自定義一個用來保存xhprof生成的源文件參考配置如下
[xhprof] extension=xhprof.so xhprof.output_dir=/data/www/xhprof/save_output_dir
4.3 重啟生效
保存好之后,筆者重啟php-fpm讓其配置生效,重啟命令可以通過brew命令來查看,參考命令如下:
brew info [email protected]
在命令執(zhí)行后,返回的信息中可以看到如下信息
To have launchd start [email protected] now and restart at login: brew services start [email protected] Or, if you don't want/need a background service you can just run: php-fpm
因此筆者構(gòu)造的重啟PHP-FPM命令如下:
brew services restart [email protected]
重啟完成后,返回結(jié)果如下
Stopping `[email protected]`... (might take a while) ==> Successfully stopped `[email protected]` (label: [email protected]) ==> Successfully started `[email protected]` (label: [email protected])
4.4 驗證安裝
現(xiàn)在驗證xhprof擴(kuò)展是否已經(jīng)安裝完成,參考命令如下
php -m | grep xhprof
命令執(zhí)行后,安裝擴(kuò)展成功的返回結(jié)果將會顯示xhprof,如下圖所示
五、測試
經(jīng)過上面的操作筆者已經(jīng)成功的安裝與配置,現(xiàn)在需要用PHP代碼來進(jìn)行驗證xhprof的分析效果
5.1 創(chuàng)建虛擬主機(jī)
首先創(chuàng)建一個虛擬主機(jī),讓用戶可以通過瀏覽器訪問所訪問,創(chuàng)建虛擬主機(jī)需要有一個根目錄,并編輯nginx配置文件,具體操作如下:
5.1.1 創(chuàng)建項目目錄
創(chuàng)建項目根目錄,參考命令如下
mkdir -p /Users/song/mycode/work/test
創(chuàng)建成功之后,筆者需要將之前git拉下來的部分代碼復(fù)制到項目根目錄當(dāng)中,參考命令如下
cp -r xhprof/xhprof_html /Users/song/mycode/work/test/ cp -r xhprof/xhprof_lib /Users/song/mycode/work/test/
5.1.2 編輯配置文件
添加配置文件,參考命令
/usr/local/etc/nginx/nginx.conf
添加配置文件如下
server { listen 80; server_name test.localhost; root /Users/song/mycode/work/test; index index.html index.htm index.php; location / { try_files $uri $uri/ /index.php?$query_string; } 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; } }
在/etc/hosts文件中增加入一行解析記錄,記錄內(nèi)容如下:
127.0.0.1 test.localhost
5.2 新建測試代碼
在git倉庫的examples文件夾下,已經(jīng)有了一份demo代碼,不過這份代碼的注釋都是英文,而且排版方式也不易筆者自己理解,因此筆者重新編輯了此文件,參考步驟如下命令
使用vim新建一個PHP文件
vim /Users/song/mycode/work/test/test.php
在文件中加入以下代碼
<?php //加載所需文件 include_once "./xhprof_lib/utils/xhprof_lib.php"; include_once "./xhprof_lib/utils/xhprof_runs.php"; //隨意定義一個函數(shù) function test($max) { for ($idx = 0; $idx < $max; $idx++) { echo ''; } } //定義測試方法 function a() { test(rand(1000,5000)); } //開始分析 xhprof_enable(); //需要分析的函數(shù) a(); //結(jié)束分析 $xhprof_data = xhprof_disable(); //實例化xhprof類 $xhprof_runs = new XHProfRuns_Default(); //獲取當(dāng)前當(dāng)前頁面分析結(jié)果 $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo"); echo "\nhttp://test.localhost/xhprof/xhprof_html/index.php?run=$run_id&source=xhprof_foo\n";
保存代碼之后,通過瀏覽器訪問對應(yīng)的URL地址,URL地址如下所示
http://test.localhost/xhprof/test.php
5.3 結(jié)果分析
運(yùn)行后結(jié)果,如下圖
在頁面中可以看到一個URL地址,復(fù)制并打開此URL地址之后,便能看到此代碼的分析結(jié)果,如下圖所示
在頁面中有一個列表,展示了每一個方法所消耗的時間,如果覺得列表的方式表示不夠清晰,點(diǎn)擊頁面中的 View Full Callgraph 鏈接可以直接生成一個圖片,如下圖所示
在圖中很清晰的可以看到執(zhí)行時間都消耗在test方法上,因此筆者可以針對這個方法進(jìn)行針對性的優(yōu)化。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。