1. 程式人生 > >windows下安裝PHP的擴充套件xhprof

windows下安裝PHP的擴充套件xhprof

1.下載擴充套件:

windows:http://windows.php.net/downloads/pecl/releases/xhprof/0.10.6/

Linux:https://github.com/phacility/xhprof 或http://pecl.php.net/package/xhprof【下載最新版 網上分享的舊版有BUG (後面說)】

注意:

1.1 windows版本一定要下載PHP版本對應的xhprof不然無效

1.2 32位64位最好對應;我電腦是64的 但我下載的是32位的5.4 也能使用;

1.3 Liunx版本有寫好的例子以及圖形顯示程式碼,也下載一份

2.安裝

2.1 解壓下載的windows版

2.2 將windows版的dll檔案放到PHP的擴充套件目錄 修改PHP.ini如下

[xhprof]  
extension=php_xhprof.dll  
; directory used by default implementation of the iXHProfRuns  
; interface (namely, the XHProfRuns_Default class) for storing  
; XHProf runs.  
xhprof.output_dir="D:/phpStudy/PHPTutorial/tmp/xhprof"  
2.3 out_dir 定義輸出檔案的存放位置 我用的phpstudy整合環境就存放在了tmp下

2.4 重啟apache,檢視phpinfo即可(如果下載的版本和PHP版本不一致 重啟也看不見擴充套件的)

3.使用

3.1 解壓下載的Linux版本

3.2 將examples xhprof_html xhprof_lib目錄移到自己的PHP工作目錄並按需求重新命名(我將後兩個檔案移到examples目錄了)

3.3 執行index.php  http://localhost/xhprof/;index.php原始碼如下

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

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

// start profiling
xhprof_enable();

// run program
foo();

// stop profiler
$xhprof_data = xhprof_disable();

// display raw xhprof data for the profiler run
print_r($xhprof_data);


include_once  "./xhprof_lib/utils/xhprof_lib.php";
include_once "./xhprof_lib/utils/xhprof_runs.php";

// save raw data for this profiler run using default
// implementation of iXHProfRuns.
$xhprof_runs = new XHProfRuns_Default();

// save the run under a namespace "xhprof_foo"
$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_html檔案所在目錄地址/index.php?run=$run_id&source=xhprof_foo\n";//xhprof_html檔案所在目錄地址一定給對
3.3.1 關於xhprof_enable函式 提供了兩個引數 xhprof_enable ([ int $flags = 0 [, array $options ]] )

flags 該引數用於為剖析結果新增額外的資訊,該引數的值使用以下巨集,如果需要提供多個值,使用|進行分隔。

XHPROFFLAGSNO_BUILTINS 跳過所有的內建函式

XHPROFFLAGSCPU 新增對CPU使用的分析

XHPROFFLAGSMEMORY 新增對記憶體使用的分析 

注:在Linux環境下,XHPROFFLAGSCPU會造成比較高的系統負載,因此不建議使用,而推薦只使用XHPROF_FLAGS_MEMORY

3.4 檢視分析:http://localhost/xhprof/xhprof_html/index.php?run=5a5ede10a5244&source=xhprof_foo

3.5 點選頁面中間的[View Full Callgraph]可檢視圖形分析結果

4.關於View Full Callgraph

開啟View Full Callgraph可能會出現如下錯誤:

4.1 

failed to execute cmd: " dot -Tpng". stderr: `'dot' 不是內部或外部命令,也不是可執行的程式 或批處理檔案。 '
這是因為本地沒有安裝Graphviz2.38或給定dot命令路徑不對,下載地址http://www.graphviz.org/download/ 或者百度下載http://rj.baidu.com/soft/detail/19179.html?ald

下載之後正常安裝即可 即可安裝路徑 我的安裝路徑是D:\Program Files\Graphviz2.38

4.2 修改xhprof\xhprof_lib\utils\callgraph_utils.php的第110行如下

  //$cmd = " dot -T".$type;
  $cmd = '"D:\Program Files\Graphviz2.38\bin/dot" -T'.$type;
4.3 再次執行可能會報錯
Warning: proc_open(): CreateProcess failed, error code - 267 in D:\phpStudy\PHPTutorial\WWW\xhprof\xhprof_lib\utils\callgraph_utils.php on line 114
failed to execute cmd ""D:\Program Files\Graphviz2.38\bin/dot" -Tpng"
出現這種狀況的原因下載了舊的Liunx版本的UI,在112行修改如下
  //$process = proc_open($cmd, $descriptorspec, $pipes, "/tmp", array());
  $process = proc_open($cmd, $descriptorspec, $pipes,sys_get_temp_dir());

或者直接在D盤根目錄下建立一個tmp檔案即可

5.關於xhGUI

6總結

記得xhprof_html與xhprof_lib在同一目錄,因為xhprof_html包含了xhprof_lib,如更改目錄 記得修改xhprof_html的程式碼;

以上是我在安裝時候遇到一些問題

其它問題 見官方手冊 http://php.net/manual/zh/book.xhprof.php