您的位置 首页 php

PHP-使用xhprof分析性能

简介

XHProf是一个分层PHP性能分析工具。(自己google吧,baidu也行)。

前提

我的PHP版本是PHP7:

php -v

而xhprof支持PHP7的库请在longxinH-xhprof查看。

github xhprof

备注:我之前在phacility克隆的,不过安装失败,原因就是我的PHP版本是PHP7,而phacility版本的并不支持。(采坑呢~~)

安装步骤

当做一个php项目!clone别人php代码放哪个目录你自己定 。

1.编译安装

cd /Users/birjemin/Developer/Php

git clone  xhprof/extension

phpize

./configure

make

make install 

make install

2.配置文件

cd /usr/local/etc/php/7.0/conf.d

vim ext-xhprof. ini  

添加内容(这个就是编译成功之后的路径,见上图)

[xhprof]

extension="/usr/local/Cellar/php70/7.0.14_7/lib/php/extensions/no-debug-non-zts-20151012/xhprof.so" 

3.重启php-fpm(视个人重启方式而定,我的重启方式是这样的)

cd /usr/local/etc/php/7.0/

sudo killall php-fpm

sudo /usr/local/Cellar/php70/7.0.14_7/sbin/php-fpm -D 

4.查看是否安装成功

php -m

phpinfo

恭喜安装成功!

使用步骤

1.如果使用呢?两种方法,第一种就是把xhprof_lib.php和xhprof_runs.php拷贝到项目中,第二种就是用绝对路径引入这两个文件,推荐第一种,这样别人clone的你的项目,不需要做啥。

2.建立两个开启函数(isDev()是判断本地环境的意思,请视情况删除)

  • 开启xhprof

function enableXhprof() {

isDev() && xhprof_enable(0, []);

}

  • 运行xhprof(xhprof_lib.php和xhprof_runs.php可以拷贝到项目的,请确保include_once请够加载这两个文件)。

function disableXhprof() {

if (isDev()) {

$xhprof_data = xhprof_disable();

include_once app_path() . “/xhprof_lib.php”;

include_once app_path() . “/xhprof_runs.php”;

$xhprof_runs = new \XHProfRuns_Default();

$run_id = $xhprof_runs->save_run($xhprof_data, “xhprof_foo”);

#echo $run_id;

}

}

3.可以在需要监听的接口中代码片段前面加上enableXhprof(),后面加上disableXhprof()就好了。接下来就可以分析这段代码了。(请确保这两个函数全局访问)

4.将克隆的xprof配置虚拟主机,这个和你的项目一样的,就把xprof也当做一个项目,配置成浏览器可访问。比如我的配置:

  • host: 127.0.0.1 local.xhprof.com

  • nginx server conf

server {

listen 80;

server_name local.xhprof.com;

access_log logs/xhprof.access.log main; autoindex on;

location / {

root /Users/birjemin/Developer/Php/xhprof;index index.html index.htm index.php;try_files $uri $uri/ /index.php?$query_string;

}

location ~ \.php$ {

root /Users/birjemin/Developer/Php/xhprof;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_intercept_errors on;

include fastcgi_params;
}
}

5.重启nginx,浏览器访问local.xhprof.com看看能不能访问(没有目录??你是不是浏览器无法访问目录??权限没开。。自己配置一下)

local.xhprof.com

6.在postman或者浏览器访问接口,转啊转,好了之后就可以去 查看了。(图我就不截了。。下次讲一下xgui的使用)

xhprof_html

遇到的问题

1.php -v的版本和phpinfo()的版本不一致,如下图:

php -v

phpinfo

解决方法:更改php cli的默认版本

  • 我用的是bash

    vim .bash_profile

  • 添加两行

    # PHP -vexport PATH=”/usr/local/Cellar/php70/7.0.14_7/bin:$PATH”

  • 生效

    source .bash_profile

2.编译成功,但是php -m 没有 xhprof。

xhprof支持PHP7的库请在longxinH-xhprof查看!!!!

备注

全程经历了2.5h吧~~~不过文章也写了2.5小时。哈哈哈哈~这个东西和xdebug不一样的,这个是分析性能瓶颈的,可以让你优化代码。xdebug主要用于调试,我下次写一个xdebug安装的过程。

文章来源:智云一二三科技

文章标题:PHP-使用xhprof分析性能

文章地址:https://www.zhihuclub.com/77951.shtml

关于作者: 智云科技

热门文章

网站地图