XHProf + Xhgui性能追踪及分析
一,介绍
xhprof是一个由facebook开源出来的函数级别的分层性能报告工具,包括查看调用次数,阻塞时间,CPU时间和内存使用情况。占用资源很少,甚至能够在生产环境中部署(但不推荐)。
二,安装XHProf扩展
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
wget http://pecl.php.net/get/xhprof-0.9.4.tgz tar zxvf xhprof-0.9.4.tgz cd xhprof-0.9.4/extension/ /usr/bin/phpize ./configure --with-php-config=/usr/bin/php-config make && make install #编辑php.ini: [xhprof] extension = xhprof.so xhprof.output_dir=/tmp #重启fpm service php-fpm restart |
三,安装MongoDB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#1.下载安装包 wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.3.tgz tar zxf mongodb-linux-x86_64-3.4.3.tgz #2.安装准备 mv mongodb-linux-x86_64-3.4.3 /usr/local/mongodb mkdir /usr/local/mongodb/data #创建数据库文件夹与日志文件 touch /usr/local/mongodb/logs #3.设置开机自启动 将mongodb启动项目追加入rc.local保证mongodb在服务器开机时启动 echo "/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data –logpath=/usr/local/mongodb/logs –logappend --auth –port=27017" >> /etc/rc.local #4.启动 cd到mongodb目录下的bin文件夹启动mongodb //下面这个是需要权限的登录方式, 用户连接需要用户名和密码 /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --auth --port=27017 --fork //这个是不需要密码的 /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --logappend --port=27017 --fork #5.进入数据库的CLI管理界面 cd到mongodb目录下的bin文件夹,执行命令./mongo 运行如下: [root@namenode mongodb]# ./bin/mongo MongoDB shell version: 1.8.2 connecting to: test > use test; switched to db test |
四,安装PHP MongoDB扩展
1 2 3 4 5 6 7 8 9 10 11 12 13 |
wget https://pecl.php.net/get/mongodb-1.2.8.tgz tar zxvf mongodb-1.2.8.tgz cd mongodb-1.2.8 /usr/bin/phpize ./configure --with-php-config=/usr/bin/php-config make && make install #编辑php.ini: [MongoDB] extension = mongodb.so #重启fpm service php-fpm restart |
五,安装Xhgui
1 2 3 4 5 6 7 |
git clone https://github.com/perftools/xhgui.git 或者汉化版 git clone https://github.com/maxincai/xhgui.git cd xhgui chmod -R 0777 cache cp config/config.default.php config/config.php composer install #需要composer依赖 |
六,在应用程序的nginx配置中注入文件
1 2 3 4 5 |
fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/xhgui/external/header.php"; #运行时发现时间格式错误,在header.php文件中加入 date_default_timezone_set('UTC'); |
七,设置Xhgui可视化界面虚拟主机
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
server { listen 5555; server_name www.xhgui.com; charset utf-8; root /usr/share/nginx/xhgui/webroot/; index index.php; rewrite_log on; client_max_body_size 150M; location / { index index.htm index.html index.php init.php; #访问路径的文件不存在则重写URL转交给ThinkPHP处理 if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; break; } } location ~ \.php/?.* { root /usr/share/nginx/xhgui/webroot/; #fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/tmp/php5-fpm.sock; fastcgi_index index.php; #加载Nginx默认"服务器环境变量"配置 include fastcgi.conf; #设置PATH_INFO并改写SCRIPT_FILENAME,SCRIPT_NAME服务器环境变量 set $fastcgi_script_name2 $fastcgi_script_name; if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") { set $fastcgi_script_name2 $1; set $path_info $2; } fastcgi_param PATH_INFO $path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2; fastcgi_param SCRIPT_NAME $fastcgi_script_name2; } } |
八,参考
URL:请求所访问的URL
Time:请求发起时间
wtor: “Wall Time” –请求所经历的所有时间. 这是 “wall clock” time的简称,表示用户等待请求完成所有的时间
cpu:花费在该请求上的CPU时间
mu:该请求所消耗的内存
pmu:请求处理过程中所消耗的最大内存
发表评论