51Testing软件测试论坛
标题:
如何监控Tomcat的性能
[打印本页]
作者:
feiyuw
时间:
2008-4-24 18:01
标题:
如何监控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截图
[attach]41490[/attach][attach]41491[/attach][attach]41492[/attach]
作者:
wxxhxy
时间:
2008-5-14 10:15
感谢楼主。
收藏。。
作者:
tiangou99073
时间:
2008-5-23 14:32
不错
作者:
ljonathan
时间:
2009-8-28 11:29
很强大
作者:
yetties2005
时间:
2009-8-28 12:10
加油加油!!!
作者:
huiguiziran111
时间:
2010-3-8 11:36
感谢楼主啊!!!
作者:
pangda
时间:
2010-3-10 11:23
好像很强大啊。回头我得试试
作者:
zhangshoujing
时间:
2010-3-19 14:39
很好,受益匪浅,谢谢楼主了
作者:
yuxuan0668
时间:
2010-4-1 15:51
谢
作者:
fpplzw
时间:
2010-6-29 15:47
帖子火了很多年
作者:
放任无奈
时间:
2010-6-30 15:52
哪位高人 能用JAVA改写这个程序
作者:
sjl
时间:
2011-7-28 10:49
作者:
xiaoxing01234
时间:
2012-2-6 13:21
今天没事来逛逛
作者:
mvvztt
时间:
2012-2-13 15:53
看了好像还是不会用~~~
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/)
Powered by Discuz! X3.2