环境:centos6.4 32 位
1、xhprof 下载 地址:https://pecl.php.net/package/xhprof
cd /home/
yum install wget
wget https://pecl.php.net/get/xhprof-0.9.4.tgz
tar -zxvf xhprof-0.9.4.tgz
cd xhprof-0.9.4
cd extension
2、安装
whereis phpize
phpize: /usr/bin/phpize /usr/share/man/man1/phpize.1.gz
#配置php环境
/usr/bin/phpize
-rw-r--r--. 1 root root 75570 Apr 10 10:02 acinclude.m4
-rw-r--r--. 1 root root 358139 Apr 10 10:02 aclocal.m4
drwxr-xr-x. 2 root root 4096 Apr 10 10:02 autom4te.cache
drwxr-xr-x. 2 root root 4096 Apr 10 10:01 build
-rwxr-xr-x. 1 root root 44892 Apr 10 10:02 config.guess
-rw-r--r--. 1 root root 1636 Apr 10 10:02 config.h.in
-rw-r--r--. 1 3055354 3664278 196 Oct 1 2013 config.m4
-rwxr-xr-x. 1 root root 33387 Apr 10 10:02 config.sub
-rwxr-xr-x. 1 root root 396905 Apr 10 10:02 configure
-rw-r--r--. 1 root root 4639 Apr 10 10:02 configure.in
-rw-r--r--. 1 root root 0 Apr 10 10:02 install-sh
-rw-r--r--. 1 root root 243248 Apr 10 10:02 ltmain.sh
-rw-r--r--. 1 root root 5462 Apr 10 10:02 Makefile.global
-rw-r--r--. 1 root root 0 Apr 10 10:02 missing
-rw-r--r--. 1 root root 0 Apr 10 10:02 mkinstalldirs
-rw-r--r--. 1 3055354 3664278 1191 Oct 1 2013 php_xhprof.h
-rw-r--r--. 1 root root 68683 Apr 10 10:02 run-tests.php
drwxr-xr-x. 2 root root 4096 Apr 10 09:51 tests
-rw-r--r--. 1 3055354 3664278 58111 Oct 1 2013 xhprof.c
#编译
编译之前需要看下文档怎么编译
cd /home/xhprof-0.9.4
-rw-r--r--. 1 3055354 3664278 3359 Oct 1 2013 CHANGELOG
-rw-r--r--. 1 3055354 3664278 266 Oct 1 2013 CREDITS
drwxr-xr-x. 2 root root 4096 Apr 10 09:51 examples
drwxr-xr-x. 5 root root 4096 Apr 10 10:02 extension
-rw-r--r--. 1 3055354 3664278 10174 Oct 1 2013 LICENSE
-rw-r--r--. 1 3055354 3664278 186 Oct 1 2013 README
drwxr-xr-x. 6 root root 4096 Apr 10 09:51 xhprof_html
drwxr-xr-x. 4 root root 4096 Apr 10 09:51 xhprof_lib
将2个文件拷到www目录下
cp -R xhprof_html/ /data/hx/test/xhprof_html
cp -R xhprof_lib/ /data/hx/test/xhprof_lib
/data/hx/test/ 是我的www 目录
通过url 访问 /data/hx/test/xhprof_html/docs/index.html
其中有下面这段
The steps below should work for Linux/Unix environments.
% cd <xhprof_source_directory>/extension/
% phpize
% ./configure --with-php-config=<path to php-config>
% make
% make install
% make test
php.ini file: You can update your php.ini file to automatically load your extension. Add the following to your php.ini file.
[xhprof]
extension=xhprof.so
;
; directory used by default implementation of the iXHProfRuns
; interface (namely, the XHProfRuns_Default class) for storing
; XHProf runs.
;
xhprof.output_dir=<directory_for_storing_xhprof_runs>
whereis php-config
php-config: /usr/bin/php-config /usr/share/man/man1/php-config.1.gz
./configure --with-php-config=/usr/bin/php-config
make
make install
ll /usr/lib/php/modules/
-rwxr-xr-x. 1 root root 76943 Apr 10 13:16 xhprof.so
vi /etc/php.ini
按键shift + g 跳到文件最后
; Local Variables:
; tab-width: 4
; End:
在上面的代码之前添加
[xhprof]
extension=/usr/lib/php/modules/xhprof.so
xhprof.output_dir=/tmp/xhprof/ #文件保存目录,需要手动创建, 确定当前用户对这个目录有读写权限
or
/etc/init.d/httpd reload
mkdir /tmp/xhprof/
使用phpinfo() 判断是否安装成功
出现上面的内容,说明安装成功了
3、使用
参考
a) Saving XHProf data persistently:
Assuming you are using the default implementation XHProfRuns_Default of the iXHProfRuns interface, a typical XHProf run followed by the save step might look something like:
// 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";
我的www 目录
[hx@localhost hx]$ cd test/
[hx@localhost test]$ ll
total 28
-rwxr--r--. 1 hx hx 19 Apr 10 13:31 index.php
drwxr-xr-x. 3 hx hx 4096 Mar 31 10:44 python
drwxr-xr-x. 2 hx hx 4096 Apr 7 14:07 shell
drwxr-xr-x. 2 hx hx 4096 Mar 17 11:53 soap
drwxr-xr-x. 6 hx hx 4096 Apr 10 10:06 xhprof_html
drwxr-xr-x. 4 hx hx 4096 Apr 10 10:06 xhprof_lib
drwxr-xr-x. 2 hx hx 4096 Mar 17 13:17 xmlrpc
index.php 内容
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
for ($i = 0; $i < 100; $i++) {
for ($j = 0; $j < 100; $j++) {
}
}
$xhprof_data = xhprof_disable();
require('./xhprof_lib/utils/xhprof_lib.php');
require('./xhprof_lib/utils/xhprof_runs.php');
$xhprof_runs = new XHProfRuns_Default();
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
header("location:/xhprof_html/index.php?run=$run_id&source=xhprof_foo");
直接访问index.php