您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關如何在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)原理
應用
接下來介紹兩種應用方式:侵入式和非侵入式
侵入式指的是在代碼中添加代碼,侵入式使用的是默認的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_lib
和xhprof_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}
接下來展示兩張效果圖:
侵入式:
侵入式使用的 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 > 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的效果圖
看完上述內(nèi)容,你們對如何在php7中使用xhprof測試php性能?有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注億速云行業(yè)資訊頻道,感謝大家的支持。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。