溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何在php7中使用xhprof測試php性能?

發(fā)布時間:2020-07-16 09:35:06 來源:億速云 閱讀:288 作者:Leah 欄目:編程語言

今天就跟大家聊聊有關如何在php7中使用xhprof測試php性能?,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

介紹

1 背景

  • PHP的xhprof擴展Facebook不再進行更新和維護,因為Faceboo已經(jīng)全面使用HHVM,不再使用PHP zend引擎。

  • xhprof不支持新版本的PHP(PHP7),tideways擴展是從xhprof項目fork下來繼續(xù)進行維護的,目前支持PHP 7.2, 7.1, 7.0, 5.6 and 5.5 。

  • tideways是開源項目,它收費的只是UI服務,其實 xhgui完全可以滿足我們?nèi)粘5男枨?/p>

2 功能

  tideways是用來測試PHP性能的擴展,它能獲取PHP執(zhí)行的整個過程中調(diào)用的函數(shù)、調(diào)用函數(shù)次數(shù)、執(zhí)行時間、CPU時間、內(nèi)存占用、內(nèi)存峰值、總執(zhí)行時間、總CPU時間、總內(nèi)存占用、總內(nèi)存峰值等數(shù)據(jù),通過以上數(shù)據(jù)進行分析,找出PHP的性能瓶頸、分析PHP執(zhí)行過程等。

3 優(yōu)點

  • tideways是一個PHP擴展,結合xhgui,無需在PHP代碼中進行埋點來監(jiān)控代碼

  • 可以設置執(zhí)行頻率(例如1/100),無需每個請求都生成執(zhí)行日志,從而導致性能損失;也可以主動控制是否生成執(zhí)行日志,通過請求參數(shù)來控制(debug=1)

  • 有簡單直接的UI對數(shù)據(jù)進行轉(zhuǎn)化

  • 可以自由的搭配條件進行數(shù)據(jù)篩選,例如分析某個特定的接口,分析某個時間段的接口請求情況等

4 缺點

  雖然是非侵入式的,但是如果對每個接口生成執(zhí)行日志,那么對CPU和內(nèi)存的消耗是不可忽略的。

5 實現(xiàn)原理

  • tideways擴展負責生成運行日志
  • nginx中通過配置fastcgi_param PHP_VALUE auto_prepend_file,在請求開始之前執(zhí)行auto_prepend_file配置的PHP文件,文件中利用register_shutdown_function方法,在PHP進程結束的時候調(diào)用tideways_disable來實現(xiàn)tideways的嵌入,然后將執(zhí)行日志存入mongodb或者mysql或者文件中,通過xhgui分析之后進行展示,展示形式包括柱狀圖、瀑布流、火焰圖。

應用

  接下來介紹兩種應用方式:侵入式非侵入式

    侵入式指的是在代碼中添加代碼,侵入式使用的是默認的ui;

    非侵入式指的是在不對代碼做任何改動,通過改動nginx/apache實現(xiàn)代碼注入,非侵入式使用的是xhgui;

  安裝 tideways_xhprof

git clone "https://github.com/tideways/php-xhprof-extension.git"
cd php-xhprof-extension
phpize
./configure --with-php-config=/usr/local/php7/bin/php-config
make
sudo make install

 在php.ini中加上 extension=tideways_xhprof.so

非侵入式:

<?php
tideways_xhprof_enable();

// your application code

$data = tideways_xhprof_disable();
file_put_contents(
    sys_get_temp_dir() . "/" . uniqid() . ".yourapp.xhprof",
    serialize($data)
);

  // $data = tideways_xhprof_disable();
  // file_put_contents(
  //     sys_get_temp_dir() . "/" . date('His', time()) . ".material.xhprof",
  //     serialize($data)
  // );

這里生成的 .xhprof文件在 tmp 目錄下,默認UI也會去tmp目錄下查找 .xhprof文件

安裝默認UI用來查找數(shù)據(jù)

git clone git@github.com:phacility/xhprof.git

將此存儲庫中的xhprof_libxhprof_html目錄安裝到您的Web文件夾中,并導航xhprof_html/index.php 以查看跟蹤列表。

如果想要看函數(shù)調(diào)用筆記需要安裝Callgraph

安裝 Callgraph

    Callgraph 實際由三個工具組合而成。
      一個是用于生成 C 函數(shù)調(diào)用樹的 cflow 或者 calltree,下文主要介紹 cflow。
      一個處理 dot 文本圖形語言的工具,由 graphviz 提升。
      一個用于把 C 函數(shù)調(diào)用樹轉(zhuǎn)換為 dot 格式的腳本:tree2dotx
  以 Ubuntu 為例,分別安裝它們:

sudo apt-get install cflow graphviz

  接下來安裝 tree2dotx 和 Callgraph,這里都默認安裝到 /usr/local/bin。

wget -c https://github.com/tinyclub/linux-0.11-lab/raw/master/tools/tree2dotx
wget -c https://github.com/tinyclub/linux-0.11-lab/raw/master/tools/callgraph
sudo cp tree2dotx callgraph /usr/local/bin
sudo chmod +x /usr/local/bin/{tree2dotx,callgraph}

接下來展示兩張效果圖:

如何在php7中使用xhprof測試php性能?     如何在php7中使用xhprof測試php性能?

侵入式:

  侵入式使用的 xhgui 需要用到 mongodb

安裝xhgui

git clone git@github.com:perftools/xhgui.git

配置Nginx

server {
  listen 80;
  server_name site.localhost;
  root /Users/markstory/Sites/awesome-thing/app/webroot/;
  fastcgi_param PHP_VALUE "auto_prepend_file=/home/www/xhgui/external/header.php";  #這里依據(jù)個人目錄而配
}

  這里的意思是在執(zhí)行項目php代碼前 先執(zhí)行 header.php,從而達到非侵入式檢測性能的目的

xhgui配置(生成日志的頻率)

    在xhgui的config/config.default.php中,可設置采樣命中次數(shù);
    return rand(1, 100) === 42; 為1%的采樣率,改成return True;則標識每次都采樣

'profiler.enable' => function() {
   // url 中包含debug=1則百分百捕獲
   if(!empty($_GET['debug'])){
       return True;
   }else{
       // 1%采樣
       return rand(1, 100) === 42;
   }
}

mongodb的配置

   xhgui/config/config.default.php

// Can be either mongodb or file.
   /*
   'save.handler' => 'file',
   'save.handler.filename' => dirname(__DIR__) . '/cache/' . 'xhgui.data.' . microtime(true) . '_' . substr(md5($url), 0, 6),
   */
   'save.handler' => 'mongodb',
   // Needed for file save handler. Beware of file locking. You can adujst this file path
   // to reduce locking problems (eg uniqid, time ...)
   //'save.handler.filename' => __DIR__.'/../data/xhgui_'.date('Ymd').'.dat',
   'db.host' => 'mongodb://127.0.0.1:27017',
   'db.db' => 'xhprof',

mongo服務器的配置

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的效果圖

如何在php7中使用xhprof測試php性能?

如何在php7中使用xhprof測試php性能?

如何在php7中使用xhprof測試php性能?


看完上述內(nèi)容,你們對如何在php7中使用xhprof測試php性能?有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI