简介
Xhprof能用来做什么?
XHProf是一个分层PHP性能分析工具。它报告函数级别的请求次数和各种指标,包括阻塞时间,CPU时间和内存使用情 况。一个函数的开销,可细分成调用者和被调用者的开销。
Xhprof 安装
Xhprof安装
wget http://pecl.php.net/get/xhprof-0.9.2.tgz tar xzvf xhprof-0.9.2.tgz cp -r xhprof_html xhprof_lib /usr/local/www/ cd xhprof-0.9.2/extension/ /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make make install
修改php.ini,添加xhprof.so (注意xhprof.so需要在extension_dir目录下)
extension=xhprof.so xhprof.output_dir=/var/logs/xhprof
如果出现xhprof.lo错误,可能是版本兼容的问题,请更换xhprof的版本执行,下载地址xhprof-0.9.4,重新再执行上面操作
graphviz安装(性能数据也可以通过callgraph视图来查看 。callgraph 会高亮显示程序的关键路径。)
wget http://www.graphviz.org/pub/graphviz/stable/SOURCES/graphviz-2.24.0.tar.gz tar zxf graphviz-2.24.0.tar.gz cd graphviz-2.24.0 编译安装 ./configure make make install
Xhprof 使用
XHProf.class.php(本事例是将xhprof_html xhprof_lib XHProf.class.php demo.php放在同一级目录下面,需要web程序能够访问到)
<?php require_once 'xhprof_lib/display/xhprof.php'; require_once 'xhprof_lib/utils/xhprof_runs.php'; class Xhprof { public function start(array $ignored_funcs=array()) { xhprof_enable(XHPROF_FLAGS_NO_BUILTINS | XHPROF_FLAGS_CPU | XHPROF_FLAGS_MEMORY, array('ignored_funcs' => $ignored_funcs)); } public function end() { $data = xhprof_disable(); return $data; } public function createImg($data){ $source = "xhprof"; $threshold = 0.01; $type = "png"; $func = ''; $page = ""; $critical_path = true; $script = xhprof_generate_dot_script($data, $threshold, $source, $page, $func, $critical_path); $content = xhprof_generate_image_by_dot($script, $type); $img_name = uniqid().".png"; $img_path = "/data1/htdocs/xhprof.com/xhprof_img/".$img_name; file_put_contents($img_path, $content); return "<img src='http://xhprof.com/xhprof_img/".$img_name."'><img>"; } public function getHtml($data) { $xhprof_runs = new \XHProfRuns_Default(); $run_id = $xhprof_runs->save_run($data, "xhprof_foo"); return "<a href=\"http://xhprof.com/xhprof_html/index.php?run=$run_id&source=xhprof_foo\" target=\"iframe_a\">性能分析结果</a>"; } }
demo.php
<?php ini_set('display_errors', 1); include './XHProf.class.php'; $objProf = new XHProf(); function foo() { echo 'hello world'; } //开启xhprof并开始记录 $objProf->start(); //运行一些函数 foo(); //停止记录并取到结果 $data = $objProf->end(); $img = $objProf->createImg($data); $href = $objProf->getHtml($data); var_dump($img, $href);
结果查询
Function Name 函数名
Calls 调用次数
Calls% 调用百分比
Incl. Wall Time (microsec) 调用的包括子函数所有花费时间 以微秒算(一百万分之一秒)
IWall% 调用的包括子函数所有花费时间的百分比
Excl. Wall Time (microsec) 函数执行本身花费的时间,不包括子树执行时间,以微秒算(一百万分之一秒)
EWall% 函数执行本身花费的时间的百分比,不包括子树执行时间
Incl. CPU(microsecs) 调用的包括子函数所有花费的cpu时间。减Incl. Wall Time即为等待cpu的时间
减Excl. Wall Time即为等待cpu的时间
ICpu% Incl. CPU(microsecs)的百分比
Excl. CPU(microsec) 函数执行本身花费的cpu时间,不包括子树执行时间,以微秒算(一百万分之一秒)。
ECPU% Excl. CPU(microsec)的百分比
Incl.MemUse(bytes) 包括子函数执行使用的内存。
IMemUse% Incl.MemUse(bytes)的百分比
Excl.MemUse(bytes) 函数执行本身内存,以字节算
EMemUse% Excl.MemUse(bytes)的百分比
Incl.PeakMemUse(bytes) Incl.MemUse的峰值
IPeakMemUse% Incl.PeakMemUse(bytes) 的峰值百分比
Excl.PeakMemUse(bytes) Excl.MemUse的峰值
EPeakMemUse% EMemUse% 峰值百分比