PHP性能監測的工具介紹 - XHProf -參考自https://jingyan.baidu.com/article/7082dc1c173359e40a89bd95.html


XHProf

這個軟件本是Facebook內部的一個應用工具,2009年3月份開源,為PHP的性能監測提供了很好的工具。官方的介紹中提到:

方法/步驟

 
  1.  

    XHProf

    這個軟件本是Facebook內部的一個應用工具,2009年3月份開源,為PHP的性能監測提供了很好的工具。官方的介紹中提到:

    XHProf is a hierarchical profiler for PHP. It reports function-level call counts and inclusive and exclusive metrics such as wall (elapsed) time, CPU time and memory usage.

    XHProf's light-weight nature and aggregation capabilities make it well suited for collecting "function-level" performance statistics from production environments.

     

    可以先來看看 XHProf 提供的圖形界面的截圖

     

    XHProf的一些特性:

    1、Flat Profile. 提供函數級的匯總信息,比如調用次數、執行時間、內存使用、CPU占用等。

    PHP性能監測的工具介紹 - XHProf
  2.  

    2、Hierarchical Profile。 對每個程序,進行了父級調用和子級調用的分解。

     

    3、Diff Reports(差異報告)。有很多種情況,我們希望能夠對比,比如新版本比舊版本提升了多少速度,兩個版本的差距究竟在哪里。Diff Report 就是這樣的工具,接收兩個輸入,並且分別給出各自的 Flat Profile 和 Hierarchical Profile 報告。

    4、Callgraph View(調用視圖)。性能監測的數據可以繪制成調用視圖,方便我們查看。

    PHP性能監測的工具介紹 - XHProf
  3.  

    5、Memory Profile(內存監控)。這個特性幫助我們了解PHP如何分配和釋放內存。值得注意的是,XHProf並不是嚴格的監測內存的分配和釋放動作,而是計算每個函數進入和退出時的內存狀況,這是一個相對簡單的實現方式,但是基本上也能夠滿足我們日常的監控需求。

    6、如何處理外部文件。XHProf將 include,require,include_once,require_once進來的文件視作是一個 function。

    XHProf目前只支持一個級別的函數追蹤,但是貌似也沒有特別大的影響。

    XHProf的安裝配置

  4.  

    xhprof的安裝配置很簡單,我們首先在 PECL 的網站上下載 源碼包 然后執行安裝過程

    % cd <xhprof_source_directory>/extension/ % phpize % ./configure --with-php-config=<path to php-config> % make % make install % make test

    php.ini file: You can update your php.ini file to automatically load your extension. Add the following to your php.ini file.

    [xhprof] extension=xhprof.so ; ; directory used by default implementation of the iXHProfRuns ; interface (namely, the XHProfRuns_Default class) for storing ; XHProf runs. ; xhprof.output_dir=<directory_for_storing_xhprof_runs>

     

    xhprof的使用也很簡單,只要將需要監控的腳本放在 xhprof_enable() 和 xhprof_disable() 中間,就可以得到相應的結果,同時也提供了一些參數可以讓我們設置是否監控 Memory, CPU 的使用,是否監控PHP內置的函數,從 0.9.2 之后,還可以設置跳過一些特定的函數。

    XHProf 生成的數據,可以用 XHProf UI 來進行簡單的顯示。

    XHProf使用也很簡單,下面是一個官方的例子:

  5.  

    <?php

    function bar($x) {   if ($x > 0) {     bar($x - 1);   } }

    function foo() {   for ($idx = 0; $idx < 2; $idx++) {     bar($idx);     $x = strlen("abc");   } }

    xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

    foo();

    $xhprof_data = xhprof_disable();

    // // Saving the XHProf run // using the default implementation of iXHProfRuns. // include_once "xhprof_lib/utils/xhprof_lib.php"; include_once "xhprof_lib/utils/xhprof_runs.php";

    $xhprof_runs = new XHProfRuns_Default();

    // Save the run under a namespace "xhprof_foo". // // **NOTE**: // By default save_run() will automatically generate a unique // run id for you. [You can override that behavior by passing // a run id (optional arg) to the save_run() method instead.] // $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");

    echo "---------------\n".      "Assuming you have set up the http based UI for \n".      "XHProf at some address, you can view run at \n".      "http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".      "---------------\n";

    ?>

     

     

    xhprof測試結果各參數的解釋:

    • funciton name : 函數名
    • calls: 調用次數
    • Incl. Wall Time (microsec): 函數運行時間(包括子函數)
    • IWall%:函數運行時間(包括子函數)占比
    • Excl. Wall Time(microsec):函數運行時間(不包括子函數)
    • EWall%:函數運行時間(不包括子函數)

    每一項應該不難理解,以項目自帶的sample.php為例,示例中編寫了一個main()函數,main()函數中調用foo()bar()等一些子函數進行了一點字符處理。整個程序運行過程中,main()函數只運行了一次,並且由於main()函數中包括了所有的邏輯,所以main()函數的IWall%占比為100%,但是由於main()函數的功能都是由子函數實現的,因此main()函數的EWall%只有0.3%,而foo()函數完成了主要的工作,EWall%有98.1%。因此在分析更大型的程序時,往往需要根據這幾項指標分別排序,從不同的角度審視性能消耗。


免責聲明!

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



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