j2ee项目性能分析工具jwebap使用简介

今天突发奇想的对j2ee项目的性能监控来了兴趣,索性找找资料研究一下!

可能很多朋友在性能优化方面都有很多困扰:程序在哪方面存在缺陷?sql哪里有瓶颈?哪个请求最频繁?哪个请求耗时最长……从某种意义上说,解决问题并不是特别困难的事儿,但如何发现问题的问题会经常困扰着我。

今天给大家介绍一款j2ee项目性能监测工具:jwebap!通过简单的研究,发现这东西还是挺好用的,哈哈!

一、jwebap简介  参见 leadyu的博客
Jwebap是一个用于J2EE工程(EJB以及WebModule系统)进行性能监控的组件,它有几个特点:
  1)基于ASM实现类的静态增强,可以无缝的部署于J2EE系统,对系统的开销几乎可以忽略
  2) 部署和使用非常的简单,整个Jwebap的部署只需要部署jwebap_core_**.jar以及需要使用的各种plugin_**.jar,然后配置 jwebap.xml和web.xml就可以完成所有的部署,比起绝大多数的profiling容易的多。 同时Jwebap提供Web Console进行整个Jwebap的管理和数据展现。在API层提供一套默认的视图框架供plugin开发者使用,可以只用Jar包就开发出相当漂亮的 Web界面。
  3)Jwebap的开发分为两个部分Jwebap-core部分,Jwebap-plugin部分。core部分基于jdk14 提供了类静态增强,轨迹生命管理,Plugin管理,视图框架等等,在这个基础上开发plugin。我觉得,好的profiling应该能够根据不同的人 群按需使用,同时在功能不断复杂和强大的过程中仍然能够保证较轻的架子。
    理论部分原作者已经介绍的很详细了,这里就不再赘述。直接上干货!
二、jwebap的部署

1、将jwebap_0.6.1.jar、tracer_0.6.1.jar以及依赖包commons-beanutils.jar、commons-betwixt-0.8.jar、commons-digester-1.7.jar、commons-logging.jar、commontemplate-0.8.1.jar、jxl.jar导入到项目的classpath下。(一般webapp项目放在web-inf下的lib中即可)。
2、将jwebap.xml导入到classpath下。
jwebap.xml
<?xml version="1.0" encoding="UTF-8"?>
<jwebap>
	<!--plugins-->
	<plugin name="Tracer" ref="${ABSOLUTE_PATH}/tracer_0.6.1.jar"/>
	 
	<!--dispatcher-->
	<dispatcher name="RedirectDispatcher" mapping="" type="org.jwebap.ui.controler.DefaultRedirectDispatcher"  />
	<dispatcher name="ActionDispatcher" mapping="/console/*" type="org.jwebap.ui.controler.ActionDispatcher"  />
	<dispatcher name="ResourceDispatcher" mapping="/resources/*" type="org.jwebap.ui.controler.ResourceDispatcher"  />
	
	<!--action-mapping-->
	<action-mapping>
		<action path="/" type="org.jwebap.ui.action.EmptyAction" template="resources/view/index.ctl" />
		<action path="/deploy/plugins" type="org.jwebap.ui.action.EmptyAction" template="resources/view/plugin_deploy.ctl" />
		<action path="/deploy/plugins/list" type="org.jwebap.ui.action.PluginListAction" />
		<action path="/deploy/plugins/remove" type="org.jwebap.ui.action.PluginRemoveAction" />
		<action path="/deploy/plugins/new" type="org.jwebap.ui.action.PluginFormAction"  template="resources/view/plugin_deploy_new.ctl" />
		<action path="/deploy/plugins/add" type="org.jwebap.ui.action.PluginAddAction"  />
		
		<action path="/deploy/plugins/detail" type="org.jwebap.ui.action.EmptyAction"  template="resources/view/plugin_detail.ctl" />
		<action path="/deploy/plugins/components/list" type="org.jwebap.ui.action.ComponentListAction" />
		<action path="/deploy/plugins/components/detail" type="org.jwebap.ui.action.ComponentFormAction"  template="resources/view/component_detail.ctl" />
		<action path="/deploy/plugins/components/save" type="org.jwebap.ui.action.ComponentSaveAction"  />
	</action-mapping>
</jwebap>

	

