在ubuntu,lampp下使用xhprof

以前用过的profile工具有xdebug, zenddebug,对于一些大的应用程序,效果非常差,今天尝试了一下在 ubuntu,lampp下使用xhprof,整个过程如下


环境:

ubuntu 11.04

lampp 1.7.3a


一。安装xhprof


编译前有两点需要注意的:

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

tar zxf xhprof-0.9.2.tgz

cd xhprof-0.9.2

cp -r xhprof_html xhprof_lib /opt/lampp/htdocs

cd extension/

/opt/lampp/bin/phpize;//phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块

运行这个命令后注意两个问题:

1.运行这个命令后可能出现以下的错误

grep: /opt/lampp/include/php/main/php.h: No such file or directory
grep: /opt/lampp/include/php/Zend/zend_modules.h: No such file or directory
grep: /opt/lampp/include/php/Zend/zend_extensions.h: No such file or directory


这是因为你的XAMPP没有 DEVEL包所以需要先确保下载并安装了 xampp 的 devel packages。下载地址:http://sourceforge.net/projects/xampp/files/XAMPP%20Linux/,选择当前xampp的版本,点击进入后下载xampp-linux-devel-1.X.X.tar.gz   然后解压到安装的目录  tar -xvzf file -C /opt。下载 xampp 的 devel 包的时候,注意要选择和自己当前的版本一致的 devel 包,因为 php 的扩展编译的时候,会附加版本信息,启动时进行检查,如果不一致,即便能够编译成功,也是不能够使用的。

完成后,你会发现lampp的目录下多了个include目录,里面是一些必要的头文件。 


2. 运行这个命令后可能出现以下的错误

Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.


解决方法:

# wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
# tar -zvxf m4-1.4.9.tar.gz
# cd m4-1.4.9/
# ./configure && make && make install
# cd ../
# wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
# tar -zvxf autoconf-2.62.tar.gz
# cd autoconf-2.62/
# ./configure && make && make install

在UBUNTU下更简单:

sudo apt-get install m4
sudo apt-get install autoconf

或者直接:

sudo apt-get install autoconf
因为autoconf 依赖于m4,所以会自动下载解决这个依赖关系.


继续执行命令:

 ./configure --with-php-config=/opt/lampp/bin/php-config --prefix=/opt/lampp/

 make

make test

make install


在 /opt/lampp/lib/php/extensions/no-debug-on-zts-20060613/ 下查看,会看到已经有 xhprof.so 了,复制到/opt/lampp/modules/xhprof.so

配置 php.ini

 在 /opt/lampp/etc/php.ini 中添加

[xhprof]
extension=/opt/lampp/modules/xhprof.so
xhprof.output_dir=/opt/lampp/var/xhprof/


注意,创建/opt/lampp/var/xhprof/要给相应的权限


 重启服务器后,就可以看到phpinfo中xhprof的信息了,如下图:





使用XHProf:


// start profiling
xhprof_enable();

// run program
....

// stop profiler
$xhprof_data = xhprof_disable();

//
// Saving the XHProf run
// using the default implementation of iXHProfRuns.
//
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();

// Save the run under a namespace "xhprof_foo".
//
// **NOTE**:
// By default save_run() will automatically generate a unique
// run id for you. [You can override that behavior by passing
// a run id (optional arg) to the save_run() method instead.]
//
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");

echo "---------------\n".
"Assuming you have set up the http based UI for \n".
"XHProf at some address, you can view run at \n".
"http://<xhprof-ui-address>/index.php?run=$run_id&source=xhprof_foo\n".
"---------------\n";


如此一来,会在上面设定的xhprof.output_dir目录里生成名字类似49bafaa3a3f66.xhprof_foo的数据文件,可以很方便的通过Web方式浏览效果:

http://<xhprof-ui-address>/index.php?run=49bafaa3a3f66&source=xhprof_foo

使用graphviz


为了有更美观的界面,安装graphviz:
wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz
tar zxf graphviz-2.24.0.tar.gz
cd graphviz-2.24.0
./configure
make
make install


安装完成后,会生成/usr/local/bin/dot文件,你应该确保路径在PATH环境变量里,以便XHProf能找到它。


当点击[View Full Callgraph]后提示以下的错误:

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.


google了一下,需要安装 libpng ( http://www.cnxct.com/you-do-not-have-dot-image-generation-utility-installed/ )



1. 先安装 http://sourceforge.net/projects/libpng/files/zlib/1.2.3/zlib-1.2.3.tar.gz/download, configure, make install, make

2. 再安装  http://sourceforge.net/projects/libpng/files/libpng15/1.5.1/ ,configure, make install, make, 安装完毕。

3. 最后,再编译 graphviz, 注意 ./configure --with-png=yes

再点击一下 [View Full Callgraph], 就能看到类似于下图:





[文章标题] 在ubuntu,lampp下使用xhprof

[文章作者]曾健生

[作者邮箱]zengjiansheng1@126.com

[作者QQ]190678908

[博客]  http://blog.csdn.net/newjueqi




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

newjueqi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值