xhprof的简单使用

简介

XHProf是一个分层PHP性能分析工具。

前提

我的PHP版本是PHP7:

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

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

安装步骤

当做一个php项目!clone别人php代码放哪个目录你自己定 1.编译安装

 

 

 

cd /Users/birjemin/Developer/Php git clone https://github.com/longxinH/xhprof cd xhprof/extension phpize ./configure make 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.查看是否安装成功

恭喜安装成功!

使用步骤

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

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

开启xhproffunction 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.comnginx server confserver { 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看看能不能访问(没有目录??你是不是浏览器无法访问目录??权限没开。。自己配置一下)

6.在postman或者浏览器访问接口,转啊转,好了之后就可以去

http://local.xhprof.com/xhprof_html/

查看了。(图我就不截了。。下次讲一下xgui的使用)

遇到的问题

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

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

我用的是bashvim .bash_profile添加两行# PHP -v export 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安装的过程。

来源:https://baijiahao.baidu.com/s?id=1591352722493070960&wfr=spider&for=pc

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值