xhprof php,PHP性能分析(xhprof)

[TOC]

### 1. 下载xhprof源码并安装

> https://github.com/longxinH/xhprof.git

```

cd xhprof/extension/

phpize

./configure --with-php-config=/www/server/php/71/bin/php-config --enable-xhprof

make && make install

```

> 修改php.ini配置

```

[xhprof]

extension = xhprof.so

xhprof.output_dir = /tmp/xhprof

```

### 2. Yii2集成xhprof

> 将xhprof下的xhprof_html以及xhprof_lib拷贝至项目中

~~~

public function actionTest1()

{

error_reporting(E_ALL);

ini_set('display_errors', 'on');

$this->xhprofStart();

for ($i = 0; $i < 83000; $i++) {

$array[] = $i;

}

$this->haha();

$this->xhprofEnd();

}

public function haha(){

echo 'haha';

}

//xhprof监控开始

public function xhprofStart(){

xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);

}

//xhprof监控结束

public function xhprofEnd(){

$xhprof_data = xhprof_disable();

dump($xhprof_data);

$xhprof_root = '/www/wwwroot/liwu4/frontend/web/';

include_once $xhprof_root."xhprof_lib/utils/xhprof_lib.php";

include_once $xhprof_root."xhprof_lib/utils/xhprof_runs.php";

$xhprof_runs = new \XHProfRuns_Default();

$route = \Yii::$app->controller->route;

$run_id = $xhprof_runs->save_run($xhprof_data, $route);

echo '统计
';

$domain = $_SERVER['SERVER_NAME'];

$url = "http://".$domain."/xhprof_html/index.php?run=$run_id&source=$route";

echo ''.$url.'';

}

~~~

> 在运行的时候,可能会没有监控数据,报Invalid Run Id的错误

> 解决办法

```

chmod -R 777 /tmp/xhprof

```

> 修改xhprof_lib/utils/xhprof_runs.php中找到save_run()方法

~~~

$file_name = $this->file_name($run_id, $type); // 找到此代码,新增以下代码

// 增加以下创建文件夹的代码,才能生成报表数据

$dir = dirname($file_name);

if (!is_dir($dir)) {

mkdir($dir, 0777, true);

}

~~~

### 3. 运行并测试

> http://www.xx.com/wangkun/test1

![](https://img.kancloud.cn/83/38/8338047e814ae3f073a56ce44461e16c_962x546.png)

> 由于xhprof属于非侵入性,可以运用到正式环境,常用URL中加入code方式,输入正确的code情况下走xhprof

> xdebug属于侵入式性能分析,安装好了php插件后,页面就自动开启,只能运用在测试环境

> 参考此文 https://blog.csdn.net/maquealone/article/details/80434699

### 4. 图形化结果

> 点击[View Full Callgraph]可以看图形化结果,会报错“failed to execute cmd " dot -Tpng"”

> 解决办法

~~~

yum install graphviz

~~~

> 安装完扩展后,需要修改php.ini,在disable_functions中把proc_open去掉。

![](https://img.kancloud.cn/3b/c6/3bc66d56babc002d5ed3da720de148f7_1797x543.png)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值