google搜索 51Testing站内搜索                    软件测试门户 | 软件测试培 训 | 文章资料精选 | 软件测试论坛 | 软件测试博客 | 测试招聘求职 
打印

如何监控Tomcat的性能

本主题由 fishy 于 2008-4-25 16:00 提升

如何监控Tomcat的性能


这里只给出基本思路和实现,在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。以下为具体实现:
复制内容到剪贴板
代码:
#========================================================================
# Author: Charlse.Zhang
# Email: feiyuw@gmail.com
# File Name: /home/zhang/Develop/BashWork/perftools/tomcatPerfMonitor.sh
# Description:
#   Monitor the performance of tomcat server   
#   Method: Get the content of status page, analyze and collect the
#   result, then give the report
# Edit History:
#   2008-04-24    File created.
#========================================================================
#!/bin/bash
# ========================Default Settings===============================
# address of status page
STATUS_ADDR="http://localhost:8080/manager/status?XML=true"
USER="admin"
PASS="pass"
# sample rate, default: 5seconds
SAMPLE_RATE=5
# if press "Ctrl+c", stop monitor
EXIT_SIGNAL=2
# connector to monitor
CONNECTOR="http-8080"
# result directory to store data
RESULT_DIR="/tmp"
# perf data file
PERF_DATA="perf_data"
# jvm data file
JVM_DATA="jvm_data"
# connector data file
CONNECTOR_DATA="connector_data"
# thread data file
THREAD_DATA="thread_data"
# request data file
REQUEST_DATA="request_data"

# ===========================Output Error Message========================
# Show Error Message and exit, get one parameter
errorMsg()
{
        if [[ $# -eq 1 ]]; then
                echo "Runtime Error: $1"
                exit 1
        else
                echo "Function Error: errorMsg"
                exit 127
        fi
}

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

}

# ========================Build Chart Function==========================
# "according the data, build the chart (use gnuplot)
buildChart()
{   
        TITLE=""
        OUTPUT=""
        PLOT=""
        YRANGE="[0:]"
        case "$1" in
                "jvm" )
                TITLE="JVM"
                OUTPUT="jvm_graph.png"
                PLOT="plot 'jvm_data' using 1 title 'free' w linespoints, \
                'jvm_data' using 2 title 'total' w linespoints,\
                'jvm_data' using 3 title 'max' w linespoints"
                ;;

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

                "request" )
                TITLE="Request"
                YRANGE="[-1:]"
                OUTPUT="request_graph.png"
                PLOT="plot 'request_data' using 1 title 'max time' w linespoints,\
                'request_data' using 2 title 'processing time' w linespoints,\
                'request_data' using 3 title 'request count' w linespoints,\
                'request_data' using 4 title 'error count' w linespoints,\
                'request_data' using 5 title 'bytes received' w linespoints,\
                'request_data' using 6 title 'bytes sent' w linespoints"
                ;;
        esac

        # build graph
        gnuplot <<EOF
        set terminal png small size 480,360
        set title "$TITLE"
        set yrange $YRANGE
        set grid
        set xlabel "timeline (s)"
        set output "$OUTPUT"
        $PLOT
EOF
}

# ========================Build Report Function=========================
# include data and chart, give a readable html report
buildReport()
{
        # build graph jvm, request,thread
        buildChart "jvm" || errorMsg "Function Error: build jvm graph"
        buildChart "thread" || errorMsg "Function Error: build thread graph"
        buildChart "request" || errorMsg "Function Error: build request graph"
        # build html report
}

# ========================Stop Monitor Function
# call buildReport function
stopMonitor()
{
    echo "Monitor stopped, and we are building the report ..."
    buildReport || errorMsg "Function Error: buildReport"
    exit
}

# =============================Main Function=============================
trap "stopMonitor" $EXIT_SIGNAL
while :
do
        getPerfData || errorMsg "Failed to get performance data"
        sleep $SAMPLE_RATE
done
上述代码中未实现生成html的报表和为每个测试建立单独目录的功能,有兴趣的可以自行修改。该例子在Bash3.2.25和gnuplot 4.2上调试通过。
    附上几张生成的png格式的chart截图
附件: 您所在的用户组无法下载或查看附件

TOP

感谢楼主。

收藏。。

TOP

不错

TOP

 
当前时区 GMT+8, 现在时间是 2008-7-24 18:10Copyright(C)上海博为峰软件技术有限公司 2001-2007 电话:021-64471599-8017
当您在访问网站、论坛及博客过程中遇到问题时可发送email:webmaster@51testing.com或发送论坛短信至管理员风在吹