phpÇáÁ¿¼¶µÄÐÔÄÜ·ÖÎö¹¤¾ßxhprofµÄ°²×°Ê¹ÓÃ

5年以前  |  阅读数:212 次  |  编程语言:PHP 

Ò»¡¢Ç°ÑÔ

¡¡¡¡ÓÐÓõĶ«Î÷»¹ÊǼǼÏÂÀ´°É£¬Ò²*½±aÒÔºoµÄ²eѯ£»Õa´Î¼Ç¼һÏÂxhprofµÄ°²×°Ê¹Óã»

¡¡¡¡xhprofÊÇfacebook¿ªÔ´³oÀ´µÄÒ»¸ophpÇaÁ¿¼¶µÄÐÔÄÜ*ÖÎo¹¤¾ß£¬¸uXdebugÀaËÆ£¬µ«ÐÔÄÜ¿ªÏu¸uµÍ£¬

¡¡¡¡»¹¿ÉÒÔÓÃÔÚÉu²u»¾³ÖУ¬Ò²¿ÉÒÔÓɳÌÐo¿ª ¹ØÀ´¿ØÖÆÊÇñ½øÐÐprofile¡£

¶þ¡¢°²×°


      wget http://pecl.php.net/get/xhprof-0.9.3.tgz 
      tar zxf xhprof-0.9.3.tgz 
      cd xhprof-0.9.3/extension
      /usr/bin/phpize 
     (php°ae±¾°²×°ºoÉu³ÉµÄphpizeÎļþ£¬¿É¸u¾Ýphpinfo²e¿´£¬ËuÒÔphp°ae±¾²»Í¬£¬Éu³ÉµÄphpizeÒ²²»Í¬£¬´Ë²½ÖeÖ÷ÒªÉu³ÉconfigureÎļþ)
      ./configure ¨Cwith-php-config=/usr/bin/php-config 
     (php-configµÄÂ*¾¶£¬Ò²ÊÇphp°²×°ºoÉu³ÉµÄÎļþ) 
      make 
      sudo make install 

(»a×Ô¶¯½«Éu³ÉµÄÀ(C)Õ¹Îļþ¿½±´µ½À(C)չĿ¼ÖÐ/usr/lib64/php/modules)

µ±È»¾ßÌaµÄphpÎļþµÄĿ¼£¬Ã¿¸oÈ˲»¾¡Ïaͬ£¬¿É¸u¾Ýphpinfo²eѯ

Èý¡¢php.iniÅaÖÃ

¸u¾ÝphpinfoÕÒµ½ extension_dirµÄĿ¼
£¨/etc/php.d/xhprof.ini£(C)

Ìi¼ÓÒ»ÏÂÄÚÈÝ£º


    extension=xhprof.so
    xhprof.output_dir=/tmp/xhprof //xhprofµÄ*ÖÎoÈÕÖ¾

*ËÄ¡¢ÖØÆoþÎñ**


     sudo /etc/init.d/http restart

²e¿´phpinfoÊÇ*ñ°²×°³É¹¦

Îa¡¢Ê¹Óý¨


    ¿ªÍ*£º
    xhprof_enable(); //¿ªÆo¼a²a 
    //xhprof_enable(XHPROF_FLAGS_NO_BUILTINS); ²»¼Ç¼ÄÚÖõĺ¯Êý 
    //xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); ͬʱ*ÖÎoCPUºÍMemµÄ¿ªÏu 

    //Òª²aÊԵĴuÂe
    ...
    ...
    ...

    ½aβ£º
    $xhprof_data = xhprof_disable(); //Í£Ö¹¼a²a£¬*µ»ØÔËÐÐÊý¾Ý
    $xhprof_root = '/£¨xhprofµÄÐeÄaÖ÷»uĿ¼£(C)/'; 
     //ÒýÈeµ±³o°²×°µ½xhprofÐeÄaÖ÷»uĿ¼ÖеÄÎļþ
    include_once $xhprof_root."xhprof_lib/utils/xhprof_lib.php"; 
    include_once $xhprof_root."xhprof_lib/utils/xhprof_runs.php";  
    $xhprof_runs = new XHProfRuns_Default();  
    $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof");
    echo '<a href="http://£¨xhprofµÄÐeÄaÖ÷»uÓoÃu£(C)/xhprof_html/index.php?run='.$run_id.'&source;=xhprof" target="_blank">xhprofͳ¼Æ</a>'; 

¡¡¡¡ÉϱߵĴuÂeʹÓÃÁË£¬¸øxhprofÉeÖÃÐeÄaÖ÷»uµÄ½¨¡£

