tideways+xhgui搭建php 7的性能测试环境
前言
我之前使用的是xhprof+xhgui分析線上環(huán)境的性能,然而PHP版本升級(jí)到PHP 7之后,xhprof已經(jīng)不可用,于是改用tideways+xhgui,這實(shí)際上也是PHP7下開源方案的唯一選擇,有興趣的可看下參考資料[2],有詳細(xì)說明。
本文主要根據(jù)參考資料[1]配置,因此會(huì)有大量重復(fù)的地方,我主要其基礎(chǔ)上根據(jù)實(shí)際生產(chǎn)環(huán)境的要求多添加了以下額外配置:
- mongodb只綁定到本地
- xhgui開啟HTTP Basic認(rèn)證
- xhgui在mongodb中只保留最近14天的數(shù)據(jù)
系統(tǒng)環(huán)境
CentOS 7.3 + nginx + mysql + php71
本文假設(shè)你的lnmp環(huán)境已經(jīng)可以正常使用,并且是通過源碼安裝PHP,現(xiàn)在只是需要添加性能測(cè)試的功能。如果你不熟悉lnmp環(huán)境的配置,推薦使用https://lnmp.org/提供的一鍵安裝包,本文的配置路徑均基于該包的默認(rèn)配置。
安裝與配置
分成以下幾個(gè)部分:
1.mongodb
安裝
#yum install mongodb-server mongodb -y #pecl install mongodb啟動(dòng)mongodb服務(wù)
#mongod --bind_ip 127.0.0.12.tideways
項(xiàng)目主頁(yè):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,執(zhí)行以下命令看到tideways的輸出表示有生效:
#php -m | grep tide tideways3.xhgui
xhgui也是一個(gè)網(wǎng)站,最終需要通過web訪問。官方版本是英文版,已經(jīng)不更新了,有很多BUG,這里推薦使用中文版:https://github.com/maxincai/xhgui。
安裝(假設(shè)在/home/wwwroot/目錄下執(zhí)行如下命令)
$ git clone https://github.com/maxincai/xhgui.git $ cd xhgui $ php install.php配置
1.給數(shù)據(jù)庫(kù)添加索引,非必須,但是強(qiáng)烈推薦:
$ 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本身沒有安全機(jī)制,它捕捉的數(shù)據(jù)中有敏感數(shù)據(jù),因此開放到外網(wǎng)后必須開啟HTTP Basic認(rèn)證)
創(chuàng)建/usr/local/nginx/conf/vhost/xhgui.conf文件,內(nèi)容如下:
server {listen 8888; # 根據(jù)實(shí)際情況改成自己的端口server_name 127.0.0.1; #根據(jù)實(shí)際情況改成自己的域名index index.html index.htm index.php;root /home/wwwroot/xhgui/webroot/;location ~ \.php{auth_basic "xhgui needs authentication"; # 開啟HTTP Basic認(rèn)證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認(rèn)證需要生成密碼文件htpasswd。假設(shè)生成一個(gè)tester用戶,密碼為123456,則執(zhí)行以下命令:
printf “tester:$(openssl passwd -crypt 123456)\n" >> /usr/local/nginx/conf/htpasswd生成后記得檢查下文件內(nèi)容,格式內(nèi)容應(yīng)該類似如下:
$cat /usr/local/nginx/conf/htpasswd tester:1qe8kAN82iOyo完成配置重啟,在瀏覽器中進(jìn)入http://127.0.0.1:8888,應(yīng)該能看到界面了,只是此時(shí)還沒有數(shù)據(jù)。
3.進(jìn)一步優(yōu)化配置
xhgui 默認(rèn)是按1%采集的,可是如果是排查問題時(shí)還是希望能夠100%采集會(huì)比較方便。進(jìn)入xhgui源碼目錄,修改config/config.default.php文件,平時(shí)仍然按1%的采樣率采樣,防止數(shù)據(jù)增長(zhǎng)過快,當(dāng)想調(diào)試時(shí),就在URL中添加debug=1的參數(shù)即可。
在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;} },如果不刪除采集的數(shù)據(jù),很快就會(huì)發(fā)現(xiàn)mongo數(shù)據(jù)庫(kù)變得很大。因此推薦配置下mongo數(shù)據(jù)庫(kù),只保留最近14天的數(shù)據(jù)。
#mongo > use xhprof > db.results.ensureIndex( { "meta.request_ts" : 1 }, { expireAfterSeconds : 3600*24*14 } )如果想手動(dòng)全部刪除,則執(zhí)行如下命令:
$ mongo $ use xhprof; $ db.dropDatabase();4.應(yīng)用配置
讓應(yīng)用實(shí)現(xiàn)采集,需要修改對(duì)應(yīng)的nginx配置文件,添加:
fastcgi_param TIDEWAYS_SAMPLERATE “100"; #是否采樣取決于xhgui的隨機(jī)數(shù)配置和這里的采樣率配置,兩者必須同時(shí)滿足,這里簡(jiǎn)單設(shè)置成100,由xhgui去控制 fastcgi_param PHP_VALUE "auto_prepend_file=/home/wwwroot/xhgui/external/header.php";完整的nginx示例配置文件如下:
server {listen 80; #根據(jù)實(shí)際情況修改server_name test.dev; #根據(jù)實(shí)際情況修改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”; # 此處為重點(diǎn)fastcgi_param PHP_VALUE "auto_prepend_file=/home/wwwroot/xhgui/external/header.php”; # 此處為重點(diǎn)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; }最終成功配置并采集到數(shù)據(jù)的界面
參考
轉(zhuǎn)載于:https://www.cnblogs.com/pheye/p/7717712.html
總結(jié)
以上是生活随笔為你收集整理的tideways+xhgui搭建php 7的性能测试环境的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 纯CSS打造进度条
- 下一篇: PHP命令空间namespace及use