欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

linux下XHProf的安装和使用 有大用

XHProf是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开 关来控制是否进行profile。
不知怎么回事 我的 
安装 XHprof 扩展 后 再 在drupal 6 drupal6 安装了 XHprof 模块后 程序会出问题



  下面 XHprof  libpng graphviz 最好要同时装

1.安装XHprof

 

[plain] view plain copy
 
  1. wget http://pecl.php.net/get/xhprof-0.9.2.tgz  
  2. tar zxf xhprof-0.9.2.tgz  
  3. cd xhprof-0.9.2  
  4. cp -r xhprof_html xhprof_lib /你的web目录路径/xhprof/  (此处目的是建立数据分析目录,可将此目录配置成虚拟主机访问)  
  5. cd extension/  
  6. /usr/local/webserver/php/bin/phpize  
  7. ./configure  --with-php-config=/www/wdlinux/apache_php-5.5.9/bin/php-config   (php-config的路径)  
  8. make && make install  
安装完提示:
Installing shared extensions:     /www/wdlinux/apache_php-5.5.9/lib/php/extensions/no-debug-non-zts-20121212/

 

 

2.php.ini中添加

extension_dir = " /www/wdlinux/apache_php-5.5.9/lib/php/extensions/no-debug-non-zts-20121212/xhprof.so"
这句我原来就有了,就这用添加下面两句
extension=xhprof.so
xhprof.output_dir=/www/logs/xhprof

分析日志输出在/www/logs/xhprof目录。

 

3.重启Nginx服务器(或 apache )和php,打开phpinfo()页面便可以看到看到输出中有了xhprof信息。

[plain] view plain copy
 
  1. xhprof  0.9.2  
  2. CPU num  2  

4.安装libpng包

否则会因为dot 不支持PNG,不能显示图表数据分析,且 出现Error: either we can not find profile data for run_id 4d7f0bd99a12f or the threshold 0.01 is too small or you do not have ‘dot’ image generation utility installed.的错误

 

[plain] view plain copy
 
  1. wget http://prdownloads.sourceforge.net/libpng/libpng-1.6.32.tar.gz   //如果不可用,可以在网上找一个下下来再编译安装  
  2. tar zxf libpng-1.5.1.tar.gz    
  3. cd libpng-1.5.1    
  4. ./configure    
  5. make    
  6. make install    

 

5.安装graphviz,一个画图工具

[plain] view plain copy
 
  1. wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz  
  2. tar zxf graphviz-2.24.0.tar.gz  
  3. cd graphviz-2.24.0  
  4. ./configure --with-png=yes  
  5. make && make install  

6.使用方法:

 

[plain] view plain copy
 
  1. 头部:  
  2. xhprof_enable();   
  3. //xhprof_enable(XHPROF_FLAGS_NO_BUILTINS); 不记录内置的函数  
  4. //xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);  同时分析CPU和Mem的开销  
  5. $xhprof_on = true;  
  6.   
  7. 生产环境可使用:  
  8. if (mt_rand(1, 10000) == 1) {  
  9.    xhprof_enable();  
  10.    $xhprof_on = true;  
  11. }  
  12.   
  13. 尾部:  
  14. if($xhprof_on){  
  15. $xhprof_data = xhprof_disable();  
  16. $xhprof_root = '/www/<span style="font-family: Arial, Helvetica, sans-serif;">(xhprof的虚拟主机目录)</span>/';  
  17. include_once $xhprof_root."xhprof_lib/utils/xhprof_lib.php";   
  18. include_once $xhprof_root."xhprof_lib/utils/xhprof_runs.php";   
  19. $xhprof_runs = new XHProfRuns_Default();   
  20. $run_id = $xhprof_runs->save_run($xhprof_data, "hx");  
  21. echo '<a href="http://(xhprof的虚拟主机域名)/xhprof_html/index.php?run='.$run_id.'&source=hx" target="_blank">统计</a>';  
  22. }  
  23.   
  24. 运行程序,底部出现统计字样,点过去就可以看到性能分析了。按运行时间排序,很容易找出化时间最长的函数。  
  25. 点[View Full Callgraph]图形化显示,最大的性能问题会用红色标出,其次是黄色。  

