LoadRunner监控Tomcat的几种方法Word下载.docx
《LoadRunner监控Tomcat的几种方法Word下载.docx》由会员分享,可在线阅读,更多相关《LoadRunner监控Tomcat的几种方法Word下载.docx(16页珍藏版)》请在冰豆网上搜索。
5、如果已经配置好,则在命令行输入jconsole,打开jdk自带的JMX客户端,选择远程连接,录入tomcat所在机器的IP,端口例192.168.1.100:
8999,帐号、密码在jmxremote.password中,如帐号controlRole,密码R&
D(缺省monitorRole只能读,controlRole能读写,jmxremote.access中可配置)。
点“连接”。
编写JAVA程序收集Tomcat性能数据
importjava.lang.management.MemoryUsage;
importjava.text.SimpleDateFormat;
importjava.util.Date;
importjava.util.Formatter;
importjava.util.HashMap;
importjava.util.Iterator;
importjava.util.Map;
importjava.util.Set;
importjavax.management.MBeanAttributeInfo;
importjavax.management.MBeanInfo;
importjavax.management.MBeanServerConnection;
importjavax.management.ObjectInstance;
importjavax.management.ObjectName;
importjavax.management.openmbean.CompositeDataSupport;
importjavax.management.remote.JMXConnector;
importjavax.management.remote.JMXConnectorFactory;
importjavax.management.remote.JMXServiceURL;
publicclassMonitorTomcat{
publicstaticvoidmain(String[]args){
try{
StringjmxURL="
service:
jmx:
rmi:
///jndi/rmi:
//192.168.1.100:
8999/jmxrmi"
;
//tomcatjmxurl
JMXServiceURLserviceURL=newJMXServiceURL(jmxURL);
Mapmap=newHashMap();
String[]credentials=newString[]{"
monitorRole"
"
QED"
};
map.put("
jmx.remote.credentials"
credentials);
JMXConnectorconnector=JMXConnectorFactory.connect(serviceURL,map);
MBeanServerConnection
mbsc=connector.getMBeanServerConnection();
//端口最好是动态取得
ObjectNamethreadObjName=newObjectName("
Catalina:
type=ThreadPool,name=http-8080"
);
MBeanInfombInfo=mbsc.getMBeanInfo(threadObjName);
StringattrName="
currentThreadCount"
//tomcat的线程数对应的属性值
MBeanAttributeInfo[]mbAttributes=mbInfo.getAttributes();
System.out.println("
currentThreadCount:
"
+mbsc.getAttribute(threadObjName,attrName));
//heap
for(intj=0;
j
###########"
+mbsc.getDomains()[j]);
}
SetMBeanset=mbsc.queryMBeans(null,null);
MBeanset.size():
"
+MBeanset.size());
IteratorMBeansetIterator=MBeanset.iterator();
while(MBeansetIterator.hasNext()){
ObjectInstanceobjectInstance=(ObjectInstance)MBeansetIterator.next();
ObjectNameobjectName=objectInstance.getObjectName();
StringcanonicalName=objectName.getCanonicalName();
canonicalName:
+canonicalName);
if(canonicalName.equals("
host=localhost,type=Cluster"
))
{
//GetdetailsofclusterMBeans
ClusterMBeansDetails:
========================================="
//getMBeansDetails(canonicalName);
StringcanonicalKeyPropList=objectName.getCanonicalKeyPropertyListString();
//-------------------------system----------------------
ObjectNameruntimeObjName=newObjectName("
java.lang:
type=Runtime"
厂商:
+(String)mbsc.getAttribute(runtimeObjName,"
VmVendor"
));
程序:
VmName"
版本:
VmVersion"
Datestarttime=newDate((Long)mbsc.getAttribute(runtimeObjName,"
StartTime"
SimpleDateFormatdf=newSimpleDateFormat("
yyyy-MM-ddHH:
mm:
ss"
启动时间:
+df.format(starttime));
Longtimespan=(Long)mbsc.getAttribute(runtimeObjName,"
Uptime"
连续工作时间:
+MonitorTomcat.formatTimeSpan(timespan));
//------------------------JVM-------------------------
//堆使用率
ObjectNameheapObjName=newObjectName("
type=Memory"
MemoryUsageheapMemoryUsage=
MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(heapObjName,"
HeapMemoryUsage"
longmaxMemory=heapMemoryUsage.getMax();
//堆最大
longcommitMemory=heapMemoryUsage.getCommitted();
//堆当前分配
longusedMemory=heapMemoryUsage.getUsed();
heap:
+(double)usedMemory*100/commitMemory+"
%"
MemoryUsagenonheapMemoryUsage=
NonHeapMemoryUsage"
longnoncommitMemory=nonheapMemoryUsage.getCommitted();
longnonusedMemory=heapMemoryUsage.getUsed();
nonheap:
+(double)nonusedMemory*100/noncommitMemory+"
ObjectNamepermObjName=newObjectName("
type=MemoryPool,name=PermGen"
MemoryUsagepermGenUsage=
MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(permObjName,"
Usage"
longcommitted=permGenUsage.getCommitted();
//持久堆大小
longused=heapMemoryUsage.getUsed();
//
permgen:
+(double)used*100/committed+"
//持久堆使用率
//--------------------Session---------------
ObjectNamemanagerObjName=newObjectName("
type=Manager,*"
Sets=mbsc.queryNames(managerObjName,null);
for(ObjectNameobj:
s){
应用名:
+obj.getKeyProperty("
path"
ObjectNameobjname=newObjectName(obj.getCanonicalName());
最大会话数:
+mbsc.getAttribute(objname,"
maxActiveSessions"
会话数:
activeSessions"
活动会话数:
sessionCounter"
//-----------------ThreadPool----------------
ObjectNamethreadpoolObjName=newObjectName("
type=ThreadPool,*"
Sets2=mbsc.queryNames(threadpoolObjName,null);
s2){
端口名:
name"
最大线程数:
maxThreads"
当前线程数:
繁忙线程数:
currentThreadsBusy"
}catch(Exceptione){
e.printStackTrace();
publicstaticStringformatTimeSpan(longspan){
longminseconds=span%1000;
span=span/1000;
longseconds=span%60;
span=span/60;
longmins=span%60;
longhours=span%24;
span=span/24;
longdays=span;
return(newFormatter()).format("
%1$d天%2$02d:
%3$02d:
%4$02d.%5$03d"
days,hours,mins,seconds,minseconds).toString();
}
记录的数据:
currentThreadCount:
150
###########JMImplementation
###########Users
###########com.sun.management
###########Catalina
###########java.lang
###########java.util.logging
MBeanset.size():
383
canonicalName:
Catalina:
name=HttpRequest152,type=RequestProcessor,worker=http-8080
J2EEApplication=none,J2EEServer=none,WebModule=//localhost/jsp-examples,j2eeType=Servlet,name=org.apache.jsp.jsp2.el.basic_002dcomparisons_jsp
……
port=8009,type=Mapper
J2EEApplication=none,J2EEServer=none,WebModule=//localhost/jsp-examples,j2eeType=Servlet,name=org.apache.jsp.dates.date_jsp
厂商:
SunMicrosystemsInc.
程序:
JavaHotSpot(TM)ClientVM
版本:
11.3-b02
启动时间:
2010-02-0911:
36:
59
连续工作时间:
0天00:
13:
55.775
heap:
62.221410820735%
nonheap:
84.59782727899399%
permgen:
210.93058268229166%
应用名:
/jsp-examples
最大会话数:
-1
会话数:
活动会话数:
/
/host-manager
/tomcat-docs
/AltoroJ
301
/webdav
/servlets-examples
/manager
/balancer
端口名:
http-8080
最大线程数:
当前线程数:
繁忙线程数:
100
jk-8009
200
4
1
使用LR的lr_user_data_point函数
Ord=1"
LAST);
web_reg_save_param("
ServerJVMVersion"
LB="
Ord=2"
ServerJVMVendor"