1.安装xhprof
git地址:https://github.com/longxinH/xhprof.git
//path为你的路径
git clone https://github.com/longxinH/xhprof.git ./xhprof
cd xhprof/extension/
/path/to/php7/bin/phpize
./configure --with-php-config=/path/php7/bin/php-config
make && sudo make install
//配置到php.ini中
extension = xhprof.so
xhprof.output_dir = /tmp/xhprof
重启php-fpm
查看xhprof是否安装成功
php -m
注意:
之前make的时候一直报错,找不到方法之类的错误。
‘zend_execute_data’ has no member named ‘function_state’
我的环境是PHP7,但是当时用的xhprof包是0.7,是不支持PHP7的。
所以一定要注意包的版本和PHP版本要兼容。
配置xhprof
将相关文件移入项目中,也可以移入示例文件
//切换到下载的 xhprof 目录
cp -r xhprof/xhprof_html ROOT_PATH/
cp -r xhprof/xhprof_lib ROOT_PATH/
//此文件可选,也可以不移入,为了方便测试,移入
cp -r xhprof/examples ROOT_PATH/
配置nginx
server {
listen 80;
server_name xhprof.test.com;
root /var/www/xhprof/xhprof_html;
index index.html index.php;
location ~ \.php$ {
fastcgi_pass php_pool;
fastcgi_index index.php;
include fastcgi.conf;
}
}
重启nginx
访问xhprof.test.com
示例
访问 http://xhprof.test.com/examples/sample.php可以看到结果
拿到?run=5d86b71a8b75b&source=xhprof_foo
访问
http://xhprof.test.com/xhprof_html/callgraph.php?run=5d86b66acd244&source=xhprof_foo
可以看到如下页面
图形化结果
点击[View Full Callgraph]可以看图形化结果
如果出现报错
failed to execute cmd:" dot -Tpng". stderr:sh: dot:command not found。
//解决方案
yum install graphviz
点开View Full Callgraph
字段含义
Calls / Call Count:函数被调用的次数
Incl. Wall Time / Wall Time:执行该函数(包括子函数)耗费的时间
Incl. MemUse / Memory Usage:该函数(包括子函数)占用的内存
Incl. PeakMemUse / Peak Memory Usage:函数(包括子函数)占用内存的峰值
Incl. CPU / CPU:执行该函数(包括子函数)花费的CPU时间
Excl. Wall Time / Exclusive Wall Time:函数本身(不包括子函数)耗费的时间
Excl. MemUse / Exclusive Memory Usage:函数本身(不包括子函数)占用的内存
Excl. PeakMemUse / Exclusive Peak Memory Usage:函数本身(不包括子函数)耗费内存的峰值
Exclusive CPU:函数本身(不包括子函数)花费的CPU时间
分析
看到在redis的get操作上是最耗时的,其次是curl。
那么我程序的瓶颈点是在redis操作上,我应该做的是减少redis操作,并且减少curl请求次数。