附运行结果里的名次解释:
[plain] view plain copy
 
  1. Function Name 函数名  
  2. Calls 调用次数  
  3. Calls% 调用百分比  
  4. Incl. Wall Time (microsec) 调用的包括子函数所有花费时间 以微秒算(一百万分之一秒)  
  5. IWall% 调用的包括子函数所有花费时间的百分比  
  6. Excl. Wall Time (microsec) 函数执行本身花费的时间,不包括子树执行时间,以微秒算(一百万分之一秒)  
  7. EWall% 函数执行本身花费的时间的百分比,不包括子树执行时间  
  8. Incl. CPU(microsecs) 调用的包括子函数所有花费的cpu时间。减Incl. Wall Time即为等待cpu的时间  
  9. 减Excl. Wall Time即为等待cpu的时间  
  10. ICpu% Incl. CPU(microsecs)的百分比  
  11. Excl. CPU(microsec) 函数执行本身花费的cpu时间,不包括子树执行时间,以微秒算(一百万分之一秒)。  
  12. ECPU% Excl. CPU(microsec)的百分比  
  13. Incl.MemUse(bytes) 包括子函数执行使用的内存。  
  14. IMemUse% Incl.MemUse(bytes)的百分比  
  15. Excl.MemUse(bytes) 函数执行本身内存,以字节算  
  16. EMemUse% Excl.MemUse(bytes)的百分比  
  17. Incl.PeakMemUse(bytes) Incl.MemUse的峰值  
  18. IPeakMemUse% Incl.PeakMemUse(bytes) 的峰值百分比  
  19. Excl.PeakMemUse(bytes) Excl.MemUse的峰值  
  20. EPeakMemUse% EMemUse% 峰值百分比  
版权声明:本文为博主原创文章,未经博主允许不得转载。

来自 http://blog.csdn.net/maitiandaozi/article/details/8896293



centos6 安装xhprof扩展

首先配置yum源

追加CentOS 6.5的epel及remi源

# rpm -Uvh http://ftp.iij.ad.jp/pub/linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm

# rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

检查已安装的php包

# yum list --enablerepo=remi --enablerepo=remi-php56 | grep php

安装php5.6

# yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit

php-pecl-xdebug php-pecl-xhprof

查看php版本

# php --version 或 php -v

安装php-fpm

# yum install --enablerepo=remi --enablerepo=remi-php56 php-fpm

以上只是准备工作!!!

# wget http://pecl.php.net/get/xhprof-0.9.4.tgz

# tar zxvf xhprof-0.9.4.tgz

# cd xhprof-0.9.4

# whereis phpize

# /usr/bin/phpize #上一步得到的命令所在位置

如果报以下错误

Cannot find config.m4.

Make sure that you run '/usr/bin/phpize'in the top level source directory of the module

在当前目录下进入extension目录下:

# cd extension/

# ls config.m4 php_xhprof.h tests xhprof.c

# phpizeConfiguringfor:PHPApiVersion:20100412ZendModuleApiNo:20100525ZendExtensionApiNo:220100525

然后编译安装:

# whereis php-config #查找php-config的位置 # ./configure --with-php-config=/usr/bin/php-config #/usr/bin/php-config 上一步的结果 # make && make install

没报错就说明安装成功,然后就是在php.ini加入扩展

# vim /etc/php.ini

在文件中添加一句:

extension = xhprof.so

重启php-fpm生效

# service php-fpm restart

安装xhprof扩展已经完成

如何使用xhprof

将文件拷贝到网站目录里面,然后添加监控代码到统计文件中,这一步是引入文件

# mkdir -p /home/www/xhprof && cd /home/www/xhprof/

之前是在tmp目录下下载xhprof-0.9.4包
# cp -r /tmp/xhprof-0.9.4/xhprof_html/ ./                        
# cp -r /tmp/xhprof-0.9.4/examples/ ./# cp -r /tmp/xhprof-0.9.4/xhprof_lib/ ./# mkdir logs


项目中的使用方法(打点):
1.开始标志
xhprof_enable();      

2.被包含的函数体,也是项目中需要检测的函数
function();

3.结束标志 
$data = xhprof_disable();
$xhprof_root = "/home/wwwroot/xhprof";
include_once $xhprof_root."/xhprof_lib/utils/xhprof_lib.php";
include_once $xhprof_root."/xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($data, "test");


访问路径:
xxx.xxx.xxx.xxx 代表服务器主机
http://xxx.xxx.xxx.xxx/xhprof/xhprof_html/index.php?run=$run_id&source=test

个人觉得打点到访问这个过程可以封装起来,方便使用

其中[View Full Callgraph]按钮可以查看耗时情况分布图,如果点击出现以下报错:
Error: either we can not find profile data for run_id 58b393370e400 or 
the threshold 0.01 is too small or you do not have 'dot' image generation utility installed.
错误︰ 我们不能为 run_id 58b393370e400 找到配置文件数据或 0.01 阈值是太小或您没有安装的 '点' 图像生成实用程序。

xhprof绘制的是png图,绘图的dot拓展没装成功,也就是dot 不支持PNG

# yum install -y graphviz 即可

 

 

 

 

 

 

 

 

wget http://pecl.php.net/get/xhprof-0.9.4.tgz tar zxvf xhprof-0.9.4.tgz cd xhprof-0.9.4 whereis phpize /usr/bin/phpize

来自  http://www.cnblogs.com/cyj1733/p/6536472.html
普通分类: