mac系統PHP 7.1.12安裝xhprof並使用[View Full Callgraph]小記


前幾天從php7.0.x 升級到了php7.2.0版本, 結果裝xhprof沒有找到能支持對應版本的xhprof

於是又安裝了一個php7.1.2的版本(brew install h)

接着安裝xhprof擴展

git clone https://github.com/longxinH/xhprof
cd xhprof/extension/
/usr/local/bin/phpize
./configure --with-php-config=/usr/local/bin//php-config
make && sudo make install

啟用擴展

vim /usr/local/etc/php/7.1/php.in

最后引入擴展,並自定義輸出目錄
[xhprof]
extension = xhprof.so
xhprof.output_dir = /Users/liugx/work/php/xhprof

將 上面下載的 xhprof 文件夾中的這兩個目錄復制一份到  /Users/liugx/work/php/xhprof 目錄下

cd ..
cp -r xhprof_lib/ /Users/liugx/work/php/xhprof/xhprof_lib/
cp -r xhprof_html/ /Users/liugx/work/php/xhprof/xhprof_html/

 

並在/Users/liugx/work/php/xhprof根目錄下添加文件 inject.php,如下:

<?php
/**
 * User: szliugx@gmail.com
 * Date: 17/12/20
 * Time: 上午11:32
 */

//開啟xhprof
xhprof_enable(XHPROF_FLAGS_MEMORY | XHPROF_FLAGS_CPU);

//在程序結束后收集數據
register_shutdown_function(function() {
    $xhprof_data   = xhprof_disable();

    //讓數據收集程序在后台運行
    if (function_exists('fastcgi_finish_request')) {
        fastcgi_finish_request();
    }

    //保存xhprof數據
    $XHPROF_ROOT = '/Users/liugx/work/php/xhprof';
    include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
    include_once $XHPROF_ROOT . "/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 $run_id;
});

 

將 /Users/liugx/work/php/xhprof 目錄設置稱web站點,監控站點nginx配置

server {
       listen 80; 
       server_name xhprof-view.com;

       #root  /Users/liugx/work/php/xhprof;
    
        location / { 
            root   /Users/liugx/work/php/xhprof;
            #try_files $uri $uri/ /index.php?$query_string;
            index  index.php index.html index.htm;
        }   

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #   
        location ~ \.php$ {
            root           /Users/liugx/work/php/xhprof;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            #fastcgi_param PHP_VALUE "auto_prepend_file=/Users/liugx/work/php/xhprof/inject.php"; #(此配置文件中不用加,需要監控哪個站點加上這一行就行)
            include        fastcgi_params;
        }   
}

 

### ⚠ 這樣監控站點和被監控站點的配置(被監控站點nginx配置只需要加一行代碼

fastcgi_param PHP_VALUE "auto_prepend_file=/Users/liugx/work/php/xhprof/inject.php";

)就完了,重啟php-fpm和nginx,訪問被監控站點,訪問http://xhprof-view.com/xhprof_html/ 就能得到一些監控信息列表,如下:

然后點擊一條詳情,輸出如下:

 

展示詳細的硬件消耗和時間消耗信息,如果想更直觀的看,可以已圖片的形式看調用過程情況,點擊頁面中的 [View Full Callgraph]

 

紅色區域就是耗時最多的地方,但是這個功能需要安裝 Graphviz

一開始准備直接 brew install graphviz,結果失敗了,因為安裝時,里面有個依賴webp是Google的源

於是上http://graphviz.org/download/官網,介紹還可以MacPorts 方式安裝,這次安裝成功了

先下載安裝MacPorts,找到系統對應的版本

http://distfiles.macports.org/MacPorts/MacPorts-2.4.0-10.10-Yosemite.pkg

下載安裝完后,終端執行

sudo port -v selfupdate
sudo port install graphviz

安裝完后,如下說明安裝成功並且執行文件已經加入了環境變量

liugx@liugx ~$ dot -V

dot - graphviz version 2.40.1 (20161225.0304)

 

這時,如果點擊[View Full Callgraph]如果還是出現,mac sh: dot: command not found 這樣的信息,直接改/Users/liugx/work/php/xhprof/xhprof_lib/utils/callgraph_utils.php

中的

$cmd = " dot -T".$type; 改成 $cmd = " /opt/local/bin/dot -T".$type;  相當於給了絕對路徑,不怕找錯地方(此方法不推薦)

我是直接建了個鏈接從原文件到目標文件

ln /opt/local/bin/dot /usr/local/bin/dot

 


免責聲明!

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



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