亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    php7如何使用xhprof測試php性能?(方法介紹)

    php7如何使用xhprof測試php性能?(方法介紹)

    介紹

    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服務(wù),其實 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擴展,結(jié)合xhgui,無需在PHP代碼中進行埋點來監(jiān)控代碼

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

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

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

    4 缺點

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

    5 實現(xiàn)原理

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

    應(yīng)用

    接下來介紹兩種應(yīng)用方式:侵入式非侵入式

    侵入式指的是在代碼中添加代碼,侵入式使用的是默認(rè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 目錄下,默認(rèn)UI也會去tmp目錄下查找 .xhprof文件

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

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

    將此存儲庫中的xhprof_libxhprof_html目錄安裝到您的Web文件夾中,并導(dǎo)航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,這里都默認(rèn)安裝到 /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è)置采樣命中次數(shù);

    return rand(1, 100) === 42; 為1%的采樣率,改成return True;則標(biāo)識每次都采樣

    '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服務(wù)器的配置

    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性能?(方法介紹)

    相關(guān)學(xué)習(xí)推薦:PHP編程從入門到精通

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號