tideways+xhgui搭建php 7的性能测试环境
前言
我之前使用的是xhprof+xhgui分析線上環境的性能,然而PHP版本升級到PHP 7之后,xhprof已經不可用,于是改用tideways+xhgui,這實際上也是PHP7下開源方案的唯一選擇,有興趣的可看下參考資料[2],有詳細說明。
本文主要根據參考資料[1]配置,因此會有大量重復的地方,我主要其基礎上根據實際生產環境的要求多添加了以下額外配置:
- mongodb只綁定到本地
- xhgui開啟HTTP Basic認證
- xhgui在mongodb中只保留最近14天的數據
系統環境
CentOS 7.3 + nginx + mysql + php71
本文假設你的lnmp環境已經可以正常使用,并且是通過源碼安裝PHP,現在只是需要添加性能測試的功能。如果你不熟悉lnmp環境的配置,推薦使用https://lnmp.org/提供的一鍵安裝包,本文的配置路徑均基于該包的默認配置。
安裝與配置
分成以下幾個部分:
1.mongodb
安裝
#yum install mongodb-server mongodb -y #pecl install mongodb啟動mongodb服務
#mongod --bind_ip 127.0.0.12.tideways
項目主頁:https://github.com/tideways/php-profiler-extension
安裝
git clone https://github.com/tideways/php-profiler-extension.git cd php-profiler-extension phpize ./configure --with-php-config=`which php-config` make sudo make install配置
編輯php.ini文件,添加:
extension=tideways.so tideways.auto_prepend_library=0重啟php-fpm,執行以下命令看到tideways的輸出表示有生效:
#php -m | grep tide tideways3.xhgui
xhgui也是一個網站,最終需要通過web訪問。官方版本是英文版,已經不更新了,有很多BUG,這里推薦使用中文版:https://github.com/maxincai/xhgui。
安裝(假設在/home/wwwroot/目錄下執行如下命令)
$ git clone https://github.com/maxincai/xhgui.git $ cd xhgui $ php install.php配置
1.給數據庫添加索引,非必須,但是強烈推薦:
$ 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 } )2.nginx配置(xhgui本身沒有安全機制,它捕捉的數據中有敏感數據,因此開放到外網后必須開啟HTTP Basic認證)
創建/usr/local/nginx/conf/vhost/xhgui.conf文件,內容如下:
server {listen 8888; # 根據實際情況改成自己的端口server_name 127.0.0.1; #根據實際情況改成自己的域名index index.html index.htm index.php;root /home/wwwroot/xhgui/webroot/;location ~ \.php{auth_basic "xhgui needs authentication"; # 開啟HTTP Basic認證auth_basic_user_file htpasswd; # 密碼文件try_files $uri =404;fastcgi_pass unix:/tmp/php-cgi.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}location / {try_files $uri $uri/ /index.php?$uri&$args;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {expires 30d;}location ~ .*\.(js|css)?$ {expires 30d;}access_log /home/wwwlogs/xhgui.access.log;error_log /home/wwwlogs/xhgui.error.log; }開啟HTTP Basic認證需要生成密碼文件htpasswd。假設生成一個tester用戶,密碼為123456,則執行以下命令:
printf “tester:$(openssl passwd -crypt 123456)\n" >> /usr/local/nginx/conf/htpasswd生成后記得檢查下文件內容,格式內容應該類似如下:
$cat /usr/local/nginx/conf/htpasswd tester:1qe8kAN82iOyo完成配置重啟,在瀏覽器中進入http://127.0.0.1:8888,應該能看到界面了,只是此時還沒有數據。
3.進一步優化配置
xhgui 默認是按1%采集的,可是如果是排查問題時還是希望能夠100%采集會比較方便。進入xhgui源碼目錄,修改config/config.default.php文件,平時仍然按1%的采樣率采樣,防止數據增長過快,當想調試時,就在URL中添加debug=1的參數即可。
在config/config.default.php中,找到profiler.enable這里,按如下修改:
'profiler.enable' => function() {// url 中包含debug=1則百分百捕獲if(!empty($_GET['debug'])){return true;} else {// 1%采樣return rand(1, 100) === 42;} },如果不刪除采集的數據,很快就會發現mongo數據庫變得很大。因此推薦配置下mongo數據庫,只保留最近14天的數據。
#mongo > use xhprof > db.results.ensureIndex( { "meta.request_ts" : 1 }, { expireAfterSeconds : 3600*24*14 } )如果想手動全部刪除,則執行如下命令:
$ mongo $ use xhprof; $ db.dropDatabase();4.應用配置
讓應用實現采集,需要修改對應的nginx配置文件,添加:
fastcgi_param TIDEWAYS_SAMPLERATE “100"; #是否采樣取決于xhgui的隨機數配置和這里的采樣率配置,兩者必須同時滿足,這里簡單設置成100,由xhgui去控制 fastcgi_param PHP_VALUE "auto_prepend_file=/home/wwwroot/xhgui/external/header.php";完整的nginx示例配置文件如下:
server {listen 80; #根據實際情況修改server_name test.dev; #根據實際情況修改index index.html index.htm index.php;root /home/wwwroot/test/web/;location ~ \.php{fastcgi_pass unix:/tmp/php-cgi.sock;fastcgi_index /index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param TIDEWAYS_SAMPLERATE "100”; # 此處為重點fastcgi_param PHP_VALUE "auto_prepend_file=/home/wwwroot/xhgui/external/header.php”; # 此處為重點include fastcgi_params;}try_files $uri $uri/ @rewrite;location @rewrite {rewrite ^/(.*)$ /index.php?_url=/$1;}location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {expires 30d;}location ~ .*\.(js|css)?$ {expires 30d;}access_log /home/wwwlogs/test.access.log;error_log /home/wwwlogs/test.error.log; }最終成功配置并采集到數據的界面
參考
轉載于:https://www.cnblogs.com/pheye/p/7717712.html
總結
以上是生活随笔為你收集整理的tideways+xhgui搭建php 7的性能测试环境的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 纯CSS打造进度条
- 下一篇: 洛谷 P1013 进制位