3、修改项目下的web.xml,添加jwebap相关的配置
	<context-param>
		<param-name>jwebap-config</param-name>
		<param-value>/WEB-INF/jwebap.xml</param-value>
	</context-param>
	<listener>	
		<listener-class>org.jwebap.startup.JwebapListener</listener-class>	
	</listener>
	<filter>
		<filter-name>PageDetectFilter</filter-name>
		<filter-class>org.jwebap.plugin.tracer.http.DetectFilter</filter-class>
		<init-param>
			<param-name>excludeUrls</param-name>
			<param-value>/detect;/detect/*;*.js;*.jpg;*.htm;*.html;*.gif;*.png;*.css;*.swf</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>PageDetectFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<servlet>
		<servlet-name>detect</servlet-name>
		<servlet-class>org.jwebap.ui.controler.JwebapServlet</servlet-class>	
	</servlet>

	<servlet-mapping>
		<servlet-name>detect</servlet-name>
		<url-pattern>/detect/*</url-pattern>
	</servlet-mapping>
	


4、修改tracer_0.6.1.jar中的plugin.xml文件(在jar包的META-INF文件夹下)
	……
	<component name="MethodComponent" type="org.jwebap.plugin.tracer.method.MethodComponent">
		<component-param>
			<name>trace-filter-active-time</name>
			<value>-1</value>
			<description>(ms) timings filter's over time</description>
		</component-param>
		<component-param>
			<name>trace-max-size</name>
			<value>1000</value>
			<description>max over-time trace size</description>
		</component-param>
		<component-param  style="longtext">
			<name>detect-clazzs</name>
			<value>
			com.gamecenter.controller.report.*;<!-- 改为需要监控的包或者类 -->
			</value>
			<description>
			package name and class name that monitored by MethodComponent,
			e.g.: 'test.*;test.Test' , divided by ';'
			</description>
		</component-param>
	</component>
	<component name="JdbcComponent" type="org.jwebap.plugin.tracer.jdbc.JdbcComponent">
		<component-param>
			<name>trace-filter-active-time</name>
			<value>-1</value>
			<description>(ms) timings filter's over time</description>
		</component-param>
		<component-param>
			<name>trace-max-size</name>
			<value>1000</value>
			<description>max over-time trace size</description>
		</component-param>
		<component-param  style="longtext">
			<name>connection-listener</name>
			<value>
			org.jwebap.plugin.tracer.http.ServletOpenedConnectionListener;org.jwebap.plugin.tracer.method.MethodOpenedConnectionListener
			</value>
			<description>Connection Listener</description>
		</component-param>
		<component-param  style="longtext">
			<name>driver-clazzs</name>
			<value>com.jolbox.bonecp.BoneCPDataSource</value><!-- 配置数据库连接池驱动,我这里用的是bonecp连接池 -->
			<description>
			1)Local datasource: set your ConnectionManagerClass,or the connection pool 's datasource. If you have more than one class ,divided by ';'.
												c3p0:com.mchange.v2.c3p0.ComboPooledDataSource; 
												dbcp: org.apache.commons.dbcp.BasicDataSource
												Also,other class. Jwebap will inject driver-clazzs,and detect any connection and datasource object it's method renturn.
												Note:  'driver-clazzs =jdbc driver' is deprecated. Beacause of connection pool, set 'driver-clazzs =jdbc driver', jwebap will find out all connection is leaked.
			2)JNDI datasource: If your application uses jndi datasource, you can set the class which manages connections in your application as driver, e.g.: 'com.china.telecom.ConnectionManager'. 
			Else if you use spring to get jndi datasource ,
			you also can set driver-clazzs=org.springframework.jndi.JndiObjectFactoryBean.JdbcComponent 
			will inject this class to proxy all connection the class's method return.
            org.springframework.orm.ibatis.SqlMapClientFactoryBean
			</description>
		</component-param>
	</component>
	
	


至此,jwebap的配置就完成了!让我们赶快启动应用来测试一下吧!

应用正常启动后,在浏览器中输入 http://ip:port/servername/detect/console/ 你会看到jwebap的性能检测界面,大功告成!




三、遇到的问题:
1、JdbcComponent 中driver-clazzs的设置。这里直接设置成数据库连接池就好了。但是设置完成以后可能会出现以下错误:
org.jwebap.toolkit.bytecode.InjectException: com.jolbox.bonecp.BoneCPConfig注入失败.
……………………
Caused by: org.jwebap.toolkit.bytecode.asm.DefineBytecodeException: com/jolbox/bonecp/BoneCPConfig注入错误:loader (instance of  org/apache/catalina/loader/WebappClassLoader): attempted  duplicate class definition for name: "com/jolbox/bonecp/BoneCPConfig"

这个问题之前我也尝试了很多方法,最后才发现这个重复加载是由于spring和jwebap都加载了boncp类造成的,而我这里采取的解决办法就是将
	    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
	


放在

	   <listener>
        <listener-class>
            org.jwebap.startup.JwebapListener
        </listener-class>
    </listener>
	

的后面即可。至于为什么,我现在还不是很清楚,欢迎哪位大师指点一二!
2、如果JdbcComponent 中driver-clazzs的设置没有报错,但是控制台下的jdbc Trances就是没有监测到数据,那么你的driver-clazzs可能没有和你项目中所使用的数据库连接池(或驱动)匹配上,再换个数据源试试吧!

猜你喜欢

转载自cpjsjxy.iteye.com/blog/1612317