Xhprof的安装与使用












关于 XHProf

XHProf FaceBook 开发的一个函数级别的 PHP 分层分析器。数据收集部分是一个基于 C PHP 扩展,分析报告是一系列基于 PHP HTML 导航页面。XHProf 能统计每个函数的调用次数、内存使用、CPU占用等多项重要的数据。并且 XHProf 还能比较两个统计样本,或从多个数据样本中汇总结果。XHProf 是分析 PHP 程序执行效率的利器,能让我们得到更底层的的分析数据。

安装测试使用xhprof

1.下载(下载最新版本)
wgethttp://pecl.php.net/get/xhprof-0.9.3.tgz
tar zxf xhprof-0.9.3.tgz

[root@F2C-1 xhprof_lib]# cd ..

[root@F2C-1 xhprof-0.9.3]# ls

CHANGELOG  CREDITS  examples  extension LICENSE  README  xhprof_html  xhprof_lib

[root@F2C-1 xhprof-0.9.3]# cd extension/

[root@F2C-1 extension]# ll

total 72

-rw-r--r-- 1 4001766 3664278   196 May 20 13:41config.m4

-rw-r--r-- 1 4001766 3664278  1191 May 20 13:41php_xhprof.h

drwxr-xr-x 2 root    root     4096Jul  5 11:03 tests

-rw-r--r-- 1 4001766 3664278 58111 May 20 13:41 xhprof.c

[root@F2C-1 extension]# phpize

Configuring for:

PHP Api Version:         20100412

Zend Module Api No:      20100525

Zend Extension Api No:   220100525

[root@F2C-1 extension]# ll

total 1280

-rw-r--r-- 1 root    root     78136Jul  5 11:11 acinclude.m4

-rw-r--r-- 1 root    root    308460Jul  5 11:11 aclocal.m4

drwxr-xr-x 2 root    root     4096 Jul  5 11:11 autom4te.cache

drwxr-xr-x 2 root    root     4096 Jul  5 11:11 build

-rwxr-xr-x 1 root    root     44893Jul  5 11:11 config.guess

-rw-r--r-- 1 root    root     1534 Jul  5 11:11 config.h.in

-rw-r--r-- 1 4001766 3664278    196 May 2013:41 config.m4

-rwxr-xr-x 1 root    root     33399Jul  5 11:11 config.sub

-rwxr-xr-x 1 root    root    447523Jul  5 11:11 configure

-rw-r--r-- 1 root    root     4690 Jul  5 11:11 configure.in

-rw-r--r-- 1 root    root        0 Jul  5 11:11 install-sh

-rw-r--r-- 1 root    root    199728Jul  5 11:11 ltmain.sh

-rw-r--r-- 1 root    root     5525 Jul  5 11:11 Makefile.global

-rw-r--r-- 1 root    root        0 Jul  5 11:11 missing

-rw-r--r-- 1 root    root        0 Jul  5 11:11 mkinstalldirs

-rw-r--r-- 1 4001766 3664278   1191 May 20 13:41php_xhprof.h

-rw-r--r-- 1 root    root     78556Jul  5 11:11 run-tests.php

drwxr-xr-x 2 root    root     4096 Jul  5 11:03 tests

-rw-r--r-- 1 4001766 3664278  58111 May 20 13:41xhprof.c

[root@F2C-1 extension]#./configure--with-php-config=/home/lnamp/php540/bin/php-config

[root@F2C-1 extension]# make && make test

[root@F2C-1 extension]# make install

Installing shared extensions:     /home/lnamp/php540/lib/php/extensions/no-debug-non-zts-20100525/


cd /etc/php.d

touch xhprof.ini

然后在xhprof.ini中加入

extension=xhprof.so

保存,

重启php和nginx服务


servicephp-fpm restart

servicenginx restart



在phpinfo()里可以看到这个模块

10.0.16.104/test/info

5.使用xhprof

1) 在待测php文件中加入如下函数:

xhprof_enable(); //统计的代码部分之前加

如需要监测CPU和内存,则为xhprof_enable(XHPROF_FLAGS_CPU+ XHPROF_FLAGS_MEMORY);

xhprof_disable();//统计的代码部分之后加

例如:


2通过xhprof_html目录的http访问url来查看结果。如:http://<域名>/xhprof_html/?run=id&source=命名空间

例如:以whm.imp.com为例


6.安装graphviz用以支持图形查看:(此步骤可省)
1)下载

wget www.graphviz.org/pub/graphviz/stable/SOURCES//graphviz-2.32.0.tar.gz
2)解压后安装

tar zxf graphviz-2.32.0.tar.gz

./configure
make && make install

安装成功后,在分析结果页面点击[View Full Callgraph],出现数据分析图,如下: