非侵入式監控PHP應用性能監控分析

3o4i6u0s2u 8年前發布 | 12K 次閱讀 PHP開發 xhprof

來自: http://www.bo56.com/非侵入式監控php應用性能/

前言

所謂非侵入式監控PHP應用性能,就是不修改現有系統代碼,而對系統進行監控。這樣的系統才能更容易的應用到PHP應用中。這里拋磚引玉,歡迎大家交流。

方案一

如果只是監控每次請求的訪問時間。直接檢測nginx的日志即可。在nginx的日志中有兩個選項。$request_time 和 $upstream_response_time 。 這兩個選項記錄了響應時間。

1、$request_time 指的就是從接受用戶請求的第一個字節到發送完響應數據的時間,即包括接收請求數據時間、程序響應時間、輸出響應數據時間。

2、$upstream_response_time 是指從Nginx向后端(php-cgi)建立連接開始到接受完數據然后關閉連接為止的時間。

如果只是監控后端PHP服務的性能,只要更多的關注 $upstream_response_time 這個選項即可。

方案二

如果還想進一步一個PHP請求處理過程中,具體那部分占用時間比較多,就需要借助xhprof了。xhprof可以生成調用關系圖,一目了然的可以看出那部分占用時間比較多。如下圖(來源于網絡):

下面是完整的搭建步驟:

下載安裝xhprof

1、下載編譯安裝的命令如下:

$wget https://github.com/phacility/xhprof/archive/master.zip
$unzip ./xhprof_master.zip
$cd ./xhprof_master/extension
$/usr/local/php/bin/phpize
$./configure --with-php-config=/usr/local/php/bin/php-config
$make
$make install

注意,我的php是安裝在/usr/local/php目錄。根據您的情況,適當修改上面的路徑。

2、修改配置文件php.ini

$vim /etc/php.ini

底部增加如下內容:

[xhprof]

extension=xhprof.so

xhprof.output_dir=/tmp/xhprof

3、通過下面的命令檢測xhprof是否安裝成功

$/usr/local/php/bin/php -m

如果以上命令輸出內容中有xhprof字樣,說明xhprof擴展安裝成功。

4、拷貝xhprof相關程序到指定目錄

$mkdir -p /www/sites/xhprof
$cp -r ./xhprof_master/xhprof_html /www/sites/xhprof
$cp -r ./xhprof_master/xhprof_lib /www/sites/xhprof

5、修改nginx配置,以便通過url訪問性能數據:在nginx中增加如下代碼:

server {
        listen  8999;
        root    /opt/sites/xhprof/;
        index  index.php index.html;
        location ~ .*\.php$ {
        add_header  Cache-Control "no-cache, no-store, max-age=0, must-revalidate";
        add_header  Pragma  no-cache;
        add_header Access-Control-Allow-Origin *;
        add_header      Via "1.0 xgs-150";
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        include fastcgi.conf;
        fastcgi_connect_timeout 300; 
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        }   
}

6、部署性能數據采集程序把xhprof.php文件部署到 /www/sites/xhprof.php 。xhprof.php文件內容如下:

<?php
define("XH_LOG_PATH", "/tmp/xhprof.log");
$xh_force_disable = false; //設置性能分析是否啟用,設置為true表示關閉。
$max_time = 100; //millisecond

$xh_enable = false;
$start_time = microtime(true);

//這里可以設置需要進行性能分析的url和設置的超時時間。如果指定的url,響應時間超過了設置的超時時間,性能分析數據就會被記錄下來。超時時間的單位為毫秒。
$xh_conf["urls"] = array(
    //url => max_time
    "/i/content/getdetail.json" => 100,
);

function xh_save_data(){
    global $start_time, $xh_force_disable, $xh_enable, $max_time;
    $end_time = microtime(true);
    $cost_time = $end_time - $start_time;
    $cost_time *= 1000; 
    if( $cost_time > $max_time && !$xh_force_disable && $xh_enable ){
        include_once "/www/sites/xhprof/xhprof_lib/utils/xhprof_lib.php";
        include_once "/www/sites/xhprof/xhprof_lib/utils/xhprof_runs.php";
        $xhprof_data = xhprof_disable();
        $objXhprofRun = new XHProfRuns_Default();
        $run_id = $objXhprofRun->save_run($xhprof_data, "xhprof");
        $log_data = "cost_time||$cost_time||run_id||$run_id||request_uri||".$_SERVER["REQUEST_URI"]."\n";
        //高并發下 可能會出現錯亂情況。建議把 const_time run_id request_uri 寫入到數據庫
        file_put_contents(XH_LOG_PATH, $log_data, FILE_APPEND);
    }
}
$xh_request_uri = isset($_SERVER["REQUEST_URI"]) ? $_SERVER["REQUEST_URI"] : "";
$arr_xh_cur_url = explode("?", $xh_request_uri);
$xh_cur_url = $arr_xh_cur_url[0];
if( !$xh_force_disable && isset($xh_conf["urls"][$xh_cur_url]) ){
    $xh_enable = true;
    $max_time = $xh_conf["urls"][$xh_cur_url];
    xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
    register_shutdown_function("xh_save_data"); 
}else{
    $xh_enable = false;
}
?>

把這個代碼文件部署到/www/sites/xhprof.php目錄后,就可以啟用這個文件了。因為我們想對PHP應用代碼不進行代碼侵入,那么我們就只能通過如下幾種方式啟用:

* Nginx/PHP-FPM 方式:

fastcgi_param PHP_VALUE "auto_prepend_file=/www/sites/xhprof.php";

* Apache 方式:

php_value auto_prepend_file "/www/sites/xhprof.php"

* php.ini 方式:

auto_prepend_file="/www/sites/xhprof.php"

注意:如果使用了 opcode 緩存,記得要重啟你的 php 進程。

7、查看性能分析日志

$tail /tmp/xhprof.log

$cost_time||200||run_id||adadfdsadad||request_uri||/i/content/getcontent.json

上面輸出內容中:

cost_time 耗時 200毫秒

run_id 為 adadfdsadad

request_uri 為 /i/content/getcontent.json

8、根據run_id 查看性能分析數據

http://127.0.0.1:8999/xhprof_html/index.php?run=adadfdsadad

查看方式請參考 http://www.cnblogs.com/siqi/p/3790186.html

注意:

1、在正式啟用前,一定要確認不會影響正常的數據輸出。確認輸出內容無異后,再上線。

2、每個url的max_time不要設置的過小。

3、xhprof會影響線上服務的性能,因此最好只在一臺機器上進行監控,或者 修改xhprof.php代碼,對請求進行隨機監控。

 本文由用戶 3o4i6u0s2u 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!