使用XHProf分析PHP性能瓶颈(二)

上一篇文章里,咱们介绍了如何基于xhprof扩展来分析PHP性能,并记录到日志里,最后使用xhprof扩展自带的UI在web里展现出来。本篇文章将讲述2个知识点:php

  • 使用xhgui代替xhprof的默认UI界面,更便于分析
  • 使用tideways扩展替换xhprof扩展

使用更漂亮的UI: xhgui

xhgui支持 XHProf, Uprofiler或者Tideways 扩展,也就是说,只要安装了这几种扩展中的一种便可。 html

本次测试中,实际使用了tideways扩展(切换为XHProf扩展后web里看不到数据,缘由未知。切换为Uprofiler也没有数据。)。nginx

xhprof虽然来自facebook但已经好久不更新,官方源已经显示This package is abandoned and no longer maintained(此包已废弃,再也不维护)。tideways刚好相反,一直有商业公司在维护,而且积极的支持了PHP7。两个扩展都是开源的,综上所述我建议你们选择tideways来分析代码。

安装tideways扩展:git

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

若是须要安装中文界面的,能够:github

git clone https://github.com/laynefyc/xhgui-branch.git  xhgui

而后安装xhgui依赖:web

cd xhgui
php install.php

安装须要等待几分钟,请耐心等待。 mongodb

设置缓存目录的权限,容许nginx建立文件:segmentfault

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/,能够看到:


点击某次请求进去看详情:

参考

一、PHP性能追踪及分析工具xhprof的安装与使用 - 马新才的技术博客 - SegmentFault 思否
https://segmentfault.com/a/11...
二、Tideways和xhgui打造PHP非侵入式监控平台 | 我是大熊
http://blog.it2048.cn/article...


防盗版声明:本文系原创文章,原发布于公众号飞鸿影的博客(fhyblog)及博客园,转载需做者赞成。

相关文章
相关标签/搜索