xhprof安装与使用

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/?
这里写图片描述

这里写图片描述

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值