入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

php7性能分析工具xhprof使用

创建时间:2018-02-17 投稿人: 浏览次数:199

官方地址

xhprof分析脚本引入

在需要分页的页面最前面引入即可,不建议全局引入

xhprof扩展安装

git clone https://github.com/longxinH/xhprof
cd xhprof/extension/
phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-xhprof
make && make install

php扩展配置
[xhprof]
extension=xhprof.so

xhprof原生web支持 当前不采用

xhprof自带web页面配置
mkdir /home/ding/xhprof
cp -r xhprof/xhprof_html  /home/ding/xhprof/
cp -r xhprof/xhprof_lib /home/ding/xhprof/


xhprof.php 首页引入文件
xhprof_enable(XHPROF_FLAGS_NO_BUILTINS + XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
register_shutdown_function(function(){
    $data = xhprof_disable();   //返回运行数据
    include "/home/ding/projects/xhprof/xhprof_lib/utils/xhprof_lib.php";
    include "/home/ding/projects/xhprof/xhprof_lib/utils/xhprof_runs.php";
    $objXhprofRun = new XHProfRuns_Default();
    $objXhprofRun->save_run($data, "test"); //test 表示文件后缀
});

nginx配置
xhprof.com
    root /home/ding/projects/xhprof/xhprof_html

xhprof.com域名访问web页面


图形扩展安装  自带的需要安装graphviz才能展示
yum install graphviz

配置php请求自动加载文件方法

1. 在php.ini中添加:
    加载前
    auto_prepend_file = /var/www/head.php
    加载后
    auto_append_file = /var/www/foot.php

2. 或者在.htaccess中添加
    php_value auto_prepend_file = /home/ding/projects/xhprof.php
    #php_value auto_append_file = /var/www/foot.php

3. nginx配置中添加
    在enable_php.config 文件中添加
    fastcgi_param PHP_VALUE "auto_prepend_file=/home/ding/projects/xhprof.php";

mongodb安装

xhgui采用mongodb存储数据
mongodb版本不易过高,把mongodb 版本降级为3.4
centos
    tar zxf mongodb-linux-x86_64-3.2.9.tgz
    mv mongodb-linux-x86_64-3.2.9 /usr/local/mongodb
    cd /usr/local/mongodb
    mkdir db
mac
    brew install mongodb

mongodb扩展安装

tar zxf mongodb-1.3.0.tgz
cd mongodb-1.3.0
phpize
mac
    ./configure --with-php-config=/usr/local/php/bin/php-config --with-openssl-dir=/usr/local/Cellar/openssl/1.0.2n
centos
    ./configure --with-php-config=/usr/local/php/bin/php-config

make && make install

修改php.ini
    extension=mongodb.so

重启php-fpm

mongodb操作

启动mongodb
    nohup /usr/local/mongodb/bin/mongod --dbpath /usr/local/mongodb/db & > /home/ding/log/mongodb_init.log

客户端连接mongod
    #不指定--host就采用默认
    /usr/local/mongodb/bin/mongo --host 127.0.0.1:27017

    执行以下命令初始化xhprof数据库
    > 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 } )
    > db.results.ensureIndex( { "meta.simple_url" : 1 } )

    //过期时间设置,定期删除信息 删除5天前的数据
    > db.results.ensureIndex( { "meta.request_ts" : 1 }, { expireAfterSeconds : 432000 } )

    > exit

    清空数据


客户端关闭服务器
    > use admin;
    > db.shutdownServer();

xhgui 图形界面安装配置

工具使用
http://blog.it2048.cn/article_tideways-xhgui.html

代码安装
    cd ~/projects
    git clone https://github.com/laynefyc/xhgui-branch.git
    cd xhgui-branch
    php install.php

nginx配置
    xhgui.com
    root   /var/www/example.com/public/xhgui/webroot/;
    index  index.php;
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    include enable_php.config;



在enable_php.conf中添加

    location ~ .php {
        fastcgi_pass unix:/tmp/php-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        # 最长执行时间
        fastcgi_read_timeout 300;
        fastcgi_split_path_info       ^(.+.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        #添加php预加载文件
        fastcgi_param PHP_VALUE "auto_prepend_file=/Users/apple/projects/xhgui/external/header.php";
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

域名过滤
    "profiler.enable" => function() {
        //域名过滤
        $domains = ["xyii.com"];
        $domain = $_SERVER["SERVER_NAME"];
        if(in_array($domain,$domains)){
            return true;
        }else{
            return false;
        }
    }

xhgui bug修复
    $profile = [];
    foreach($data["profile"] as $key => $value) {
        $profile[strtr($key, ["." => "_"])] = $value;
    }
    $data["profile"] = $profile;

基本返回值解读

"ct" => 1,          执行的次数
"wt" => 793898,     执行时间
"cpu" => 110507,    cpu时间
"mu" => 6582856,    内存使用
"pmu" => 6990728,   内存峰值
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。