1,安装XHProf:
1
2
3
4
5
6
7
8
9
10
11
12
|
wget http:
//pecl
.php.net
/get/xhprof-0
.9.2.tgz
tar
zxf xhprof-0.9.2.tgz
cd
xhprof-0.9.2
mkdir
-p
/web/xhprof/output
cp
-r xhprof_html xhprof_lib
/web/xhprof
#你的网站根目录下的xhprof目录
chown
-R web.web
/web/xhprof
cd
extension
phpize
.
/configure
make
make
install
make
test
#可以看一下是否通过测试
|
编辑 /etc/php.ini:
1
2
3
4
5
6
7
8
|
[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=/web/xhprof/output
|
重启PHP服务(/etc/init.d/php-fpm restart)让修改生效,现在就可以使用XHProf了,不过为了显示效果更炫,最好继续安装Graphviz。
安装libpng和Graphviz:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#必须编译安装,不要yum install libpng
wget http:
//sourceforge
.net
/projects/libpng/files/libpng15/1
.5.11
/libpng-1
.5.11.
tar
.gz
/download
tar
zxf libpng-1.5.11.
tar
.gz
cd
libpng-1.5.11
.
/configure
make
make
install
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
dot -V
#确认是否可以使用dot程序
|
安装完成后,会生成/usr/local/bin/dot文件,你应该确保路径在PATH环境变量里,以便XHProf能找到它。
使用XHProf:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
// start profiling
xhprof_enable();
// run program
sleep(1);
// stop profiler
$xhprof_data
= xhprof_disable();
//
// Saving the XHProf run
// using the default implementation of iXHProfRuns.
//
$XHPROF_ROOT
=
"/web/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();
// 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
"<a href='/xhprof/xhprof_html/index.php?run=$run_id&source=xhprof_foo'>[display xhprof report]</a>"
;
|
如此一来,会在上面设定的xhprof.output_dir目录里生成名字类似49bafaa3a3f66.xhprof_foo的数据文件,可以很方便的通过Web方式浏览效果。
你应该看到:
Function Name | Calls | Calls% | Incl. Wall Time (microsec) | IWall% | Excl. Wall Time (microsec) | EWall% |
---|---|---|---|---|---|---|
main() | 1 | 33.3% | 1,000,135 | 100.0% | 40 | 0.0% |
sleep | 1 | 33.3% | 1,000,094 | 100.0% | 1,000,094 | 100.0% |
xhprof_disable | 1 | 33.3% | 1 | 0.0% | 1 | 0.0% |
点击[View Full Callgraph]就可以看到图片: