- 浏览: 3506756 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
wanglf1207:
EJB的确是个不错的产品,只是因为用起来有点门槛,招来太多人吐 ...
weblogic-ejb-jar.xml的元素解析 -
qwfys200:
总结的不错。
Spring Web Flow 2.0 入门 -
u011577913:
u011577913 写道也能给我发一份翻译文档? 邮件437 ...
Hazelcast 参考文档-4 -
u011577913:
也能给我发一份翻译文档?
Hazelcast 参考文档-4 -
songzj001:
DbUnit入门实战
perf4j是一款类似于log4j的性能检测工具.
它的基本操作就是封装我们常用的通过System.currentTimeMillis();来计算执行时间.如下:
- long start = System.currentTimeMillis();
- // execute the block of code to be timed
- System.out.println("ms for block n was: " + (System.currentTimeMillis() - start));
long start = System.currentTimeMillis(); // execute the block of code to be timed System.out.println("ms for block n was: " + (System.currentTimeMillis() - start));
它的最大优势是可以根据这些数据对要检测的代码块的执行进行数字以及图形化统计.
下面是通过AOP方式为jpetstore添加的perf4j步骤.
1.在pom文件中增加如下依赖
- < dependency >
- < groupId > org.perf4j </ groupId >
- < artifactId > perf4j </ artifactId >
- < version > 0.9.12-SNAPSHOT </ version >
- </ dependency >
- < dependency >
- < groupId > commons-jexl </ groupId >
- < artifactId > commons-jexl </ artifactId >
- < version > 1.1 </ version >
- < type > jar </ type >
- </ dependency >
- < dependency >
- < groupId > log4j </ groupId >
- < artifactId > log4j </ artifactId >
- < version > 1.2.14 </ version >
- < type > jar </ type >
- </ dependency >
<dependency> <groupId>org.perf4j</groupId> <artifactId>perf4j</artifactId> <version>0.9.12-SNAPSHOT</version> </dependency> <dependency> <groupId>commons-jexl</groupId> <artifactId>commons-jexl</artifactId> <version>1.1</version> <type>jar</type> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> <type>jar</type> </dependency>
2.增加编译插件如下:
- < build >
- < finalName > jpetstore </ finalName >
- < defaultGoal > package </ defaultGoal >
- < plugins >
- < plugin >
- < groupId > org.apache.maven.plugins </ groupId >
- < artifactId > maven-compiler-plugin </ artifactId >
- < version > 2.0.2 </ version >
- < configuration >
- < source > 1.5 </ source >
- < target > 1.5 </ target >
- < encoding > UTF-8 </ encoding >
- </ configuration >
- </ plugin >
- </ plugins >
- </ build >
<build> <finalName>jpetstore</finalName> <defaultGoal>package</defaultGoal> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build>
3.为需要进行检测的方法增加注解@Profiled
譬如PetStoreImpl类中
- @Profiled
- public Category getCategory(String categoryId) {
- return this .categoryDao.getCategory(categoryId);
- }
@Profiled public Category getCategory(String categoryId) { return this.categoryDao.getCategory(categoryId); }
4.在spring配置文件中增加Aspectj的支持以及perf4j的支持
- < aop:aspectj-autoproxy />
- < bean id = "timingAspect" class = "org.perf4j.log4j.aop.TimingAspect" />
<aop:aspectj-autoproxy/> <bean id="timingAspect" class="org.perf4j.log4j.aop.TimingAspect"/>
5.为了能够在页面上进行定制统计图表的实时查看,需要在web.xml中增加如下配置
- < servlet >
- < servlet-name > perf4j </ servlet-name >
- < servlet-class > org.perf4j.log4j.servlet.GraphingServlet </ servlet-class >
- < init-param >
- < param-name > graphNames </ param-name >
- < param-value > graphExecutionTimes,graphExecutionTPS </ param-value >
- </ init-param >
- </ servlet >
- < servlet-mapping >
- < servlet-name > perf4j </ servlet-name >
- < url-pattern > /perf4j </ url-pattern >
- </ servlet-mapping >
<servlet> <servlet-name>perf4j</servlet-name> <servlet-class>org.perf4j.log4j.servlet.GraphingServlet</servlet-class> <init-param> <param-name>graphNames</param-name> <param-value>graphExecutionTimes,graphExecutionTPS</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>perf4j</servlet-name> <url-pattern>/perf4j</url-pattern> </servlet-mapping>
此处定义的graphExecutionTimes,graphExecutionTPS为log4j配置文件中定义的appender,可增加可删除.
6.现在最重要的一步就是log4j中的设置了.
首先将jpetstore的log4j配置文件log4j.properties更换为log4j.xml。内容如下
- <? xml version = "1.0" encoding = "UTF-8" ?>
- <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
- < log4j:configuration debug = "false" xmlns:log4j = "http://jakarta.apache.org/log4j/" >
- <!--
- This default ConsoleAppender is used to log all NON perf4j messages
- to System.out
- -->
- < appender name = "console" class = "org.apache.log4j.ConsoleAppender" >
- < layout class = "org.apache.log4j.PatternLayout" >
- < param name = "ConversionPattern" value = "%-5p %c{1} - %m%n" />
- </ layout >
- </ appender >
- <!-- Perf4J appenders -->
- <!--
- This AsyncCoalescingStatisticsAppender groups StopWatch log messages
- into GroupedTimingStatistics messages which it sends on the
- file appender defined below
- -->
- < appender name = "CoalescingStatistics"
- class = "org.perf4j.log4j.AsyncCoalescingStatisticsAppender" >
- <!--
- The TimeSlice option is used to determine the time window for which
- all received StopWatch logs are aggregated to create a single
- GroupedTimingStatistics log. Here we set it to 10 seconds, overriding
- the default of 30000 ms
- -->
- < param name = "TimeSlice" value = "10000" />
- < appender-ref ref = "fileAppender" />
- <!--
- Note how the GraphingStatisticsAppenders have been attached to the
- CoalescingStatistics here.
- -->
- < appender-ref ref = "graphExecutionTimes" />
- < appender-ref ref = "graphExecutionTPS" />
- </ appender >
- <!-- This file appender is used to output aggregated performance statistics -->
- < appender name = "fileAppender" class = "org.apache.log4j.FileAppender" >
- < param name = "File" value = "${petstore.root}/WEB-INF/perfStats.log" />
- < layout class = "org.apache.log4j.PatternLayout" >
- < param name = "ConversionPattern" value = "%m%n" />
- </ layout >
- </ appender >
- <!--
- This first GraphingStatisticsAppender graphs Mean execution times for the
- firstBlock and secondBlock tags
- -->
- < appender name = "graphExecutionTimes"
- class = "org.perf4j.log4j.GraphingStatisticsAppender" >
- <!-- Possible GraphTypes are Mean, Min, Max, StdDev, Count and TPS -->
- < param name = "GraphType" value = "Mean" />
- <!-- The tags of the timed execution blocks to graph are specified here -->
- < param name = "TagNamesToGraph" value = "getCategory,getItem,getItemListByProduct,getProduct,getProductListByCategory,searchProductList" />
- < appender-ref ref = "graphsFileAppender" />
- </ appender >
- <!--
- This second GraphingStatisticsAppender graphs transactions per second
- for the firstBlock and secondBlock tags
- -->
- < appender name = "graphExecutionTPS"
- class = "org.perf4j.log4j.GraphingStatisticsAppender" >
- < param name = "GraphType" value = "TPS" />
- < param name = "TagNamesToGraph" value = "getCategory,getItem,getItemListByProduct,getProduct,getProductListByCategory,searchProductList" />
- < appender-ref ref = "graphsFileAppender" />
- </ appender >
- <!--
- This file appender is used to output the graph URLs generated
- by the GraphingStatisticsAppenders
- -->
- < appender name = "graphsFileAppender" class = "org.apache.log4j.FileAppender" >
- < param name = "File" value = "${petstore.root}/WEB-INF/perfGraphs.log" />
- < layout class = "org.apache.log4j.PatternLayout" >
- < param name = "ConversionPattern" value = "%m%n" />
- </ layout >
- </ appender >
- <!-- Loggers -->
- <!--
- The Perf4J logger. Note that org.perf4j.TimingLogger is the value of the
- org.perf4j.StopWatch.DEFAULT_LOGGER_NAME constant. Also, note that
- additivity is set to false, which is usually what is desired - this means
- that timing statements will only be sent to this logger and NOT to
- upstream loggers.
- -->
- < logger name = "org.perf4j.TimingLogger" additivity = "false" >
- < level value = "DEBUG" />
- < appender-ref ref = "CoalescingStatistics" />
- </ logger >
- <!--
- The root logger sends all log statements EXCEPT those sent to the perf4j
- logger to System.out.
- -->
- < root >
- < level value = "INFO" />
- < appender-ref ref = "console" />
- </ root >
- </ log4j:configuration >
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration debug="false" xmlns:log4j="http://jakarta.apache.org/log4j/"> <!-- This default ConsoleAppender is used to log all NON perf4j messages to System.out --> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/> </layout> </appender> <!-- Perf4J appenders --> <!-- This AsyncCoalescingStatisticsAppender groups StopWatch log messages into GroupedTimingStatistics messages which it sends on the file appender defined below --> <appender name="CoalescingStatistics" class="org.perf4j.log4j.AsyncCoalescingStatisticsAppender"> <!-- The TimeSlice option is used to determine the time window for which all received StopWatch logs are aggregated to create a single GroupedTimingStatistics log. Here we set it to 10 seconds, overriding the default of 30000 ms --> <param name="TimeSlice" value="10000"/> <appender-ref ref="fileAppender"/> <!-- Note how the GraphingStatisticsAppenders have been attached to the CoalescingStatistics here. --> <appender-ref ref="graphExecutionTimes"/> <appender-ref ref="graphExecutionTPS"/> </appender> <!-- This file appender is used to output aggregated performance statistics --> <appender name="fileAppender" class="org.apache.log4j.FileAppender"> <param name="File" value="${petstore.root}/WEB-INF/perfStats.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%m%n"/> </layout> </appender> <!-- This first GraphingStatisticsAppender graphs Mean execution times for the firstBlock and secondBlock tags --> <appender name="graphExecutionTimes" class="org.perf4j.log4j.GraphingStatisticsAppender"> <!-- Possible GraphTypes are Mean, Min, Max, StdDev, Count and TPS --> <param name="GraphType" value="Mean"/> <!-- The tags of the timed execution blocks to graph are specified here --> <param name="TagNamesToGraph" value="getCategory,getItem,getItemListByProduct,getProduct,getProductListByCategory,searchProductList"/> <appender-ref ref="graphsFileAppender"/> </appender> <!-- This second GraphingStatisticsAppender graphs transactions per second for the firstBlock and secondBlock tags --> <appender name="graphExecutionTPS" class="org.perf4j.log4j.GraphingStatisticsAppender"> <param name="GraphType" value="TPS"/> <param name="TagNamesToGraph" value="getCategory,getItem,getItemListByProduct,getProduct,getProductListByCategory,searchProductList"/> <appender-ref ref="graphsFileAppender"/> </appender> <!-- This file appender is used to output the graph URLs generated by the GraphingStatisticsAppenders --> <appender name="graphsFileAppender" class="org.apache.log4j.FileAppender"> <param name="File" value="${petstore.root}/WEB-INF/perfGraphs.log"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%m%n"/> </layout> </appender> <!-- Loggers --> <!-- The Perf4J logger. Note that org.perf4j.TimingLogger is the value of the org.perf4j.StopWatch.DEFAULT_LOGGER_NAME constant. Also, note that additivity is set to false, which is usually what is desired - this means that timing statements will only be sent to this logger and NOT to upstream loggers. --> <logger name="org.perf4j.TimingLogger" additivity="false"> <level value="DEBUG"/> <appender-ref ref="CoalescingStatistics"/> </logger> <!-- The root logger sends all log statements EXCEPT those sent to the perf4j logger to System.out. --> <root> <level value="INFO"/> <appender-ref ref="console"/> </root> </log4j:configuration>
最后启动服务器,点击petstore上面的链接,就可以看到项目WEB-INF/下面的perfStats.log,perfGraphs.log已经有数据了。
现在查看http://localhost:8080/jpetstore/perf4j,就可以查看perf4j对各个检测方法的图形统计了,如下:
上面我们是采用的AOP方式,另外也可以采用更具侵入性的方法来实现,在SqlMapProductDao代码块的开始和结尾增加如下代码:
- public List searchProductList(String keywords) throws DataAccessException {
- StopWatch stopWatch = new LoggingStopWatch();
- Object parameterObject = new ProductSearch(keywords);
- List aaa = getSqlMapClientTemplate().queryForList("searchProductList" , parameterObject);
- stopWatch.stop("searchProductList" );
- return aaa;
- }
public List searchProductList(String keywords) throws DataAccessException { StopWatch stopWatch = new LoggingStopWatch(); Object parameterObject = new ProductSearch(keywords); List aaa = getSqlMapClientTemplate().queryForList("searchProductList", parameterObject); stopWatch.stop("searchProductList"); return aaa; }
最后,perf4j在检测方式上还提供其他一些特性,譬如可以自定义TAG。。。可以查阅其相关文档http://perf4j.codehaus.org/devguide.html 。
来自:http://eddysheng.iteye.com/blog/434913
发表评论
-
说明SOA监管(SOA Governance)实例(收录备查)
2012-12-19 11:35 1756SOA 已经不是单纯技术问 ... -
Injecting Spring Beans into Java Servlets
2012-11-01 10:21 1942If you are working in a Java ... -
用 HttpServletResponseWrapper 实现 Etag 过滤器
2012-07-09 16:58 3764原文出处:http://blog.chenlb.com/200 ... -
Eclipse Indigo - Cannot install Android ADT Plugin
2012-02-29 01:17 3888When I try to install the And ... -
Eclipse Indigo - Cannot install Android ADT Plugin
2012-02-29 01:13 1994When I try to install the And ... -
[转]mybatis下的分页,支持所有的数据库
2011-07-21 13:21 14844大 家都知道,mybatis的自带分页方法只是逻 ... -
Java framework for text- & console-based forms?
2011-07-21 01:06 1714charva jcurses JNA , ... -
JNA(Java Native Access)学习入门
2011-07-21 01:04 22656Java Native Access 项目 在 ... -
使用IntrospectorCleanupListener 解决quartz引起的内存泄漏
2011-04-20 11:59 13378"在服务器运行过程中,Spring不停的运行的计划任 ... -
DBCP代码研读以及就数据库连接失效的解决
2011-03-31 11:03 3772问题 网上很多评论说DBCP有很多BUG,但是都没有指明是什 ... -
ContextLoaderListener
2010-12-06 15:58 8473(1) org.springframework.web.c ... -
Servlet3.0新功能: 异步处理
2010-12-06 15:22 3188J2EE 6和Glassfish 3V正式发 ... -
Servlet3.0引入的新特性
2010-12-06 15:20 3063Servlet3.0规范的新特性主要是为了3个目的: ... -
100個節點上運行群集亞馬遜EC2上Hazelcast
2010-12-03 23:59 3322本文的目的,適是给妳湮示的細節集群的100個節點。此湮示記錄, ... -
Spring Properties Reloaded
2010-12-02 14:54 4378Spring Properties Reloaded Som ... -
语义网的学习资源大汇集(备忘)
2010-06-23 22:48 1761网上资源 http:/ ... -
使用 JOLAP 实现复杂分析查询
2010-06-06 13:42 1971Shashank Tiwari 在本文中对 ... -
HTML5 Canvas for Internet Explorer
2010-06-04 21:16 1859Canvascape http://www.benjoff ... -
大型网站架构演变和知识体系
2010-06-01 23:47 1982架构演变第一步:物 ... -
Ejb Con Spring
2010-05-20 17:46 2214Contenido [ocultar] 1 ...
相关推荐
在压缩包文件名称 "springapp-petclinic" 中,"petclinic"可能是JPetStore的一个变体或者相关项目,通常Spring PetClinic是一个与JPetStore类似的学习资源,用于教授Spring Boot和Spring Data JPA等现代Spring技术。...
《Spring框架学习:以JpetStore为例》 Spring框架是Java企业级应用开发中的核心框架,它为开发者提供了丰富的功能,简化了开发流程,提高了代码的可测试性和可维护性。JpetStore作为Spring的经典示例项目,是学习...
6. **配置Hibernate**:由于JPetStore使用了Hibernate作为ORM工具,需要配置Hibernate的SessionFactory,并在Spring中进行声明。 7. **运行与调试**:最后,配置Tomcat服务器,部署项目并启动,通过浏览器访问应用...
描述中的链接指向了iteye博客上的一篇文章,虽然描述本身为空,但可以推测这篇文章可能详细介绍了如何创建、配置和运行这个Spring 2.5 Demo。通过访问该链接,开发者可以获得更深入的步骤指导和上下文信息。 标签...
spring自带的JPetStore,我已经配置好(数据库也配置好,用的是hsqldb),可以直接导 入eclipse中运行。共3个压缩包
《MyEclipse中加载Spring的JPetStore详解》 在软件开发领域,Spring框架以其强大的功能和灵活性,已经成为Java企业级应用开发的事实标准。而MyEclipse作为一款强大的Java集成开发环境,为开发者提供了便捷的Spring...
《基于Spring、Struts和iBatis的jpetstore4.0详解》 jpetstore4.0是一款经典的电子商务示例应用,它采用Spring、Struts和iBatis这三个核心框架构建,展示了如何在Java环境下实现一个完整的MVC(Model-View-...
在jpetstore项目中,Spring作为整个应用的“胶水”,负责管理各个组件的生命周期,以及它们之间的依赖关系。 接着,iBatis作为一个优秀的持久层框架,它将SQL语句与Java代码分离,提高了代码的可维护性。在...
最新spring带的JPetStore的MyEclipse项目,包括了数据库,可用hsqldb直接运行,可以直接导入MyEclipse中并部署运行。 在Myeclipse里新建一个web项目,导入shopping项目即可,数据库在db文件夹里
4. 视图(View):在JPetStore中,视图通常由JSP页面或者Thymeleaf模板构成。视图解析器根据模型数据和逻辑决定渲染哪个视图,实现数据的展示。 5. 数据访问(DAO):Spring框架提供了对数据库操作的支持,包括使用...
本篇文章将深入探讨如何在MyEclipse环境中搭建和运行这个基于Spring的JPetStore工程,并介绍其中涉及的关键技术点。 首先,JPetStore是一个完整的电子商务应用,展示了Spring MVC的用法,包括模型(Model)、视图...
在JPetStore项目中,Spring通过配置文件管理对象的生命周期和相互依赖关系,使得代码更加模块化和易于维护。 2. **Spring MVC** JPetStore使用Spring MVC模式构建Web应用程序。Spring MVC提供了模型-视图-控制器的...
本文将深入探讨如何在Eclipse环境中使用Spring框架,以JPetStore项目为例,帮助开发者了解Spring的核心特性和实际应用。 一、Eclipse与Spring框架 Eclipse是一款开源的Java IDE,提供了丰富的插件支持,使得开发者...
Struts、Spring 和 Hibernate 是Java开发中的三大框架,它们各自在不同的层面上为应用程序提供支持。Struts 是一个 MVC(模型-视图-控制器)框架,Spring 是一个全面的后端应用框架,而 Hibernate 则是 ORM(对象...
spring自带的JPetStore,我已经配置好(数据库也配置好,用的是hsqldb),可以直接导入eclipse中运行。共3个压缩包
这个版本相较于之前的JPetStore5.0,进行了重要的技术升级,主要体现在将数据持久层的Ibatis替换为更强大的Hibernate,并引入了Spring框架来增强应用程序的模块化和管理。 **Struts框架:** Struts是Apache基金会的...
spring自带的JPetStore,我已经配置好(数据库也配置好,用的是hsqldb),可以直接导 入eclipse中运行。共3个压缩包
**Spring jpetstore** 是一个基于Spring框架的开源示例应用,它展示了如何使用Spring MVC、Spring JDBC以及Spring的其他核心特性来构建一个完整的Web应用。这个项目是Spring官方提供的,旨在帮助开发者学习和理解...
4. **源代码分析**:在提供的源代码中,你可以看到Spring的配置文件(如`applicationContext.xml`),其中定义了bean的实例化和依赖关系。同时,iBatis的配置文件(如`sqlMapConfig.xml`)描述了数据源、事务管理器...
该jpetstore经典案例为最新SPRING开发包里面的完全案例,并集成到eclipse里面了,在eclipse里面可以直接运行并调试,在工作目录里面直接建立jpetstore目录,自动导入该目录下文件,修改jdbc.properties配置文件,连接...