xhprof是Facebook开源的轻量级PHP性能分析工具
1、下载并安装扩展
http://pecl.php.net/package/xhprof
解压后会有目录
CHANGELOG CREDITS examples extension LICENSE README xhprof_html xhprof_lib
2、extension,为扩展编译目录,编译安装即可
3、xhprof_html xhprof_lib,可视化的一些性能分析页面目录,将其复制到可访问的web站点根目录,后续会用到
4、在需要性能分析的代码开头加上
// start profiling
xhprof_enable();
结束位置加上
// stop profiler
$xhprof_data = xhprof_disable();
// display raw xhprof data for the profiler run
print_r($xhprof_data);
include_once "./xhprof/xhprof_lib/utils/xhprof_lib.php";
include_once "./xhprof/xhprof_lib/utils/xhprof_runs.php";
// save raw data for this profiler run using default
// implementation of iXHProfRuns.
$xhprof_runs = new XHProfRuns_Default();
// save the run under a namespace "xhprof_foo"
$run_id = $xhprof_runs->save_run($xhprof_data, "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、执行要性能分析的php代码,会看到有类似输出,/index.php?run=5b2917561bc37&source=xhprof_foo,复制它
6、访问,web站点根目录/xhprof_html/index.php?run=5b2917561bc37&source=xhprof_foo,会看到一些性能分析表格

7、点击中间的 View Full Callgraph,即可看见性能分析图片。
如果报错
failed to execute cmd:" dot -Tpng". stderr:sh: dot:command not found。
linux环境安装
yum install graphviz
8、红色的部分为性能比较低,耗时比较长的部分,我们可以根据根据哪些函数被标记为红色对系统的代码进行优化
9、补充说明一些性能指标含义
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峰值百分比。