欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長(zhǎng)資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

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

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

          介紹

          1 背景

          • PHP的xhprof擴(kuò)展Facebook不再進(jìn)行更新和維護(hù),因?yàn)镕aceboo已經(jīng)全面使用HHVM,不再使用PHP zend引擎。

          • xhprof不支持新版本的PHP(PHP7),tideways擴(kuò)展是從xhprof項(xiàng)目fork下來(lái)繼續(xù)進(jìn)行維護(hù)的,目前支持PHP 7.2, 7.1, 7.0, 5.6 and 5.5 。

          • tideways是開(kāi)源項(xiàng)目,它收費(fèi)的只是UI服務(wù),其實(shí) xhgui完全可以滿足我們?nèi)粘5男枨?/p>

          2 功能

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

          3 優(yōu)點(diǎn)

          • tideways是一個(gè)PHP擴(kuò)展,結(jié)合x(chóng)hgui,無(wú)需在PHP代碼中進(jìn)行埋點(diǎn)來(lái)監(jiān)控代碼

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

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

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

          4 缺點(diǎn)

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

          5 實(shí)現(xiàn)原理

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

          應(yīng)用

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

          侵入式指的是在代碼中添加代碼,侵入式使用的是默認(rèn)的ui;

          非侵入式指的是在不對(duì)代碼做任何改動(dòng),通過(guò)改動(dòng)nginx/apache實(shí)現(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也會(huì)去tmp目錄下查找 .xhprof文件

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

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

          將此存儲(chǔ)庫(kù)中的xhprof_libxhprof_html目錄安裝到您的Web文件夾中,并導(dǎo)航xhprof_html/index.php 以查看跟蹤列表。

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

          安裝 Callgraph

          Callgraph 實(shí)際由三個(gè)工具組合而成。

          一個(gè)是用于生成 C 函數(shù)調(diào)用樹(shù)的 cflow 或者 calltree,下文主要介紹 cflow。

          一個(gè)處理 dot 文本圖形語(yǔ)言的工具,由 graphviz 提升。

          一個(gè)用于把 C 函數(shù)調(diào)用樹(shù)轉(zhuǎn)換為 dot 格式的腳本:tree2dotx

          以 Ubuntu 為例,分別安裝它們:

          sudo apt-get install cflow graphviz

          接下來(lái)安裝 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}

          接下來(lái)展示兩張效果圖:

          php7如何使用xhprof測(cè)試php性能?(方法介紹) php7如何使用xhprof測(cè)試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ù)個(gè)人目錄而配 }

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

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

          在xhgui的config/config.default.php中,可設(shè)置采樣命中次數(shù);

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

          '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測(cè)試php性能?(方法介紹)

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

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

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

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