1.$ wget https://pecl.php.net/get/xhprof-0.9.4.tgz
2. tar zxvf xhprof-0.9.4.tgz
3. cd xhprof-0.9.4/extension/
4. phpize
5. ./configure --with-php-config=/usr/local/php5628/bin/php-config
6. make && make install
7. vim /usr/local/php5628/lib/php.ini
[Xhprof]
extension=xhprof.so;
output_dir=/tmp/xhprof
当你在一次请求中多次调用xhprof_enable方法,只有第一次调用时进行的设置能生效。在调用xhprof_disable()后,你又可以使用xhprof_enable方法进行设置。
example1
<?php
function abc(){
$s = str_repeat('1', 1024);
}
xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
//XHPROF_FLAGS_NO_BUILTINS (integer)
使得跳过所有内置(内部)函数。
XHPROF_FLAGS_CPU (integer)
使输出的性能数据中添加 CPU 数据。
XHPROF_FLAGS_MEMORY (integer)
使输出的性能数据中添加内存数据。
sleep(1);
abc();
$data = xhprof_disable();
print_r($data);
?>
Array
(
[main()==>sleep] => Array
(
[ct] => 1 #sleep函数调用次数
[wt] => 1000850 #消耗的时间,单位微秒
[cpu] => 346 #cpu时间,微秒
[mu] => 856 #使用的内存,bytes
[pmu] => 0 #使用的内存峰值,bytes
)
[abc==>str_repeat] => Array
(
[ct] => 1
[wt] => 65
[cpu] => 70
[mu] => 1808
[pmu] => 0
)
[main()==>abc] => Array
(
[ct] => 1
[wt] => 77
[cpu] => 80
[mu] => 1824
[pmu] => 0
)
[main()==>xhprof_disable] => Array
(
[ct] => 1
[wt] => 1
[cpu] => 2
[mu] => 768
[pmu] => 0
)
[main()] => Array
(
[ct] => 1
[wt] => 1001469
[cpu] => 566
[mu] => 5064
[pmu] => 0
)
)
example2 UI
<?php
function bar($x) {
if ($x > 0) {
bar($x - 1);
}
}
function foo() {
for ($idx = 0; $idx < 5; $idx++) {
bar($idx);
$x = strlen("abc");
}
}
// start profiling
xhprof_enable();
// run program
foo();
// stop profiler
$xhprof_data = xhprof_disable();
// display raw xhprof data for the profiler run
print_r($xhprof_data);
$XHPROF_ROOT = realpath(dirname(__FILE__));
//$XHPROF_ROOT = '/usr/local/src/xhprof-0.9.4';
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_lib.php";
include_once $XHPROF_ROOT . "/xhprof_lib/utils/xhprof_runs.php";
// save raw data for this profiler run using default
// implementation of iXHProfRuns.
$xhprof_runs = new XHProfRuns_Default();
$xhprof_runs = new XHProfRuns_Default();
// save the run under a namespace "xhprof_foo"
$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";
必须把xhprof_lib和xhprof_html目录拷贝到web目录下
cp -r xhprof_html/ /usr/local/nginx/html/xhprof/
cp -r xhprof_lib/ /usr/local/nginx/html/xhprof/另外还要安装 yum install graphviz
这样通过在通过在浏览器中输入http://192.168.0.107/xhprof/xhprof_html/?