# <directory_for_htdocs> - web可访问的目录,比如/www/xhprof,对应xhprof.***.com
#<directory_for_storing_xhprof_runs> - xhprof数据存放的目录,比如/www/xhprof/xhprof_data,需要有web用户写入的权限,比如web用户为www
chown -hR www /www/xhprof/xhprof_data
#安装 xhprof扩展
wget http://pecl.php.net/get/xhprof-0.9.2.tgz
tar zxvf xhprof-0.9.2.tgz
cd xhprof-0.9.2
cp -r xhprof_html xhprof_lib <directory_for_htdocs> # 应用程序所在目录
cd extension
/usr/local/webserver/php/bin/phpize
./configure --with-php-config=/usr/local/webserver/php/bin/php-config
make
make install
#配置php.ini,在extension附近加上
extension=xhprof.so
; 存放目录
xhprof.output_dir=<directory_for_storing_xhprof_runs>
#如果要图形话,需要安装dot
wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz
tar zxvf graphviz-2.24.0.tar.gz
cd graphviz-2.24.0
./configure
make
make install
#修改/etc/profile,加入dot的PATH
PATH="$PATH:/usr/local/bin/dot"
export PATH
#使用
#在php的头部加上,可以设置记录频率,比如记录万分之一
$xhprof_flag = mt_rand(1, 10000);
if ($xhprof_flag === 1) {
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
}
#在代码页的底部加上
if ($xhprof_flag) {
$xhprof_data = xhprof_disable();
include_once "./xhprof_lib/utils/xhprof_lib.php";
include_once "./xhprof_lib/utils/xhprof_runs.php";
$xhprof_runs = new XHProfRuns_Default();
$xhprof_runs->save_run($xhprof_data, 'xhprof',"index_".time());
}
#当有该php的访问时,会在<directory_for_storing_xhprof_runs>目录下生成类似index_1277956324.xhprof的记录文件
#使用类似 http://xhprof.***.com/xhprof_html/index.php?run=index_1277956324,就可以看到结果