php7使用xhprof測試php性能


  

介紹

1 背景

  • PHP的xhprof擴展Facebook不再進行更新和維護,因為Faceboo已經全面使用HHVM,不再使用PHP zend引擎。
  • xhprof不支持新版本的PHP(PHP7),tideways擴展是從xhprof項目fork下來繼續進行維護的,目前支持PHP 7.2, 7.1, 7.0, 5.6 and 5.5 。
  • tideways是開源項目,它收費的只是UI服務,其實 xhgui完全可以滿足我們日常的需求

2 功能

  tideways是用來測試PHP性能的擴展,它能獲取PHP執行的整個過程中調用的函數、調用函數次數、執行時間、CPU時間、內存占用、內存峰值、總執行時間、總CPU時間、總內存占用、總內存峰值等數據,通過以上數據進行分析,找出PHP的性能瓶頸、分析PHP執行過程等。

3 優點

  • tideways是一個PHP擴展,結合xhgui,無需在PHP代碼中進行埋點來監控代碼
  • 可以設置執行頻率(例如1/100),無需每個請求都生成執行日志,從而導致性能損失;也可以主動控制是否生成執行日志,通過請求參數來控制(debug=1)
  • 有簡單直接的UI對數據進行轉化
  • 可以自由的搭配條件進行數據篩選,例如分析某個特定的接口,分析某個時間段的接口請求情況等

4 缺點

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

5 實現原理

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

 應用

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

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

    非侵入式指的是在不對代碼做任何改動,通過改動nginx/apache實現代碼注入,非侵入式使用的是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用來查找數據

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

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

如果想要看函數調用筆記需要安裝Callgraph

  安裝 Callgraph

    Callgraph 實際由三個工具組合而成。
      一個是用於生成 C 函數調用樹的 cflow 或者 calltree,下文主要介紹 cflow。
      一個處理 dot 文本圖形語言的工具,由 graphviz 提升。
      一個用於把 C 函數調用樹轉換為 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}

接下來展示兩張效果圖:

     

 

 侵入式:

  侵入式使用的 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";  #這里依據個人目錄而配
}

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

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

    在xhgui的config/config.default.php中,可設置采樣命中次數;
    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的效果圖

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM