xhprof简介

用下面的示例程序来试着产生一些原始的性能数据:

foo.php


<?php

function bar($x) {
  if ($x > 0) {
    bar($x - 1);
  }
}

function foo() {
  for ($idx = 0; $idx < 2; $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);

运行上面的测试程序:

% php -dextension=xhprof.so foo.php

你应该得到的输出如下:

Array
(
    [foo==>bar] => Array
        (
            [ct] => 2         # 2 calls to bar() from foo()
            [wt] => 27        # inclusive time in bar() when called from foo()
        )

    [foo==>strlen] => Array
        (
            [ct] => 2
            [wt] => 2
        )

    [bar==>bar@1] => Array    # a recursive call to bar()
        (
            [ct] => 1
            [wt] => 2
        )

    [main()==>foo] => Array
        (
            [ct] => 1
            [wt] => 74
        )

    [main()==>xhprof_disable] => Array
        (
            [ct] => 1
            [wt] => 0
        )

    [main()] => Array         # fake symbol representing root
        (
            [ct] => 1
            [wt] => 83
        )

)

注:The raw data only contains "inclusive" metrics. For example, the wall time metric in the raw data represents inclusive time in microsecs. Exclusive times for any function are computed during the analysis/reporting phase.

注意:默认情况下只有调用次数和占用时间是记录了的。您也可以选择同时记录CPU时间和/或单纯记录内存使用量。将下面这一行:

xhprof_enable();

换成:

xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

您应该得到如下的输出:

Array
(
    [foo==>bar] => Array
        (
            [ct] => 2        # number of calls to bar() from foo()
            [wt] => 37       # time in bar() when called from foo()
            [cpu] => 0       # cpu time in bar() when called from foo()
            [mu] => 2208     # change in PHP memory usage in bar() when called from foo()
            [pmu] => 0       # change in PHP peak memory usage in bar() when called from foo()
        )

    [foo==>strlen] => Array
        (
            [ct] => 2
            [wt] => 3
            [cpu] => 0
            [mu] => 624
            [pmu] => 0
        )

    [bar==>bar@1] => Array
        (
            [ct] => 1
            [wt] => 2
            [cpu] => 0
            [mu] => 856
            [pmu] => 0
        )

    [main()==>foo] => Array
        (
            [ct] => 1
            [wt] => 104
            [cpu] => 0
            [mu] => 4168
            [pmu] => 0
        )

    [main()==>xhprof_disable] => Array
        (
            [ct] => 1
            [wt] => 1
            [cpu] => 0
            [mu] => 344
            [pmu] => 0
        )

    [main()] => Array
        (
            [ct] => 1
            [wt] => 139
            [cpu] => 0
            [mu] => 5936
            [pmu] => 0
        )

)

在分析时跳过内置函数

默认情况下, PHP的内置函数(如strlen )也被分析了。如果您不希望分析内置函数(为了进一步减少性能分析的开销貌相或减小产生的原始数据) ,您可以使用XHPROF_FLAGS_NO_BUILTINS标 志,例如:

// do not profile builtin functions xhprof_enable( XHPROF_FLAGS_NO_BUILTINS );

在性能分析时忽略特定函数( 0.9.2或更高版本支持)

从0.9.2版本的xhprof ,你可以告诉XHProf在性能分析时忽略一些指定函数。这样,您就可以忽略,像功能用于间接函数调用,如call_user_func和call_user_func_array的函数 。这些不必要的中间层,因为他们乱七八糟的父-子关系和间接的互相调用,使XHProf报告难以理解。

要设定要忽略的函数列表,可以在分析时给xhprof_enable函数 指定第二个参数[是个可选参数] 。例如,

// elapsed time profiling; ignore call_user_func* during profiling
xhprof_enable(0, array('ignored_functions' =>  array('call_user_func','call_user_func_array')));

or,

// elapsed time + memory profiling; ignore call_user_func* during profiling
xhprof_enable(XHPROF_FLAGS_MEMORY,array('ignored_functions' => array('call_user_func', 'call_user_func_array')));