xhprof以pear扩展的形式安装
下载:wget http://pecl.php.net/get/xhprof-0.9.4.tgz
解压之后的目录结构:
其中extension目录是xhprof扩展目录,在该目录下执行phpize && ./configure && make && make install即可生成xhprof.so扩展
xhprof_lib目录是xhprof的库函数目录,xhprof_html目录主要用于展示生成的分析结果
1、编译xhprof扩展
解压之后在extension目录执行 phpize && ./configure && make && make install 即可在php的扩展目录下生成对应的xhprof.so
在php.ini中添加
extension=xhprof.so
xhprof.output_dir=/tmp
xhprof.output_dir指定了分析结果的输出存放目录。
2、分析结果展示web server搭建
在nginx.conf中增加server
server {
listen 80;
server_name 192.168.0.108;
root /data1/www/htdocs/xhprof/;
location / {
fastcgi_pass 127.0.0.1:8999;
fastcgi_param SCRIPT_URL $script_uri;
include fastcgi/comm_fastcgi_params;
}
}
将解压之后的文件夹下的xhprof_html和xhprof_lib拷贝到 server的root目录下
3、在代码中添加xhprof
<?php
require_once 'xhprof_lib/utils/xhprof_lib.php';
require_once 'xhprof_lib/utils/xhprof_runs.php';
xhprof_enable(
XHPROF_FLAGS_MEMORY|XHPROF_FLAGS_CPU,
[
'ignored_functions' => [
'call_user_func',
'call_user_func_array'
]
]
);
$xhprofData = xhprof_disable();
$xhprofRuns = new XHProfRuns_Default();
$runId = $xhprofRuns->save_run($xhprofData, 'xhprof_test');
echo 'http://192.168.0.108/xhprof_html/index.php?run=' . $runId . '&source=xhprof_test';
需要注意的是,必须包含xhprof_lib下面的两个文件。
生成的分析结果存在php.ini的xhprof.output_dir选项指定的目录下
代码的最后输出的就是xhprof分析结果的url地址,通过访问该url,即可直观的查看分析报告