July 20, 2009

Performance Java - Perf4J

« AspectJ in Maven | Main | Servlet Filter »

Perf4J represents an API for performance analysis in Java. It uses standard logging APIs how Log4J or the java.util.logging interface for grabbing performance statistics. Perf4J uses the System.currentTimeMillis() method to memorize time states. By the help of Spring AOP or AspectJ you can weave automatically your business logic with Perf4J time hooks.

Simple example:

 StopWatch stopWatch = new LoggingStopWatch("block1");
 //business logic
 stopWatch.stop();
By the power of AOP you can automate the setting of LogginStopWatch segments, which have to be profiled. Spring offers two ways. Either you use Spring AOP proxy interceptor or the AspectJ functionality. The following DataService class will be profiled because of the setting of the "@Profiled" annotation above getCustomers method.
public class DataService implements IDataService{
  @Profiled
  public List getCustomers() throws Exception {
     //business and data acces logic
  }
}
 <aop:aspectj-autoproxy/>
 <bean id="timingAspect" class="org.perf4j.log4j.aop.TimingAspect"/>
 <bean id="dataService" class="org.developers.blog.DataService"<
Or you are using the AspectJ integration in Spring for profiling methods. If you want to profile class as well as Spring created proxies you have to set the proxy-target-class attribute to true.
<aop:config proxy-target-class="true">
        <aop:aspect id="performanceLoggerAspect" ref="performanceLogger">
     <aop:around pointcut="execution(* org.developers..*.*(..))" method="log"/>
  </aop:aspect>
 </aop:config>
Regards
Rafael

Technorati Tags:

Posted by rafael.sobek at 11:20 PM in Uncategorized

 

[Trackback URL for this entry]

Your comment:

(not displayed)
 
 
 

Live Comment Preview:

 
 
test