51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 22521|回复: 13

如何监控Tomcat的性能

[复制链接]

该用户从未签到

发表于 2008-4-24 18:01:53 | 显示全部楼层 |阅读模式
这里只给出基本思路和实现,在Tomcat6和Linux下检验通过。
基本思路:利用Tomcat自带的Status页面
基本实现步骤:
  • 打开Tomcat的status页面,方法为编辑Tomcat的conf目录下的tomcat-users.xml文件,在文件中添加
    <tomcat-users>
      <role rolename="manager"/>
      <user username="admin" password="pass" roles="manager"/>
    </tomcat-users>
    这里的password和username请自行修改,保存之后重启tomcat,打开http://localhost:8080/manager/status就可以看到status页面了
  • 通常我们需要读取的是另一个页面,即http://localhost:8080/manager/status?XML=true,这样整个服务器的性能数据就以一个单行的xml文件形式表示了。
  • 接下来我们需要读取这个页面的信息,然后对其进行xml或者字符串分析和处理,以取得我们需要的性能数据
  • 上面得到的只是当前情况下的性能数据,要获得一个阶段的性能数据,必须设定采样频率,定时读取,将数据汇总并分析
  • 接下来我们还可以对得到数据进行生成图表等操作

上面是一个粗略的实现步骤,下面给出一个简单的例子来说明,为了简单起见,没有采用Java等重量级语言,而是通过Linux自带的Bash来实现,绘制图表采用的是Gnuplot。以下为具体实现:
  1. #========================================================================
  2. # Author: Charlse.Zhang
  3. # Email: feiyuw@gmail.com
  4. # File Name: /home/zhang/Develop/BashWork/perftools/tomcatPerfMonitor.sh
  5. # Description:
  6. #   Monitor the performance of tomcat server   
  7. #   Method: Get the content of status page, analyze and collect the
  8. #   result, then give the report
  9. # Edit History:
  10. #   2008-04-24    File created.
  11. #========================================================================
  12. #!/bin/bash
  13. # ========================Default Settings===============================
  14. # address of status page
  15. STATUS_ADDR="http://localhost:8080/manager/status?XML=true"
  16. USER="admin"
  17. PASS="pass"
  18. # sample rate, default: 5seconds
  19. SAMPLE_RATE=5
  20. # if press "Ctrl+c", stop monitor
  21. EXIT_SIGNAL=2
  22. # connector to monitor
  23. CONNECTOR="http-8080"
  24. # result directory to store data
  25. RESULT_DIR="/tmp"
  26. # perf data file
  27. PERF_DATA="perf_data"
  28. # jvm data file
  29. JVM_DATA="jvm_data"
  30. # connector data file
  31. CONNECTOR_DATA="connector_data"
  32. # thread data file
  33. THREAD_DATA="thread_data"
  34. # request data file
  35. REQUEST_DATA="request_data"

  36. # ===========================Output Error Message========================
  37. # Show Error Message and exit, get one parameter
  38. errorMsg()
  39. {
  40.         if [[ $# -eq 1 ]]; then
  41.                 echo "Runtime Error: $1"
  42.                 exit 1
  43.         else
  44.                 echo "Function Error: errorMsg"
  45.                 exit 127
  46.         fi
  47. }

  48. # =========================Get Data Function=============================
  49. # Get performance data, no parameter wanted
  50. getPerfData()
  51. {
  52.         cd $RESULT_DIR
  53.         wget --http-user="$USER" --http-password="$PASS" "$STATUS_ADDR" -O "$PERF_DATA" || errorMsg "Failed to get data, please check the connection"
  54.         # JVM data
  55.         sed 's/.*<jvm>//g;s/<\/jvm>.*//g' $PERF_DATA | awk -F \' '{ print $2, $4, $6 }' >> $JVM_DATA
  56.         # 'Connector data
  57.         sed 's/.*'$CONNECTOR'.>//g;s/<\/connector>.*//g' $PERF_DATA >> $CONNECTOR_DATA
  58.         # Thread data
  59.         sed 's/.*<threadInfo//g;s/\/>.*//g' $CONNECTOR_DATA | awk -F \" '{ print $2, $4, $6 }' >> $THREAD_DATA
  60.         # " Request data
  61.         sed 's/.*<requestInfo//g;s/\/>.*//g' $CONNECTOR_DATA | awk -F \" '{ print $2, $4, $6, $8, $10, $12 }' >> $REQUEST_DATA

  62. }

  63. # ========================Build Chart Function==========================
  64. # "according the data, build the chart (use gnuplot)
  65. buildChart()
  66. {   
  67.         TITLE=""
  68.         OUTPUT=""
  69.         PLOT=""
  70.         YRANGE="[0:]"
  71.         case "$1" in
  72.                 "jvm" )
  73.                 TITLE="JVM"
  74.                 OUTPUT="jvm_graph.png"
  75.                 PLOT="plot 'jvm_data' using 1 title 'free' w linespoints, \
  76.                 'jvm_data' using 2 title 'total' w linespoints,\
  77.                 'jvm_data' using 3 title 'max' w linespoints"
  78.                 ;;

  79.                 "thread" )
  80.                 TITLE="Thread"
  81.                 OUTPUT="thread_graph.png"
  82.                 PLOT="plot 'thread_data' using 1 title 'max threads' w linespoints,\
  83.                 'thread_data' using 2 title 'current thread count' w linespoints,\
  84.                 'thread_data' using 3 title 'current thread busy' w linespoints"
  85.                 ;;

  86.                 "request" )
  87.                 TITLE="Request"
  88.                 YRANGE="[-1:]"
  89.                 OUTPUT="request_graph.png"
  90.                 PLOT="plot 'request_data' using 1 title 'max time' w linespoints,\
  91.                 'request_data' using 2 title 'processing time' w linespoints,\
  92.                 'request_data' using 3 title 'request count' w linespoints,\
  93.                 'request_data' using 4 title 'error count' w linespoints,\
  94.                 'request_data' using 5 title 'bytes received' w linespoints,\
  95.                 'request_data' using 6 title 'bytes sent' w linespoints"
  96.                 ;;
  97.         esac

  98.         # build graph
  99.         gnuplot <<EOF
  100.         set terminal png small size 480,360
  101.         set title "$TITLE"
  102.         set yrange $YRANGE
  103.         set grid
  104.         set xlabel "timeline (s)"
  105.         set output "$OUTPUT"
  106.         $PLOT
  107. EOF
  108. }

  109. # ========================Build Report Function=========================
  110. # include data and chart, give a readable html report
  111. buildReport()
  112. {
  113.         # build graph jvm, request,thread
  114.         buildChart "jvm" || errorMsg "Function Error: build jvm graph"
  115.         buildChart "thread" || errorMsg "Function Error: build thread graph"
  116.         buildChart "request" || errorMsg "Function Error: build request graph"
  117.         # build html report
  118. }

  119. # ========================Stop Monitor Function
  120. # call buildReport function
  121. stopMonitor()
  122. {
  123.     echo "Monitor stopped, and we are building the report ..."
  124.     buildReport || errorMsg "Function Error: buildReport"
  125.     exit
  126. }

  127. # =============================Main Function=============================
  128. trap "stopMonitor" $EXIT_SIGNAL
  129. while :
  130. do
  131.         getPerfData || errorMsg "Failed to get performance data"
  132.         sleep $SAMPLE_RATE
  133. done
