我玩的应用:
   
  
 | 
	
 
XHProf是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开 关来控制是否进行profile。 
   
  以下是记录和总结: 
   
  安装xhprof: 
  wget http://pecl.php.net/get/xhprof-0.9.2.tgz 
  tar zxf xhprof-0.9.2.tgz 
  cd xhprof-0.9.2 
  cp -r xhprof_html xhprof_lib /www/www.hx.com/xhprof/ 
  cd extension/ 
  /usr/local/webserver/php/bin/phpize 
  ./configure  –with-php-config=/usr/local/webserver/php/bin/php-config 
  make && make install 
   
  安装完提示: 
  Installing shared extensions:     /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/ 
   
  php.ini中添加 
  extension_dir = \"/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/\" 
  这句我原来就有了,就这用添加下面两句 
  extension=xhprof.so 
  xhprof.output_dir=/www/logs/xhprof 
   
  分析日志输出在/www/logs/xhprof目录。 
   
   
  重新加载php配置文件和重启web 
  /usr/local/webserver/php/sbin/php-fpm reload 
  /usr/local/webserver/nginx/sbin/nginx -s reload 
   
  刷新phpinfo页面,看到输出中有了xhprof信息。 
  xhprof  0.9.2 
  CPU num  2 
   
  安装graphviz,一个画图工具 
  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 
   
  Graphviz生成图形。编译Graphviz遇到一些问题,主要是”/usr/lib/libexpat.so: could not read symbols: File in wrong format”,使用下面congfigure命令便可以正确编译。 
  ./configure --disable-swig LDFLAGS=\"-L/usr/lib64 -L/lib64\" 
   
  程序试例 
   
  头部:- if (@$_GET[\'xhprof\'] == 1) {
 -     /* -----------------xhprof------------------------ */
 -     xhprof_enable();
 - //xhprof_enable(XHPROF_FLAGS_NO_BUILTINS); //不记录内置的函数
 - //xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); //同时分析CPU和Mem的开销
 -     $xhprof_on = true;
 -     /* -----------------xhprof------------------------ */
 - }
 
  复制代码 
尾部:- if (@$_GET[\'xhprof\'] == 1) {
 -     /* -----------------xhprof------------------------ */
 - // stop profiler
 -     $xhprof_data = xhprof_disable();
 - // display raw xhprof data for the profiler run
 - //print_r($xhprof_data);
 -     $XHPROF_ROOT = realpath(dirname(__FILE__) . \'/..\');
 -     $XHPROF_ROOT = \'/data/apache/www/shao/xhprof\';
 -     include_once $XHPROF_ROOT . \"/xhprof_lib/utils/xhprof_lib.php\";
 -     include_once $XHPROF_ROOT . \"/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 \"
 - \" .
 -      \"<a target=\"\" _blank=\"\" href=\"http://shaogx.com/\" http:=\"\"  domain=\"\" cc=\"\" xhprof=\"\" xhprof_html=\"\" index=\"\" php=\"\"  run=\"$run_id&source=xhprof_foo\">XHProf
 - \" .
 -     \"
 - \";
 -     /* -----------------xhprof------------------------ */
 - }
 
  复制代码 
运行程序,底部出现统计字样,点过去就可以看到性能分析了。按运行时间排序,很容易找出化时间最长的函数。 
  点[View Full Callgraph]图形化显示,最大的性能问题会用红色标出,其次是黄色,很明显。 
  官方文档:http://mirror.facebook.net/facebook/xhprof/doc.html 
  名词解释 
  主要的 
  Inclusive Time (或子树时间):包括子函数所有执行时间。 
  Exclusive Time/Self Time:函数执行本身花费的时间,不包括子树执行时间。 
  Wall时间:花去了的时间或挂钟时间。 
  CPU时间:用户耗的时间+内核耗的时间 
   表单中的 
  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% 峰值百分比 
   
  from:http://blog.163.com/lgh_2002/blog/static/4401752620115243821744/ |   
 
 
 
 |