Mac 下安装xhprof

首先下载xhprof安装包,下载地址xhprof

解压安装包

tar zxvf xhprof-0.9.4.tgz

进入安装包目录

cd xhprof-0.9.4

编译

phpize

报错:

Cannot find config.m4. 
Make sure that you run '/usr/local/bin/phpize' in the top level source directory of the module

这是由于没有进入扩展安装目录

cd extension/

再次执行phpize

phpize

报错

/usr/local/bin/phpize: line 61: /usr/local/Library/ENV/4.3/sed: No such file or directory
/usr/local/bin/phpize: line 62: /usr/local/Library/ENV/4.3/sed: No such file or directory
/usr/local/bin/phpize: line 63: /usr/local/Library/ENV/4.3/sed: No such file or directory
Configuring for:
PHP Api Version:        
Zend Module Api No:     
Zend Extension Api No:  
/usr/local/bin/phpize: line 155: /usr/local/Library/ENV/4.3/sed: No such file or directory
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

这是没有安装autoconfm4

下载autoconf 和m4 autoconfm4

解压,安装

tar zxvf autoconf-latest.tar.gz

cd autoconf-2.69/

./configure 

make && make install

tar zxvf m4-1.4.18.tar.gz

cd m4-1.4.18

./configure

make && make install

再次进入扩展目录执行phpize

cd xhprof-0.9.4/extension

phpize

报错

/usr/local/bin/phpize: line 155: /usr/local/Library/ENV/4.3/sed: No such file or directory

解决参考MAC 安装扩展执行PHPIZE报错

再次执行phpize,结果显示

Configuring for:
PHP Api Version:         20151012
Zend Module Api No:      20151012
Zend Extension Api No:   320151012

应该是成功了

继续执行编译安装

./configure --with-php-config=/usr/local/bin/php-config

sudo make && make install

报了一大串错误,各种搜索,最好在github 的issue上找到这个版本不支持php7,心累。。。

好在也有解决方案 xhprof php7

重新来过,下载新的安装报执行上面的步骤 xhprof

结果显示:

Installing shared extensions:     /usr/local/Cellar/php70/7.0.4/lib/php/extensions/no-debug-non-zts-20151012/

配置php.ini

extension_dir = 'ext' //修改扩展所在目录

[xhprof]
extension = xhprof.so
xhprof.output_dir = "/www/xhprof"  //最高权限

重启服务器,安装成功。

代码测试

<?php 
 function vilay()
 {
		phpinfo();
 }

 xhprof_enable(XHPROF_FLAGS_CPU|XHPROF_FLAGS_MEMORY);
 vilay();
 $data = xhprof_disable();
 echo "<pre>";
 var_dump($data);

结果显示:

array(4) {
	["vilay==>phpinfo"]=>
	array(5) {
		["ct"]=>
		int(1)
		["wt"]=>
		int(4116)
		["cpu"]=>
		int(4273)
		["mu"]=>
		int(99432)
		["pmu"]=>
		int(159840)
	}
	["main()==>vilay"]=>
	array(5) {
		["ct"]=>
		int(1)
		["wt"]=>
		int(4306)
		["cpu"]=>
		int(4285)
		["mu"]=>
		int(100008)
		["pmu"]=>
		int(159840)
	}
	["main()==>xhprof_disable"]=>
	array(5) {
		["ct"]=>
		int(1)
		["wt"]=>
		int(0)
		["cpu"]=>
		int(1)
		["mu"]=>
		int(520)
		["pmu"]=>
		int(0)
	}
	["main()"]=>
	array(5) {
		["ct"]=>
		int(1)
		["wt"]=>
		int(4639)
		["cpu"]=>
		int(4609)
		["mu"]=>
		int(101240)
		["pmu"]=>
		int(159840)
	}
}

结果解释:

ct:表示调用次数

mt:表示执行代码消耗的时间

cpu:函数方法执行消耗的cpu时间

mu:函数方法所使用的内存

pmu:函数方法所使用的内存峰值

xhprof_disable()提供了三个常量参数

XHPROF_FLAGS_NO_BUILTINS :设置这个常量后,将不统计PHP内置函数

XHPROF_FLAGS_CPU :统计进程占用CPU时间。由于CPU时间是通过调用系统调用getrusage获取,导致性能比较差。开启这个选项后,性能可能下降一半,所以生产环境不太建议使用

XHPROF_FLAGS_MEMORY:统计内存占用情况。获取内存情况,使用的是zend_memory_usage和zend_memory_peak_usage,并不是系统调用,对性能影响不大,有需要可以使用。