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

这里的技术是共享的

You are here

php----------php安装xhprof扩展和简单使用

php----------php安装xhprof扩展和简单使用

1、下载源码包 https://github.com/longxinH/xhprof  (wget https://github.com/longxinH/xhprof/archive/master.zip)    下面截图那个是官网的,已经没有维护了,不行了。

  

2、编译

  

  

3、./configure --with-php-config=/usr/local/php/bin/php-config

  

4、make && make install

  

5、然后在 /etc/php.ini里面增加 extension=xhprof.so  保存退出。完成,执行php -m可以查看一下。

6、将之前下载的扩展包,移动到网站根目录下

  

  然后直接访问 http://192.168.18.160/xhprof/examples/sample.php

  

  拿到红线画的以后,直接访问 http://192.168.18.160/xhprof/xhprof_html/index.php?run=5ac86cc0cec09&source=xhprof_foo


  然后安装 yum install -y graphviz

  点击箭头指的地方,就可以看到图形界面

  

  红色代表运行时间长的代码块

  http://192.168.18.160/xhprof/xhprof_html/index.php  可以查看历史检测的记录

  

7、正式项目中,在入口文件加入

开头 : xhprof_enable();

结尾:

  $data = xhprof_disable();
  include_once "/usr/local/nginx/html/laravel/public/xhprof/xhprof_lib/utils/xhprof_lib.php";
  include_once "/usr/local/nginx/html/laravel/public/xhprof/xhprof_lib/utils/xhprof_runs.php";
  $aa = new XHProfRuns_Default();
  $aa->save_run($data,"test");

分类: PHP

来自 https://www.cnblogs.com/wamptao/p/8733288.html


php xhprof 扩展的使用

一,什么是XHProf

XHProf是一个分层PHP性能分析工具。它报告函数级别的请求次数和各种指标,包括阻塞时间,CPU时间和内存使用情况。一个函数的开销,可细分成调用者和被调用者的开销,XHProf数据收集阶段,它记录调用次数的追踪和包容性的指标弧在动态callgraph的一个程序。它独有的数据计算的报告/后处理阶段。在数据收集时,XHProfd通过检测循环来处理递归的函数调用,并通过给递归调用中每个深度的调用一个有用的命名来避开死循环。XHProf分析报告有助于理解被执行的代码的结构,它有一个简单的HTML的用户界面( PHP写成的)。基于浏览器的性能分析用户界面能更容易查看,或是与同行们分享成果。也能绘制调用关系图。


二,安装XHProf扩展模块

1,安装

   

  1. wget http://pecl.php.net/get/xhprof-0.9.2.tgz  
  2. tar zxvf  xhprof-0.9.2.tgz
  3. cp ./xhprof-0.9.2 /www/web         //xhprof自身带有一个web版的分析页面,放到我的web服务器下面  
  4. cd xhprof-0.9.2/extension
  5. /usr/local/php/bin/phpize
  6. ./configure --enable-xhprof --with-php-config=/usr/local/php/bin/php-config
  7. make && make install



这里你要先运行examples 下面的例子, 然后去xhprof_html 下面运行index.php 查看运行分析结果


2,配置

  1. [xhprof]
  2. extension=xhprof.so
  3. xhprof.output_dir=/home/zhangy/xhprof  //如果不加存放目录的话,默认是放在/tmp下面  


三,XHProf测试

前面我们说过了,XHProf自身带有一个web版的测试工具,里面还有一个小例子。看一下这个例子,我做了一点修改和注释

  1. <?php  
  2. function bar($x) {
  3. if ($x > 0) {
  4. bar($x -1);
  5. }
  6. }
  7. function foo() {
  8. for ($idx = 0; $idx < 5; $idx++) {
  9. bar($idx);
  10. $x = strlen("abc");
  11. }
  12. }
  13. //启动xhprof  
  14. xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
  15. //调用foo函数,也是我们要分析的函数  
  16. foo();
  17. //停止xhprof  
  18. $xhprof_data = xhprof_disable();
  19. //取得统计数据  
  20. print_r($xhprof_data);
  21. $XHPROF_ROOT = realpath(dirname(__FILE__) . '/..');
  22. include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
  23. include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
  24. //保存统计数据,生成统计ID和source名称  
  25. $xhprof_runs = new XHProfRuns_Default();
  26. $run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo"); //source名称是xhprof_foo  
  27. //弹出一个统计窗口,查看统计信息  
  28. echo "<script language='javascript'>window.open('../xhprof_html/index.php?run=" . $run_id . "&source=xhprof_foo');</script>";
  29. ?>  


以下是部分的结果:

  1. [foo==>bar] => Array  
  2. (
  3. [ct] => 5       //bar()这个函数被调用了5次  
  4. [wt] => 63      //每次运行bar()所要的时间,不知道这个是不是平均值  
  5. [cpu] => 0      //每次运行bar(),cpu运算时间  
  6. [mu] => 2860    //每次运行bar(),php所使用内存的改变  
  7. [pmu] => 0      //每次运行bar(),php在内存使用最高峰时,所使用内存的改变  
  8. )



错误提示