¡¡¡¡°ÑÔ´Âe°uÖÐµÄ xhprof_html ºÍ xhprof_lib Îļþ¼Ð¿½±´µ½×Ô¼º½¨Á¢µÄÐeÄaĿ¼ÖÐ

¡¡¡¡cp -r xhprof_html xhprof_lib /xxx/xhprof/ £¨´Ë´¦Ä¿µÄÊǽ¨Á¢Êý¾ÝÖÎoĿ¼£¬¿É½«´ËĿ¼ÅaÖóÉÐeÄaÖ÷»uÃÎÊ£(C)

¡¡¡¡ÔËÐкo£¬Í³¼Æµa»÷*µ»ØµÄ xhprofͳ¼Æ Á´½Ó£¬¼´¿É¡£

Áu¡¢×¢ÒaÎÊÌaÒÔ¼°Ãu´Ê½aÊÍ

¡¡¡¡ÔÚÏÔʾµÄͳ¼ÆÒ³ÃaeÖУ¬µa[View Full Callgraph]ͼÐλ¯ÏÔʾ£¨×i´oµÄÐÔÄÜÎÊÌa»aÓúiÉ«±e³o£¬Æa´ÎÊÇ»ÆÉ«£(C)£»

¡¡¡¡µa»÷ºo£¬¿ÉÄÜÌaʾ´iÎoÏuÏ¢£¬Ö´ÐÐÒÔÏÂÃuÁi¼´¿É


     yum install -y graphviz
     yum install graphviz-gd

¡¡¡¡Ãu´Ê½aÊÍ


    Function Name º¯ÊýÃu
     Calls µ÷ÓôÎÊý
     Calls% µ÷ÓðÙ*Ö±È
     Incl. Wall Time (microsec) µ÷ÓõİuÀ¨×Óº¯ÊýËuÓл¨*Ñʱ¼a ÒÔ΢ÃeËa(Ò»°ÙÍo*ÖÖ®Ò»Ãe)
     IWall% µ÷ÓõİuÀ¨×Óº¯ÊýËuÓл¨*Ñʱ¼aµÄ°Ù*Ö±È
     Excl. Wall Time (microsec) º¯ÊýÖ´Ðб¾Éi»¨*ѵÄʱ¼a£¬²»°uÀ¨×ÓÊ÷Ö´ÐÐʱ¼a,ÒÔ΢ÃeËa(Ò»°ÙÍo*ÖÖ®Ò»Ãe)
     EWall% º¯ÊýÖ´Ðб¾Éi»¨*ѵÄʱ¼aµÄ°Ù*ֱȣ¬²»°uÀ¨×ÓÊ÷Ö´ÐÐʱ¼a
     Incl. CPU(microsecs) µ÷ÓõİuÀ¨×Óº¯ÊýËuÓл¨*ѵÄcpuʱ¼a¡£¼oIncl. Wall Time¼´ÎªµÈ´ýcpuµÄʱ¼a
     ¼oExcl. Wall Time¼´ÎªµÈ´ýcpuµÄʱ¼a
     ICpu% Incl. CPU(microsecs)µÄ°Ù*Ö±È
     Excl. CPU(microsec) º¯ÊýÖ´Ðб¾Éi»¨*ѵÄcpuʱ¼a£¬²»°uÀ¨×ÓÊ÷Ö´ÐÐʱ¼a,ÒÔ΢ÃeËa(Ò»°ÙÍo*ÖÖ®Ò»Ãe)¡£
     ECPU% Excl. CPU(microsec)µÄ°Ù*Ö±È
     Incl.MemUse(bytes) °uÀ¨×Óº¯ÊýÖ´ÐÐʹÓõÄÄÚ´ae¡£
     IMemUse% Incl.MemUse(bytes)µÄ°Ù*Ö±È
     Excl.MemUse(bytes) º¯ÊýÖ´Ðб¾ÉiÄÚ´ae,ÒÔ×Ö½ÚËa
     EMemUse% Excl.MemUse(bytes)µÄ°Ù*Ö±È
     Incl.PeakMemUse(bytes) Incl.MemUseµÄ*aÖµ
     IPeakMemUse% Incl.PeakMemUse(bytes) µÄ*aÖµ°Ù*Ö±È
     Excl.PeakMemUse(bytes) Excl.MemUseµÄ*aÖµ
     EPeakMemUse% EMemUse% *aÖµ°Ù*Ö±È

*xhprofµÄ°²×°Óe¼oÒ×Óè**

xhprofÊÇFacebook¿ªÔ´µÄÇaÁ¿¼¶PHPÐÔÄÜÖÎo¹¤¾ß£¬Linux»¾³Ï¿ÉÒÔͨ¹ýpeclÖ±½Ó°²×°£¬±ÈÈçÔÚUbuntuϽoÐe3ÐÐÖ¸Ái


    pecl install xhprof-beta
    echo "extension=xhprof.so" > /etc/php5/fpm/conf.d/xhprof.ini
    service php5-fpm restart

Ö®ºo¿ÉÒÔͨ¹ýphpinfo()¼i²eÀ(C)Õ¹ÊÇ*ñÒѾ­¼ÓÔØ¡£

¾ßÌaÈçºÎʹÓÃÄØ£¬xhprofÏiÄ¿ÖÐÒѾ­Ìa¹(C)ÁËʾÀýÒÔ¼°¼oÒ×µÄUI£¬ÏÂÔØxhprofÏiÄ¿µ½webþÎñÆ÷£¬¼ÙÉe¿ÉÒÔͨ¹ýhttp://localhost/xhprof/ÃÎÊ£¬ÄÇôÃÎÊhttp://localhost/xhprof/examples/sample.php¿ÉÒÔ¿´µ½Ò»Ð(C)Êa³o£¬²¢ÇÒÌaʾͨ¹ýÃÎÊhttp:///index.php?run=XXX&source;=xhprof_foo²e¿´½a¹u¡£½ÓÏÂÀ´*ÃÎÊhttp://localhost/xhprof/xhprof_html/¾Í¿ÉÒÔ¿´µ½ÒѾ­±£´aeµÄ½a¹u£¬ÁгoÁËËuÓк¯ÊýµÄµ÷ÓÃÒÔ¼°ËuÏuºÄµÄʱ¼a¡£

ÖÎoÒ»ÏÂʾÀý´uÂesample.php£¬¹Ø¼u²¿ÖÖ»ÓÐ2ÐУº


    //¿ªÆoxhprof²¢¿ªÊ¼¼Ç¼
    xhprof_enable();
    //ÔËÐÐÒ»Ð(C)º¯Êý
    foo();
    //Í£Ö¹¼Ç¼²¢È¡µ½½a¹u
    $xhprof_data = xhprof_disable();

$xhprof_dataÖмǼÁ˳ÌÐoµ¥²½ÔËÐйý³ÌÖÐËuÓеĺ¯Êýµ÷ÓÃʱ¼a¼°CPUÄÚ´aeÏuºÄµÈ£¬¾ßÌa¼Ç¼ÄÄÐ(C)Ö¸±e¿ÉÒÔͨ¹ýxhprof_enableµÄÈe¿Ú²ÎÊý¿ØÖÆ£¬Ö®ºoµÄ´¦ÀiÒѾ­ÓexhprofÀ(C)Õ¹Î޹أ¬´oÖÂÊDZaдÁËÒ»¸o´ae´¢ÀaXHProfRuns_Default£¬½«$xhprof_dataÐoÁл¯²¢±£´aeµ½Ä³¸oĿ¼£¬¿ÉÒÔͨ¹ýXHProfRuns_Default(DIR)½«½a¹uÊa³oµ½µ±Ç°Ä¿Â¼£¬Èç¹u²»Ö¸¶¨Ôo»a¶ÁÈ¡php.iniÅaÖÃÎļþÖеÄxhprof.output_dir£¬ÈÔȻûÓÐÖ¸¶¨Ôo»aÊa³oµ½/tmp¡£

xhprof_html/index.php½«¼Ç¼µÄ½a¹uÕuÀi²¢¿ÉÊÓ»¯£¬Ä¬ÈϵÄUIÀiÁгoÁË£º
•funciton name £º º¯ÊýÃu
•calls: µ÷ÓôÎÊý
•Incl. Wall Time (microsec)£º º¯ÊýÔËÐÐʱ¼a£¨°uÀ¨×Óº¯Êý£(C)
•IWall%£ºº¯ÊýÔËÐÐʱ¼a£¨°uÀ¨×Óº¯Êý£(C)Õ¼±È
•Excl. Wall Time(microsec)£ºº¯ÊýÔËÐÐʱ¼a£¨²»°uÀ¨×Óº¯Êý£(C)
•EWall%£ºº¯ÊýÔËÐÐʱ¼a£¨²»°uÀ¨×Óº¯Êý£(C)

