PHP使用xfprof进行性能分析

  • 创作背景
    因为公司有个项目,大致有1200万数据,当前使用ElasticSearch进行操作,需求是每次进入页面进行3次查询,一次更新。进入页面之后window.onload执行一个异步请求。
    MySql查询就只有一点权限管理。耗时大概DB23次,耗时104ms
    服务器是8核32G的,php-fpm的动态进程数量是够得。这样的配置下200次/分就会变慢。后来将进入页面es查询改为一次缓存3条数据,异步请求记录到Redis后性能达到400次/分就到顶峰。这时候去看MySqlESRedis和服务器,经检查各项指标都正常。后加大进入页面的缓存也无济于事,网上就搜索到有个类型X-Debug的扩展 xhprof

  • 安装

git clone https://github.com/longxinH/xhprof.git 
cd xhprof/extension/
phpize  #phpize请根据自己的项目来(一般来说在安装...php/bin/phpize)
./configure --with-php-config=/www-web/software/php71/bin/php-config #同理这里的php-config文件找自己的
make && make test 
make install

编辑php.ini文件,在文件中添加以下内容。不确定位置的可以使用命令php -i | grep php.ini 查看对应的ini文件地址

[xhprof]
extension = /www-web/software/php71/lib/php/extensions/no-debug-non-zts-20160303/xhprof.so
xhprof.output_dir = /www-web/wwwroot/xhprof  #xhprof输出文件的地址,需要运行php的账号有权限

  • 重启php-fpm
  • 查看扩展是否生效
php --ri xhprof

输出以下类似内容则为成功


xhprof

xhprof support => enabled
Version => 2.2.2-dev

Directive => Local Value => Master Value
xhprof.output_dir => /www-web/wwwroot/xhprof => /www-web/wwwroot/xhprof
xhprof.collect_additional_info => 0 => 0
xhprof.sampling_interval => 100000 => 100000
xhprof.sampling_depth => 2147483647 => 2147483647
  • 测试
    将xhprof/xhprof_lib/utils下xhprof_lib.php和xhprof_runs.php两个文件copy到项目目录中
xhprof_enable();#start profiling

//TODO function()

$xhprof_data = xhprof_disable();// stop profiler
include_once "xhprof_lib.php";
include_once "xhprof_runs.php";
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_test");
//echo $run_id;
file_put_contents('xh.log', $run_id . ' ' . date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);


image.png

注:红色标记的地方为代码块,绿色划线为此次执行的唯一ID。
我在这里是写在了根目录 xh.log这个文件里面。

执行对应代码2次。可查看xh.log内容

5f8ed7c8812e3 2020-10-20 20:27:52
5f8ed87cb09ba 2020-10-20 20:30:52
  • 查看结果
    方式1:浏览器打开对应域名http://xx.xxx.com/xhprof/xhprof_html/index.php
    image.png

    这是我将xhprof文件移到我项目根目录了。
    image.png

    方式2:将xhprof/xhprof_html配置为一个域名。
server {
        listen 80 ;
        #listen [::]:80 default_server ipv6only=on;
        server_name  xhprof.local.com;
        index index.html index.htm index.php;
        root  /www-web/software/xhprof/xhprof_html;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_prefer_server_ciphers on;
        #error_page   404   /404.html;
        #url rewire
        location / {
                if (!-e $request_filename){
                        rewrite ^/(.*) /index.php last;
                }
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        location ~ \.php$ {
           # root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }

        location ~ /\.
        {
            deny all;
        }
}

访问xhprof.local.com这个也是可以的.

点击任意一个文件都可以看得到每个过程,大致的流程如下:


image.png

点击[View Full Callgraph]进入图片浏览:

image.png

注意:查看图片必须安装graphviz才能使用我是直接使用yum install安装的,各服务器不同就使用不同的命令安装即可。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。