<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://jackxiang.com/post//</link>
<title><![CDATA[[有空实践]tideways+xhgui搭建php 7的性能测试环境]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Php/Js/Shell/Go]]></category>
<pubDate>Mon, 25 Mar 2019 05:57:30 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	xhprof不维护了，用的是另一个，格式和xhprof一样,还兼容swoole 协程。<br/>---------------------------------------------------------------<br/>前言<br/>我之前使用的是xhprof+xhgui分析线上环境的性能，然而PHP版本升级到PHP 7之后，xhprof已经不可用，于是改用tideways+xhgui，这实际上也是PHP7下开源方案的唯一选择，有兴趣的可看下参考资料[2]，有详细说明。<br/><br/>本文主要根据参考资料[1]配置，因此会有大量重复的地方，我主要其基础上根据实际生产环境的要求多添加了以下额外配置：<br/><br/>mongodb只绑定到本地<br/>xhgui开启HTTP Basic认证<br/>xhgui在mongodb中只保留最近14天的数据<br/>系统环境<br/>CentOS 7.3 + nginx + mysql + php71<br/><br/>本文假设你的lnmp环境已经可以正常使用，并且是通过源码安装PHP，现在只是需要添加性能测试的功能。如果你不熟悉lnmp环境的配置，推荐使用https://lnmp.org/提供的一键安装包，本文的配置路径均基于该包的默认配置。<br/><br/>安装与配置<br/>分成以下几个部分:<br/><br/>mongodb<br/>tideways<br/>xhgui<br/>应用配置<br/>1.mongodb<br/>安装<br/><br/>#yum install mongodb-server mongodb -y<br/>#pecl install mongodb<br/>启动mongodb服务<br/><br/>#mongod --bind_ip 127.0.0.1 <br/>2.tideways<br/>项目主页：https://github.com/tideways/php-profiler-extension<br/><br/>安装<br/>git clone https://github.com/tideways/php-profiler-extension.git<br/>cd php-profiler-extension<br/>phpize<br/>./configure --with-php-config=`which php-config` <br/>make<br/>sudo make install<br/>配置<br/>编辑php.ini文件，添加:<br/><br/>extension=tideways.so<br/>tideways.auto_prepend_library=0<br/>重启php-fpm，执行以下命令看到tideways的输出表示有生效：<br/><br/>#php -m &#124; grep tide<br/>tideways<br/>3.xhgui<br/>xhgui也是一个网站，最终需要通过web访问。官方版本是英文版，已经不更新了，有很多BUG，这里推荐使用中文版：https://github.com/maxincai/xhgui。<br/><br/>安装(假设在/home/wwwroot/目录下执行如下命令)<br/>$ git clone https://github.com/maxincai/xhgui.git<br/>$ cd xhgui<br/>$ php install.php<br/>配置<br/>1.给数据库添加索引，非必须，但是强烈推荐:<br/><br/>$ mongo<br/> &gt; use xhprof<br/> &gt; db.results.ensureIndex( &#123; &#039;meta.SERVER.REQUEST_TIME&#039; : -1 &#125; )<br/> &gt; db.results.ensureIndex( &#123; &#039;profile.main().wt&#039; : -1 &#125; )<br/> &gt; db.results.ensureIndex( &#123; &#039;profile.main().mu&#039; : -1 &#125; )<br/> &gt; db.results.ensureIndex( &#123; &#039;profile.main().cpu&#039; : -1 &#125; )<br/> &gt; db.results.ensureIndex( &#123; &#039;meta.url&#039; : 1 &#125; )<br/>2.nginx配置(xhgui本身没有安全机制，它捕捉的数据中有敏感数据，因此开放到外网后必须开启HTTP Basic认证）<br/><br/>创建/usr/local/nginx/conf/vhost/xhgui.conf文件，内容如下：<br/><br/>server<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;listen 8888; # 根据实际情况改成自己的端口<br/>&nbsp;&nbsp;&nbsp;&nbsp;server_name 127.0.0.1; #根据实际情况改成自己的域名<br/>&nbsp;&nbsp;&nbsp;&nbsp;index index.html index.htm index.php;<br/>&nbsp;&nbsp;&nbsp;&nbsp;root&nbsp;&nbsp;/home/wwwroot/xhgui/webroot/;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;location ~ &#92;.php<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;auth_basic &quot;xhgui needs authentication&quot;; # 开启HTTP Basic认证<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;auth_basic_user_file htpasswd;&nbsp;&nbsp;# 密码文件<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try_files $uri =404;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_pass&nbsp;&nbsp;unix:/tmp/php-cgi.sock;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_index index.php;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include fastcgi_params;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;location / &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try_files $uri $uri/ /index.php?$uri&amp;$args;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;location ~ .*&#92;.(gif&#124;jpg&#124;jpeg&#124;png&#124;bmp&#124;swf)$ &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expires&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;30d;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;location ~ .*&#92;.(js&#124;css)?$ &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expires&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;30d;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;access_log&nbsp;&nbsp;/home/wwwlogs/xhgui.access.log;<br/>&nbsp;&nbsp;&nbsp;&nbsp;error_log&nbsp;&nbsp;/home/wwwlogs/xhgui.error.log;<br/>&#125;<br/>开启HTTP Basic认证需要生成密码文件htpasswd。假设生成一个tester用户，密码为123456，则执行以下命令：<br/><br/>printf “tester:$(openssl passwd -crypt 123456)&#92;n&quot; &gt;&gt; /usr/local/nginx/conf/htpasswd <br/>生成后记得检查下文件内容，格式内容应该类似如下：<br/><br/>$cat /usr/local/nginx/conf/htpasswd <br/>tester:1qe8kAN82iOyo<br/>完成配置重启，在浏览器中进入http://127.0.0.1:8888，应该能看到界面了，只是此时还没有数据。<br/><br/>3.进一步优化配置<br/><br/>xhgui 默认是按1%采集的，可是如果是排查问题时还是希望能够100%采集会比较方便。进入xhgui源码目录，修改config/config.default.php文件，平时仍然按1%的采样率采样，防止数据增长过快，当想调试时，就在URL中添加debug=1的参数即可。<br/><br/>在config/config.default.php中，找到profiler.enable这里，按如下修改：<br/><br/>&#039;profiler.enable&#039; =&gt; function() &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;// url 中包含debug=1则百分百捕获<br/>&nbsp;&nbsp;&nbsp;&nbsp;if(!empty($_GET[&#039;debug&#039;]))&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return true;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125; else &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 1%采样<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return rand(1, 100) === 42;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&#125;,<br/>如果不删除采集的数据，很快就会发现mongo数据库变得很大。因此推荐配置下mongo数据库，只保留最近14天的数据。<br/><br/>#mongo<br/>&gt; use xhprof<br/>&gt; db.results.ensureIndex( &#123; &quot;meta.request_ts&quot; : 1 &#125;, &#123; expireAfterSeconds : 3600*24*14 &#125; )<br/>如果想手动全部删除，则执行如下命令：<br/><br/>$ mongo<br/>$ use xhprof;<br/>$ db.dropDatabase();<br/>4.应用配置<br/>让应用实现采集，需要修改对应的nginx配置文件，添加：<br/><br/>fastcgi_param TIDEWAYS_SAMPLERATE “100&quot;; #是否采样取决于xhgui的随机数配置和这里的采样率配置，两者必须同时满足，这里简单设置成100，由xhgui去控制<br/>fastcgi_param PHP_VALUE &quot;auto_prepend_file=/home/wwwroot/xhgui/external/header.php&quot;;<br/>完整的nginx示例配置文件如下：<br/><br/>server<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;listen 80; #根据实际情况修改<br/>&nbsp;&nbsp;&nbsp;&nbsp;server_name test.dev; #根据实际情况修改<br/>&nbsp;&nbsp;&nbsp;&nbsp;index index.html index.htm index.php;<br/>&nbsp;&nbsp;&nbsp;&nbsp;root&nbsp;&nbsp;/home/wwwroot/test/web/;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;location ~ &#92;.php<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_pass&nbsp;&nbsp;unix:/tmp/php-cgi.sock;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_index /index.php;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_param TIDEWAYS_SAMPLERATE &quot;100”; # 此处为重点<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_param PHP_VALUE &quot;auto_prepend_file=/home/wwwroot/xhgui/external/header.php”; # 此处为重点<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include fastcgi_params;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;try_files $uri $uri/ @rewrite;<br/>&nbsp;&nbsp;&nbsp;&nbsp;location @rewrite &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/(.*)$ /index.php?_url=/$1;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;location ~ .*&#92;.(gif&#124;jpg&#124;jpeg&#124;png&#124;bmp&#124;swf)$ &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expires&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;30d;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;location ~ .*&#92;.(js&#124;css)?$ &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expires&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;30d;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;access_log&nbsp;&nbsp;/home/wwwlogs/test.access.log;<br/>&nbsp;&nbsp;&nbsp;&nbsp;error_log&nbsp;&nbsp;/home/wwwlogs/test.error.log;<br/>&#125;<br/>最终成功配置并采集到数据的界面<br/><br/><br/>参考<br/>PHP性能被动分析工具之xhgui加tideways的安装实践<br/>Profiling Drupal: XHprof, Uprofiler, Tideways with PHP7 on Linux and Windows<br/>nginx用户认证配置<br/><br/><br/>From:<a href="https://www.cnblogs.com/pheye/p/7717712.html" target="_blank">https://www.cnblogs.com/pheye/p/7717712.html</a>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [有空实践]tideways+xhgui搭建php 7的性能测试环境]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>