failed to execute cmd: " dot -Tpng". stderr: `sh: dot: command not found '

原因:未安装图形化工具

解决如下:

brew install graphviz
sudo apt-get  install graphviz

来自  https://blog.csdn.net/wjc19911118/article/details/41884087



Laravel下使用 xhprof php版本5.6

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28018283/article/details/60576728

1
先下载安装包 
  1. cd /usr/local/src
  2. git clone https://github.com/phacility/xhprof.git
  3. cd xhprof/extension



2
编译安装
  1. phpize
  2. ./configure --with-php-config=/usr/bin/php-config   --enable-xhprof     //这里 /usr/bin/php-config 是php-config的路径
  3. make
  4. make install


     

3
 修改php.ini
在扩展模块添加 
  1. [xhprof]
  2. extension=xhprof.so
  3. xhprof.output_dir=/var/www/xhprof/output     //自定义保存data的目录



4配置nginx 访问   我这里配置的vhost是  vhost.xhprof 
1 将之前下载的  xhprof  文件夹 移到 web 目录
2 创建在php.ini 中配置的output目录   /var/www/xhprof/output   ,并赋予读写权限
3 配置nginx  
访问测试 :http://vhost.xhprof/examples/sample.php 看到一些数据了
http://vhost.xhprof/xhprof_html  访问这个   可以查看最近的 xhprof 数据列表

点击 view full callgraph    发现不能发访问


安装可视化图形 ,graphviz      编译或者yum安装 

  1. yum install -y libpng
  2. yum install -y graphviz



6
 在laravel中配置使用   xhprof  
(1)使用服务容器绑定xhprof   其实就是写一个XhprofProvider    bind  xhprof 的扩展 ,实现  加载,实例化存储类 。
(2)添加  Tools 。(看个人喜欢)。  里面是  end 和 start 方法,在其他地方调用使用。
(3)在想要添加的项目中 , 比如入口处  使用  Xhprof::end();  Xhprof::start();  这里可以打印出来对应的xhprof id 。方便查看
(4)  同样 http://vhost.xhprof/xhprof_html   访问对应的xhprof记录  


来自 https://blog.csdn.net/qq_28018283/article/details/60576728?locationNum=8&fps=1



xhprof扩展的安装和简单使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuzp111/article/details/51474197

查看是否是否安装xhprof扩展

php --ri xhprof #检查php是否有这个扩展
  • 1

xhprof扩展的安装

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
  • 1

  • 2

  • 3

  • 4

  • 5

执行到这里报错了,错误信息如下:

[root@fengniu020 xhprof-0.9.4]# /usr/bin/phpize
Cannot find config.m4. 
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module

[root@fengniu020 xhprof-0.9.4]# 
  • 1

  • 2

  • 3

  • 4

  • 5

咋办呢? 
参考:安装PHP扩展 xhprof 报错 Cannot find config.m4 的解决办法

[root@fengniu020 xhprof-0.9.4]# /usr/bin/phpize
Cannot find config.m4. 
Make sure that you run '/usr/bin/phpize' in the top level source directory of the module

[root@fengniu020 xhprof-0.9.4]# l
-bash: l: command not found
[root@fengniu020 xhprof-0.9.4]# ls
CHANGELOG  examples   LICENSE  xhprof_html
CREDITS    extension  README   xhprof_lib
[root@fengniu020 xhprof-0.9.4]# cd extension/
[root@fengniu020 extension]# ls
config.m4  php_xhprof.h  tests  xhprof.c
[root@fengniu020 extension]# phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
[root@fengniu020 extension]# 
  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10

  • 11

  • 12

  • 13

  • 14

  • 15

  • 16

  • 17

  • 18

解决了安装错误,继续安装

 whereis php-config#查找php-config的位置
 ./configure --with-php-config=/usr/bin/php-config 
 make && make install 
 vim /etc/php.ini 
  • 1

  • 2

  • 3

  • 4

在php.ini中加入此扩展

extension = xhprof.so
  • 1

重启php-fpm生效

service php-fpm restart
  • 1

在脚本中开始处使用xhprof_enable()开启 
在脚本之后使用 $data = xhprof_disable() 返回数据

运行下xhprof,试试效果,可能可以正常运行不:

我的xhprof安装目录是在:

/hotdata/soft
  • 1

现在建立个测试目录,并有一个域名指向到/hotdata/kehu/wk

/hotdata/kehu/wk/ddd/
  • 1

把安装目录的文件夹拷贝到测试目录下

 cp -Rf ./xhprof-0.9.4  /hotdata/kehu/wk/ddd/
 mv xhprof-0.9.4 xhprof
  • 1

  • 2

在浏览器上运行官方提供的例子

http://wk.baidu.com/ddd/xhprof/examples/sample.php
  • 1

可以看到有如下结果 
这里写图片描述 
第一种方式:复制上面到运行的结果(http路径),再次放入浏览器,填好前面对应的域名:

http://wk.baidu.com/ddd/xhprof/xhprof_html/index.php?run=5741289a6ccef&source=xhprof_foo
  • 1

可以看到结果: 
这里写图片描述
第二种方式: 
直接访问

http://wk.baidu.com/ddd/xhprof/xhprof_html/index.php
  • 1

这里会展示检测到运行过的id 
这里写图片描述

如果查看图形化展示 
报错了:

failed to execute cmd:" dot -Tpng". stderr:`sh: dot:command not found`
  • 1

解决办法:

yum install graphviz
  • 1

再点击就可以看到图形化展示了 
这里写图片描述 
可参考这个链接,写的很不错: 
XHProf的安装和使用(PHP性能测试神器)

PHP官网链接: 
http://php.net/manual/zh/intro.xhprof.php

facebook文档: 
http://web.archive.org/web/20110514095512/http://mirror.facebook.net/facebook/xhprof/doc.html

来自  https://blog.csdn.net/everything1209/article/details/51474197

普通分类: