使用xhprof分析php性能

时间:2022-10-15 14:55:52

今天偶然发现 xhprof可以远程分析php代码性能,大致步骤如下

1.  进入 xhprof , 点击右上角注册 并 登陆, 网站左侧解释了如何在本地安装测试xhprof, 我用的是右侧的图表模式, 这样可以省去本地搭建的步骤。

2.  在本地编译安装 xhprof扩展, 这个和普通扩展一样

  wget http://pecl.php.net/get/xhprof-0.9.3.tgz
tar zxvf xhprof-0.9.2.tgz
cd xhprof-0.9.2/extension/
sudo phpize
./configure --with-php-config=/usr/local/php/bin/php-config
sudo make
sudo make install
或者 yum install php-xhprof

3.  下载xhprof 提供的埋点脚本,并include到想要测试的页面中

wget 'http://www.xhprof.com/lib_xhprof_com.php?username=***&&password=****************' -O /tmp/lib_xhprof_com.php

在测试页面最开始包含
include_once('/tmp/lib_from_xhprof_com.php');
# 如果是线上,最好添加概率
if (mt_rand(1, 1000) == 1) {
include_once('/tmp/lib_from_xhprof_com.php');
}

4.  点击访问 图表模式 xhprof  就可以看到自己网站的性能信息了。

5.  可以对性能信息进行排序, 筛选url等, 非常方便

6.  可以保存自己觉得有特征的性能信息(网站默认只存储300条没有选择保存的性能信息,多余的自动清除), 当然也可以删除性能信息。