使用PHP擴展Xhprof分析項目性能實踐

1、背景

項目即將上線,想經過一些工具來分析代碼的穩定性和效率,想起在上個團隊時使用過的xhprof擴展;由於換了新電腦,因此須要從新編譯此擴展,現將安裝與實際排查過程完整記錄下來,方便本身回顧和幫助更多的讀者。php

2、操做步驟

  1. 安裝擴展
  2. 配置擴展
  3. 測試分析

3、安裝

xhprof擴展PHP並不自帶,須要筆者去單獨安裝它,安裝以後才能使用,筆者這裏採用源碼安裝方式,安裝過程以下html

3.1 下載源碼

xhprof在PHP的PECL官方上面已經比較老了,筆者的PHP版本爲PHP7.1所以,須要在GitHub上下載xhprof上比較新的源碼,參考命令以下nginx

git clone https://github.com/longxinH/xhprof

3.2 檢測環境

進入編譯的文件夾,參考命令git

cd xhprof/extension/

如今筆者須要編譯一下源碼,在編譯以前可使用phpze來探測PHP的環境,參考命令以下:github

phpize

返回結果以下sql

Configuring for:
PHP Api Version:         20160303
Zend Module Api No:      20160303
Zend Extension Api No:   320160303

3.3 編譯安裝

生成 Makefile,爲下一步的編譯作準備vim

./configure

返回結果以下瀏覽器

creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
config.status: config.h is unchanged

開始編譯,並進行安裝微信

make && make install

返回結果以下app

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/Cellar/php@7.1/7.1.19/pecl/20160303/

從返回信息中能夠看到已經安裝完成,並顯示了擴展文件存放的位置

4、配置

在編譯安裝源碼以後,筆者還須要對PHP的配置文件夾以及xhprof的進行一些簡單的配置,操做過程以下所示

4.1 找出配置文件位置

要修改PHP的配置首先須要知道配置文件在什麼位置,這裏能夠經過PHP的命令來查看配置文件存放位置,參考命令以下:

php --ini

執行命令後,返回結果以下

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

在返回結果當中,能夠看到多個配置文件的路徑,筆者所須要的是第二個文件php.ini

查看擴展目錄存放位置,參考命令以下

cat /usr/local/etc/php/7.1/php.ini | grep extension_dir

返回結果以下

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 修改配置

從返回的結果當中,能夠看到擴展的存放目錄位置以下

/usr/local/lib/php/pecl/20160303

如今須要將剛纔編譯好的xhprof擴展複製到該目錄當中,參考命令以下

cp /usr/local/Cellar/php@7.1/7.1.19/pecl/20160303/xhprof.so  /usr/local/Cellar/php@7.1/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 php@7.1

在命令執行後,返回的信息中能夠看到以下信息

To have launchd start php@7.1 now and restart at login:
  brew services start php@7.1
Or, if you don't want/need a background service you can just run:
  php-fpm

所以筆者構造的重啓PHP-FPM命令以下:

brew services restart php@7.1

重啓完成後,返回結果以下

Stopping `php@7.1`... (might take a while)
==> Successfully stopped `php@7.1` (label: homebrew.mxcl.php@7.1)
==> Successfully started `php@7.1` (label: homebrew.mxcl.php@7.1)

4.4 驗證安裝

如今驗證xhprof擴展是否已經安裝完成,參考命令以下

php -m | grep xhprof

命令執行後,安裝擴展成功的返回結果將會顯示xhprof,以下圖所示

image

5、測試

通過上面的操做筆者已經成功的安裝與配置,如今須要用PHP代碼來進行驗證xhprof的分析效果

5.1 建立虛擬主機

首先建立一個虛擬主機,讓用戶能夠經過瀏覽器訪問所訪問,建立虛擬主機須要有一個根目錄,並編輯nginx配置文件,具體操做以下:

5.1.1 建立項目目錄

建立項目根目錄,參考命令以下

mkdir -p /Users/song/mycode/work/test

建立成功以後,筆者須要將以前git拉下來的部分代碼複製到項目根目錄當中,參考命令以下

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文件中增長入一行解析記錄,記錄內容以下:

127.0.0.1 test.localhost

5.2 新建測試代碼

在git倉庫的examples文件夾下,已經有了一份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";

//隨意定義一個函數
function test($max)
{
    for ($idx = 0; $idx < $max; $idx++) {
        echo '';
    }
}

//定義測試方法
function a()
{
    test(rand(1000,5000));
}

//開始分析
xhprof_enable();

//須要分析的函數
a();

//結束分析
$xhprof_data = xhprof_disable();
//實例化xhprof類
$xhprof_runs = new XHProfRuns_Default();
//獲取當前當前頁面分析結果
$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";

保存代碼以後,經過瀏覽器訪問對應的URL地址,URL地址以下所示

http://test.localhost/xhprof/test.php

5.3 結果分析

運行後結果,以下圖

image

在頁面中能夠看到一個URL地址,複製並打開此URL地址以後,便能看到此代碼的分析結果,以下圖所示

image

在頁面中有一個列表,展現了每個方法所消耗的時間,若是以爲列表的方式表示不夠清晰,點擊頁面中的 View Full Callgraph 連接能夠直接生成一個圖片,以下圖所示

image

在圖中很清晰的能夠看到執行時間都消耗在test方法上,所以筆者能夠針對這個方法進行鍼對性的優化。


做者:湯青松

微信:songboy8888

日期:2018-08-27

相關文章
相關標籤/搜索