设为首页收藏本站
开启辅助访问
切换到宽版

创星网络[分享知识 传递快乐]

 找回密码
 立即注册

QQ登录

只需一步,快速开始

用新浪微博登录

只需一步,快速搞定

搜索
查看: 3934|回复: 0
打印 上一主题 下一主题

Facebook XHProf - php轻量级的性能分析工具

[复制链接]

我玩的应用:

跳转到指定楼层
楼主
发表于 2012-10-3 08:31:49 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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\"
  
  程序试例
  
  头部:
  1. if (@$_GET[\'xhprof\'] == 1) {
  2.     /* -----------------xhprof------------------------ */
  3.     xhprof_enable();
  4. //xhprof_enable(XHPROF_FLAGS_NO_BUILTINS); //不记录内置的函数
  5. //xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY); //同时分析CPU和Mem的开销
  6.     $xhprof_on = true;
  7.     /* -----------------xhprof------------------------ */
  8. }
复制代码

尾部:
  1. if (@$_GET[\'xhprof\'] == 1) {
  2.     /* -----------------xhprof------------------------ */
  3. // stop profiler
  4.     $xhprof_data = xhprof_disable();
  5. // display raw xhprof data for the profiler run
  6. //print_r($xhprof_data);
  7.     $XHPROF_ROOT = realpath(dirname(__FILE__) . \'/..\');
  8.     $XHPROF_ROOT = \'/data/apache/www/shao/xhprof\';
  9.     include_once $XHPROF_ROOT . \"/xhprof_lib/utils/xhprof_lib.php\";
  10.     include_once $XHPROF_ROOT . \"/xhprof_lib/utils/xhprof_runs.php\";
  11. // save raw data for this profiler run using default
  12. // implementation of iXHProfRuns.
  13.     $xhprof_runs = new XHProfRuns_Default();
  14. // save the run under a namespace \"xhprof_foo\"
  15.     $run_id = $xhprof_runs->save_run($xhprof_data, \"xhprof_foo\");
  16.     echo \"
  17. \" .
  18.      \"<a target=\"\" _blank=\"\" href=\"http://shaogx.com/\" http:=\"\"  domain=\"\" cc=\"\" xhprof=\"\" xhprof_html=\"\" index=\"\" php=\"\"  run=\"$run_id&source=xhprof_foo\">XHProf
  19. \" .
  20.     \"
  21. \";
  22.     /* -----------------xhprof------------------------ */
  23. }
复制代码

运行程序,底部出现统计字样,点过去就可以看到性能分析了。按运行时间排序,很容易找出化时间最长的函数。
  点[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/
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|创星网络 ( 苏ICP备11027519号|网站地图  

GMT+8, 2024-9-22 06:55 , Processed in 0.082005 second(s), 22 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表