php性能分析工具tideways+php-monitor安装
历史版本介绍
tideways是对原始Xhprof扩展的一个重写,专门针对php7进行了优化,目前版本到5.0.4,但是推荐使用4.1.7版本,因为4.1.7版本以上去掉了sql分析功能,将sql的分析作为了saas收费功能,扩展名字也变成了tideways_xhprof.so,但是4.1.7版本还可以使用,缺点就是只能支持到php7.3。
为什么用tideways+php-monitor
tideways是一个php扩展,可以收集函数调用的次数、执行时间耗时、消耗的CPU时间,使用的内存等等,还可以查看sql语句执行的情况。那么为了方便我们查看这些数据,通常需要把这些数据存储到数据库中,通过友好的UI页面去查看,成熟的工具已经有很多,比如xhgui、php-monitor等等,这篇文字中使用的是php-monitor。php-monitor支持三种数据存储方式:mysql,mongodb和sqlite,这篇文章中我们选择使用mongodb作为数据存储的容器。
安装
-
安装mongodb
仅以最小化安装mongo,生产环境还是需要依照官方教程把mongo的和其相关工具安装全
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/5.0/x86_64/RPMS/mongodb-org-server-5.0.2-1.el7.x86_64.rpm
wget https://repo.mongodb.org/yum/redhat/7/mongodb-org/5.0/x86_64/RPMS/mongodb-org-shell-5.0.2-1.el7.x86_64.rpm
rpm -ivh mongodb-org-server-5.0.2-1.el7.x86_64.rpm
rpm -ivh mongodb-org-shell-5.0.2-1.el7.x86_64.rpm
如果需要远程访问mongo,则需要修改配置文件/etc/mongod.conf,将127.0.0.1修改为0.0.0.0
systemctl start mongod #开启mongo服务
systemctl status mongod #查看mongo状态
-
php安装mongo扩展
pecl install mongodb
-
php安装tideways扩展
wget https://github.com/tideways/php-xhprof-extension/archive/v4.1.7.tar.gz
tar zxvf v4.1.7.tar.gz
cd php-xhprof-extension-4.1.7
phpize
./configure --with-php-config=/usr/local/php/bin/php-config #这里替换成自己的php-config路径
make && make install
编辑php.ini,加上
extension = tideways.so
-
安装php-monitor
git clone https://github.com/laynefyc/php-monitor.git
cd php-monitor
vim composer.json
# 在require节点最后一行加上"psr/container":"1.0.0" 这里需要指定版本,否则会不兼容
composer update --ignore-platform-reqs
# php-monitor支持三种数据存储方式:mysql,mongodb和sqlite,我们现在需要使用mongodb的存储,并且屏蔽掉自身的数据收集,因此修改
vim src/config/config.php
\'extension\' => \'tideways\',
// \'save\' => [
// \'driver\' => \'mysql\',
// \'host\' => \'127.0.0.1:3306\',
// \'database\' => \'php_monitor\',
// \'username\' => \'\',
// \'password\' => \'abcd1234\',
// \'charset\' => \'utf8mb4\',
// \'options\' => [
// 1005 => 16777216, //PDO::MYSQL_ATTR_MAX_BUFFER_SIZE and 16M
// ]
// ],
\'save\' => [
\'driver\' => \'mongodb\',
\'host\' => \'127.0.0.1:27017\',
\'database\' => \'php_monitor\',
\'username\' => \'\',
\'password\' => \'\'
],
// \'save\' => [
// \'driver\' => \'sqlite\',
// \'database\' => dirname(__DIR__).\'/db/php_monitor.sqlite3\'
// ],
\'profiler\' => [
\'enable\' => function() {
return true;//rand(1, 100) > 0;
},
\'filter_path\' => [
//filter the DOCUMENT_ROOT
//\'/home/www/xhgui/webroot\',\'F:/phpPro\'
\'/data/zhangsc/php-monitor/public\'
]
],
-
配置php-monitor nginx
server{
listen 8066;
server_name localhost;
root {your path};
index index.php index.html;
location~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
}
-
修改需要监控站点的nginx配置
#在localtion ~\.php 节点下增加
fastcgi_param TIDEWAYS_SAMPLERATE "100";
fastcgi_param PHP_VALUE "auto_prepend_file={php-monitor-path}/src/autoPrepend.php";
-
mongo优化
我们可以给mongodb加上一些索引,并且防止数据越来越多,比如我们将数据保留7天
mongo
use php_monitor
db.php_monitor.createIndex( { \'meta.SERVER.REQUEST_TIME\' : -1 } )
db.php_monitor.createIndex( { \'profile.main().wt\' : -1 } )
db.php_monitor.createIndex( { \'profile.main().mu\' : -1 } )
db.php_monitor.createIndex( { \'profile.main().cpu\' : -1 } )
db.php_monitor.createIndex( { \'meta.url\' : 1 } )
db.php_monitor.createIndex( { "meta.request_ts" : 1 }, { expireAfterSeconds : 3600*24*7 } )