ÿһÏiÓ¦¸Ã²»ÄÑÀi½a£¬ÒÔÏiÄ¿×Ô´øµÄsample.phpΪÀý£¬Ê¾ÀýÖбaдÁËÒ»¸omain()º¯Êý£¬main()º¯ÊýÖе÷ÓÃfoo()¡¢bar()µÈÒ»Ð(C)×Óº¯Êý½øÐÐÁËÒ»µa×Öu´¦Ài¡£Õu¸o³ÌÐoÔËÐйý³ÌÖУ¬main()º¯ÊýÖ»ÔËÐÐÁËÒ»´Î£¬²¢ÇÒÓÉÓÚmain()º¯ÊýÖаuÀ¨ÁËËuÓеÄÂß¼­£¬ËuÒÔmain()º¯ÊýµÄIWall%Õ¼±ÈΪ100%£¬µ«ÊÇÓÉÓÚmain()º¯ÊýµÄ¹¦Äܶ¼ÊÇÓÉ×Óº¯ÊýʵÏֵģ¬Òo´Ëmain()º¯ÊýµÄEWall%Ö»ÓÐ0.3%£¬¶øfoo()º¯ÊýÍe³ÉÁËÖ÷ÒªµÄ¹¤×÷£¬EWall%ÓÐ98.1%¡£Òo´ËÔÚÖÎo¸u´oÐ͵ijÌÐoʱ£¬ÍuÍuÐeÒª¸u¾ÝÕa¼¸ÏiÖ¸±e*Ö±ðÅÅÐo£¬´Ó²»Í¬µÄ½Ç¶ÈÉoÊÓÐÔÄÜÏuºÄ¡£

ÔÚxhprof_html/index.phpÖл¹¿ÉÒÔ¿´µ½[View Full Callgraph]Á´½Ó£¬µa»÷ºo¿ÉÒÔ»aeÖƳoÒ»ÕÅ¿ÉÊÓ»¯µÄÐÔÄÜ*ÖÎoͼ£¬Èç¹uµa»÷ºo±¨´iµÄ»°£¬¿ÉÄÜÊÇȱÉÙÒÀÀµgraphviz£¬ubuntu¿ÉÒÔͨ¹ýapt°²×°
apt-get install graphviz

*¸uºÃµÄ×¢Èe½Ê½**

Á˽aÁËÉÏÃaeÕaÐ(C)£¬Æaʵ¾ÍÒѾ­¿ÉÒÔ½«xhprofÕuºÏµ½ÈκÎÎÒÃÇÒÑÓеÄÏiÄ¿ÖÐÈ¥ÁË¡£Ä¿Ç°´o²¿*ÖMVC¿o¼Ü¶¼ÓÐΨһµÄÈe¿ÚÎļþ£¬Ö»ÐeÒªÔÚÈe¿ÚÎļþµÄ¿ªÊ¼´¦×¢ÈexhprofµÄÂß¼­


    //¿ªÆoxhprof
    xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);
    //ÔÚ³ÌÐo½aÊøºoÊÕ¼¯Êý¾Ý
    register_shutdown_function(function() {
      $xhprof_data    = xhprof_disable();

      //ÈÃÊý¾ÝÊÕ¼¯³ÌÐoÔÚºǫÔËÐÐ
      if (function_exists('fastcgi_finish_request')) {
        fastcgi_finish_request();
      }

      //±£´aexhprofÊý¾Ý
      ...
    });

µ«ÊÇÕaÑuÃa²»ÁËÒªÐÞ¸ÄÏiÄ¿µÄÔ´´uÂe£¬Æaʵphp±¾Éi¾ÍÌa¹(C)Á˸uºÃµÄ×¢Èe*½Ê½£¬±ÈÈ罫ÉÏÊoÂß¼­±£´aeΪ/opt/inject.php£¬È»ºoÐÞ¸Äphp fpmÅaÖÃÎļþ


    vi /etc/php5/fpm/php.ini

ÐÞ¸Äauto_prepend_fileÅaÖÃ


    auto_prepend_file = /opt/inject.php

ÕaÑuËuÓеÄphp-fpmÇeÇoµÄphpÎļþÇ°¶¼»a×Ô¶¯×¢Èe/opt/inject.phpÎļþ

Èç¹uʹÓÃNginxµÄ»°£¬»¹¿ÉÒÔͨ¹ýNginxµÄÅaÖÃÎļþÉeÖã¬ÕaÑuÇÖÈeÐÔ¸uС£¬²¢ÇÒ¿ÉÒÔʵÏÖ»uÓÚÕ¾µaµÄ×¢Èe¡£


    fastcgi_param PHP_VALUE "auto_prepend_file=/opt/inject.php";
 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例