复制代码
上述代码中未实现生成html的报表和为每个测试建立单独目录的功能,有兴趣的可以自行修改。该例子在Bash3.2.25和gnuplot 4.2上调试通过。
    附上几张生成的png格式的chart截图

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
回复

使用道具 举报

该用户从未签到

发表于 2008-5-14 10:15:23 | 显示全部楼层
感谢楼主。

收藏。。
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2008-5-23 14:32:54 | 显示全部楼层
不错
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2009-8-28 11:29:36 | 显示全部楼层
很强大
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2009-8-28 12:10:45 | 显示全部楼层
加油加油!!!
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-3-8 11:36:54 | 显示全部楼层
感谢楼主啊!!!
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-3-10 11:23:00 | 显示全部楼层
好像很强大啊。回头我得试试
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-3-19 14:39:22 | 显示全部楼层
很好,受益匪浅,谢谢楼主了
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-4-1 15:51:07 | 显示全部楼层
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-6-29 15:47:47 | 显示全部楼层
帖子火了很多年
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-6-30 15:52:47 | 显示全部楼层
哪位高人 能用JAVA改写这个程序
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2011-7-28 10:49:48 | 显示全部楼层
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2012-2-6 13:21:44 | 显示全部楼层
今天没事来逛逛
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2012-2-13 15:53:36 | 显示全部楼层
看了好像还是不会用~~~
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-19 01:43 , Processed in 0.085499 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

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