上一篇文章里, 我们介绍了如何基于 xhprof 扩展来分析 PHP 性能, 并记录到日志里, 最后使用 xhprof 扩展自带的 UI 在 web 里展示出来. 本篇文章将讲述 2 个知识点:
使用 xhgui 代替 xhprof 的默认 UI 界面, 更便于分析
使用 tideways 扩展替换 xhprof 扩展
使用更漂亮的 UI: xhgui
xhgui 支持 XHProf http://pecl.php.net/package/xhprof , Uprofiler https://github.com/FriendsOfPHP/uprofiler 或者 Tideways https://github.com/tideways/php-profiler-extension 扩展, 也就是说, 只要安装了这几种扩展中的一种即可.
本次测试中, 实际使用了 tideways 扩展 (切换为 XHProf 扩展后 web 里看不到数据, 原因未知. 切换为 Uprofiler 也没有数据.).
xhprof 虽然来自 facebook 但已经很久不更新, 官方源已经显示 This package is abandoned and no longer maintained(此包已废弃, 不再维护).tideways 恰好相反, 一直有商业公司在维护, 并且积极的支持了 php7. 两个扩展都是开源的, 综上所述我建议大家选择 tideways 来分析代码.
安装 tideways 扩展:
- wget https://github.com/tideways/php-xhprof-extension/archive/v4.1.5.tar.gz -O php-xhprof-extension-4.1.5.tar.gz
- tar xzf /php-xhprof-extension-4.1.5.tar.gz
- cd php-xhprof-extension-4.1.5
- phpize
- ./configure
- make && make install
安装 xhgui
- cd /work/
- git clone https://github.com/perftools/xhgui.git xhgui
如果需要安装中文界面的, 可以:
git clone https://github.com/laynefyc/xhgui-branch.git xhgui
然后安装 xhgui 依赖:
- cd xhgui
- php install.php
安装需要等待几分钟, 请耐心等待.
设置缓存目录的权限, 允许 nginx 创建文件:
chmod -R 777
xhgui 已经把注入入口文件都写好了, 位于
external/header.php
, 无需我们手动去写类似上一篇的 xhprof.inc.php 注入文件.
安装 MongoDB 及客户端
xhgui 把日志写到了 MongoDB, 所以使用 xhgui 需要安装 MongoDB 服务端. 此处省略安装, 启动 MongoDB 服务端过程.
为提高 MongoDB 的性能, 你可以运行以下指令以添加索引:
- $ /usr/local/mongodb/bin/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 } )
同理, 由于 xhgui 是 PHP 写的, 还需要读取 MongoDB 里的数据, 需要安装 MongoDB php 客户端:
pecl install mongodb
然后在 php.ini 文件添加配置:
- [mongo]
- extension=mongo.so
查看扩展是否安装成功:
php -m | grep mongo
然后重启 php-fpm 服务.
配置 xhgui
xhgui 的 config 目录有一个 config.default.php, 复制为 config.php, 如果 mongodb 地址不是默认的, 修改:
'db.host' => 'mongodb://127.0.0.1:27017',
还有修改采样频率, 默认是 1/100, 测试的话改为 true:
- 'profiler.enable' => function() {
- //return rand(1, 100) === 42;
- return true;
- },
配置项目注入
上一篇文章中, 我们介绍到, 注入的入口文件可以写到 php.ini 或者 nginx, 我建议写在 nginx 配置, 这样只会影响该 nginx 对应的项目, 而不是所有使用该 php 环境的项目. 入口文件使用 xhgui 自带的注入文件:
- jifen.cc.conf
- location ~ \.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param PHP_VALUE "auto_prepend_file=/work/xhgui/external/header.php";
- include fastcgi_params;
- }
配置 xhgui web
我们修改 xhprof.test.com.conf 为:
- server {
- listen 80;
- server_name xhprof.test.com;
- #root /work/xhprof/xhprof_html;
- root /work/xhgui/webroot/;
- index index.php index.html;
- location / {
- if (!-e $request_filename) {
- rewrite . /index.php last;
- }
- }
- location ~ \.php$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
重启 nginx 服务.
我们请求几次应用的接口, 打开浏览器输入 http://xhprof.test.com/, 可以看到:
点击某次请求进去看详情:
参考
1,PHP 性能追踪及分析工具 xhprof 的安装与使用 - 马新才的技术博客 - SegmentFault 思否
https://segmentfault.com/a/1190000007288664#articleHeader4
2,Tideways 和 xhgui 打造 PHP 非侵入式监控平台 | 我是大熊
http://blog.it2048.cn/article-tideways-xhgui/
来源: https://www.cnblogs.com/52fhy/p/9038788.html