怎么在PHP中利用Xhprof擴展分析項目性能-創(chuàng)新互聯(lián)

怎么在PHP中利用Xhprof擴展分析項目性能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

成都創(chuàng)新互聯(lián)制作網(wǎng)站網(wǎng)頁找三站合一網(wǎng)站制作公司,專注于網(wǎng)頁設(shè)計,網(wǎng)站建設(shè)、網(wǎng)站制作,網(wǎng)站設(shè)計,企業(yè)網(wǎng)站搭建,網(wǎng)站開發(fā),建網(wǎng)站業(yè)務(wù),680元做網(wǎng)站,已為近千家服務(wù),成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)將一如既往的為我們的客戶提供最優(yōu)質(zhì)的網(wǎng)站建設(shè)、網(wǎng)絡(luò)營銷推廣服務(wù)!

 下載源碼


xhprof在PHP的PECL官方上面已經(jīng)比較老了,筆者的PHP版本為PHP7.1因此,需要在GitHub上下載xhprof上比較新的源碼,參考命令如下

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

3.2 檢測環(huá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


開始編譯,并進行安裝

make && make install

返回結(jié)果如下

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

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


從返回信息中可以看到已經(jīng)安裝完成,并顯示了擴展文件存放的位置

四、配置


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

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

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

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)中,可以看到擴展的存放目錄位置如下

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


現(xiàn)在需要將剛才編譯好的xhprof擴展復(fù)制到該目錄當(dāng)中,參考命令如下

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

在命令執(zhí)行后,返回的信息中可以看到如下信息

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

因此筆者構(gòu)造的重啟PHP-FPM命令如下:

brew services restart php@7.1

重啟完成后,返回結(jié)果如下

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 驗證安裝


現(xiàn)在驗證xhprof擴展是否已經(jīng)安裝完成,參考命令如下

php -m | grep xhprof

命令執(zhí)行后,安裝擴展成功的返回結(jié)果將會顯示xhprof,如下圖所示

怎么在PHP中利用Xhprof擴展分析項目性能

五、測試


經(jīng)過上面的操作筆者已經(jīng)成功的安裝與配置,現(xiàn)在需要用PHP代碼來進行驗證xhprof的分析效果

5.1 創(chuàng)建虛擬主機


首先創(chuàng)建一個虛擬主機,讓用戶可以通過瀏覽器訪問所訪問,創(chuàng)建虛擬主機需要有一個根目錄,并編輯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é)果分析


運行后結(jié)果,如下圖

怎么在PHP中利用Xhprof擴展分析項目性能

在頁面中可以看到一個URL地址,復(fù)制并打開此URL地址之后,便能看到此代碼的分析結(jié)果,如下圖所示

怎么在PHP中利用Xhprof擴展分析項目性能

在頁面中有一個列表,展示了每一個方法所消耗的時間,如果覺得列表的方式表示不夠清晰,點擊頁面中的 View Full Callgraph 鏈接可以直接生成一個圖片,如下圖所示

怎么在PHP中利用Xhprof擴展分析項目性能

看完上述內(nèi)容,你們掌握怎么在PHP中利用Xhprof擴展分析項目性能的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)頁標(biāo)題:怎么在PHP中利用Xhprof擴展分析項目性能-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://bm7419.com/article28/gdgjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計公司營銷型網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、域名注冊、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司