Mac安裝PHP性能分析工具之xhgui+xhprof+mongo

 

參考網上很多文章發的都不能用,只能一步一步老老實實安裝了, 還是自己來踩坑吧!方便後來人!

環境

  • xhprof + xhgui
  • php 7.2
  • mongodb
  • mcrypt(裝這玩意兒需要首先安裝libmcrypt,否則甭想成功)

1.安裝mongodb

    brew install mongodb

2. 安裝php 的mongo擴展

wget http://pecl.php.net/get/mongodb-1.4.3.tgz
tar -zxvf  mongodb-1.4.3.tgz
cd mongodb-1.4.3
phpize
./configure --with-php-config=/usr/local/bin/php-config  && make && make install

php -m|grep mongo 查看是否安裝成功

然後別忘了啓動下mongo哦  nohup mongod --bind_ip 127.0.0.1 --port 27017 &

3.安裝php的xhprof擴展(先是安裝了PHP的tideways擴展【.so 文件叫tideways_xhprof.so】github上叫這個 tideways/php-xhprof-extension,發現這玩意兒配合xhgui使用時總是報錯,各種不兼容,可能是我的版本沒配套對?最後果斷放棄tideways,另尋出路安裝了xhprof)

git clone https://github.com/longxinH/xhprof

cd xhprof

./configure --with-php-config=/usr/local/bin/php-config --enable-xhprof

make && make install

php -m | grep xhprof  查看是否安裝成功(記得重啓php-fpm)

4.安裝 xhgui

git clone https://github.com/laynefyc/xhgui-branch.git

mv xhgui-branch xhgui

cd xhgui

最坑的恐怕就在這裏了,網上很多文章直接就開始php install.php這一步了,mmp的!要先執行composer install !!  再php install.php

然後再配置xhgui(extension, profiler.enable, db.host, db.db參數)

cp config.default.php config.php(願意cp一下也行,看着好看!)

vim config.php

return array(
    'debug' => false,
    'mode' => 'development',
    
    'extension' => 'xhprof',
    
    // Can be either mongodb or file.
     
    //'save.handler' => 'file',
    //'save.handler.filename' => dirname(__DIR__) . '/cache/' . 'xhgui.data.' . microtime(true) . '_' . substr(md5($url), 0, 6),
    'save.handler' => 'mongodb', 
    
    // Needed for file save handler. Beware of file locking. You can adujst this file path
    // to reduce locking problems (eg uniqid, time ...)
    //'save.handler.filename' => __DIR__.'/../data/xhgui_'.date('Ymd').'.dat',
    'db.host' => 'mongodb://127.0.0.1:27017',
    'db.db' => 'xhprof',
    
    // Allows you to pass additional options like replicaSet to MongoClient.
    // 'username', 'password' and 'db' (where the user is added)
    'db.options' => array(),
    'templates.path' => dirname(__DIR__) . '/src/templates',
    'date.format' => 'M jS H:i:s',
    'detail.count' => 6,
    'page.limit' => 25,
    
    // Profile 1 in 100 requests.
    // You can return true to profile every request.
    'profiler.enable' => function() { 
          return true;
    },    
    
    'profiler.simple_url' => function($url) {
        return preg_replace('/\=\d+/', '', $url);
    }   
    
);

5. 把xhgui配置成web服務

vim /usr/local/etc/nginx/conf/xhgui.conf

server
{ 
    listen 8888;
    server_name localhost;
    index index.html index.htm index.php;
    root  /projects/webroot/xhgui/webroot/;

    location ~ \.php
    {
        try_files $uri =404;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
        expires      30d;
    }

    location ~ .*\.(js|css)?$ {
        expires      30d;
    }

    access_log  /data/logs/xhgui.access.log;
    error_log  /data/logs/xhgui.error.log;
}

想抓哪個端口的請求,在其server裏的location配置如下兩句:

fastcgi_param TIDEWAYS_SAMPLERATE 100; # 是否採樣取決xhgui的隨機數配置和這裏的採樣率配置,設成100時由xhgui控制
fastcgi_param PHP_VALUE "auto_prepend_file=/projects/webroot/xhgui/external/header.php"; # 配置使用xhgui分析性能

注意:重啓nginx是必要的!

6.由於調用棧等信息都是保存到mongo的(當然你也可以在config裏配置保存到file),在mongo建上索引,優化下查詢:

$ mongo
> use xhprof
> db.results.ensureIndex( { 'meta.SERVER.REQUEST_TIME' : -1 } )
> db.results.ensureIndex( { 'profile.main().wt' : -1 } )
> db.results.ensureIndex( { 'profile.main().mu' : -1 } )
> db.results.ensureIndex( { 'profile.main().cpu' : -1 } )
> db.results.ensureIndex( { 'meta.url' : 1 } )

7.配置php.ini

還需要安裝mcrypt這個東西,否則甭想正確運行

先安裝libmcrypt

wget http://softlayer.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz

再安裝mcrypt

wget http://pecl.php.net/get/mcrypt-1.0.1.tgz

最後配置php.ini

extension=xhprof.so
extension=mongodb.so
extension=mcrypt.so

這個時候就可以瀏覽器訪問localhost:8888試試了!

ok 這個時候應該可以看到頁面了,點擊抓到的請求發現報錯!

經過查看xhgui的error日誌和google之後,發現xhprof有bug需要修復下  參考這個鏈接 https://github.com/perftools/xhgui/issues/221

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章