1.克隆
git clone https://github.com/longxinH/xhprof
2.安装(phpize和./configure都要针对自己要安装的php版本)
cd xhprof/extension/
phpize
./configure --with-php-config=/usr/local/php/7.2/bin/php-config
make
make install
3.修改php.ini并重启
# vi /etc/php/7.2/php.ini
; 内容为:
extension = xhprof.so
; 注意:output_dir 必须存在且可写
xhprof.output_dir = /tmp/xhpro
然后重启apache服务
# service php-fpm restart 或 service httpd restart
4.使用
// start profiling
xhprof_enable(XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY);
// run program
....
// stop profiler
$xhprof_data = xhprof_disable();
//
// Saving the XHProf run
// using the default implementation of iXHProfRuns.
// include 下载的xhprof包
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();
// Save the run under a namespace "xhprof_foo".
//
// **NOTE**:
// By default save_run() will automatically generate a unique
// run id for you. [You can override that behavior by passing
// a run id (optional arg) to the save_run() method instead.]
// 两种方式写入
$run_id = $xhprof_runs->save_run($xhprof_data, "xhprof_foo");
/*file_put_contents(
"/srv/log/xhprof" . DIRECTORY_SEPARATOR . uniqid() . '.myapplication.xhprof',
serialize($xhprof_data)
);*/
//如果放在项目入口文件同目录,访问如下地址
http://test.com/xhprof_html/
http://test.com/xhprof_html/index.php?run=5cd528dc4fafd&source=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";
5.graphviz
graphviz是一个绘制图形的工具,可以更为直观的让你查看性能的瓶颈。
wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz
cd graphviz-2.24.0
./configure
make && make install
或者
yum install libpng
yum install graphviz
ubuntu
sudo apt-get install graphviz
6.补充解释
Function Name:方法名称。
Calls:方法被调用的次数。
Calls%:方法调用次数在同级方法总数调用次数中所占的百分比。
Incl.Wall Time(microsec):方法执行花费的时间,包括子方法的执行时间。(单位:微秒)
IWall%:方法执行花费的时间百分比。
Excl. Wall Time(microsec):方法本身执行花费的时间,不包括子方法的执行时间。(单位:微秒)
EWall%:方法本身执行花费的时间百分比。
Incl. CPU(microsecs):方法执行花费的CPU时间,包括子方法的执行时间。(单位:微秒)
ICpu%:方法执行花费的CPU时间百分比。
Excl. CPU(microsec):方法本身执行花费的CPU时间,不包括子方法的执行时间。(单位:微秒)
ECPU%:方法本身执行花费的CPU时间百分比。
Incl.MemUse(bytes):方法执行占用的内存,包括子方法执行占用的内存。(单位:字节)
IMemUse%:方法执行占用的内存百分比。
Excl.MemUse(bytes):方法本身执行占用的内存,不包括子方法执行占用的内存。(单位:字节)
EMemUse%:方法本身执行占用的内存百分比。
Incl.PeakMemUse(bytes):Incl.MemUse峰值。(单位:字节)
IPeakMemUse%:Incl.MemUse峰值百分比。
Excl.PeakMemUse(bytes):Excl.MemUse峰值。单位:(字节)
EPeakMemUse%:Excl.MemUse峰值百分比。