Perf4j This is a performance analysis and plugin test for the application. It can be integrated with spring using spring AOP. It creates a log file that gives the parser the analysis and obtaining the relevant information. By default, this can lead to an average, average std. For more general information, please check http://perf4j.codehaus.org/index.html
How to configure Perf4j. For normal setup, you just need to add the perf4j jar and create a StopWatch instance for each sniplet code that you want to control.
StopWatch stopWatch= new StopWatch("snipletTagName") β¦
This will create the perf4j monitor and you will get registration information on the console.
The main purpose of this documentation is to set the configuration by establishing an understanding of perf4j integrating with spring.
1. Add the entire Jar file.
1. perf4j-0.9.16-slf4jonly.jar 2.aspectjweaver-1.6.12.jar 3.aopalliance-1.0.jar 4.commons-logging-1.1.1.jar 5.logback-classic-1.0.7.jar 6.logback-core-1.0.7.jar 7.slf4j-api-1.7.1.jar 8.perf4j-0.9.16.jar 9.aspectjrt-1.6.1.jar 10.commons-jexl-1.1.jar 11.asm-1.5.3.jar 12.cglib-2.1_3.jar
Make sure you have all this jar in your class path along with the spring library. 2.Create your own logback.xml, which will be used by perf4j implicitly the contents of logback.xml will be
<configuration> <appender name="CoalescingStatistics" class="org.perf4j.logback.AsyncCoalescingStatisticsAppender"> <param name="TimeSlice" value="1" /> <appender-ref ref="perf4jFileAppender" /> </appender> <appender name="RootConsoleAppender" class="ch.qos.logback.core.ConsoleAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>debug</level> </filter> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%date %-5level [%thread] %logger{36} [%file:%line] %msg%n </pattern> </layout> </appender> <appender name="perf4jFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> <File>logs/perf4j.log</File> <encoder> <Pattern>%date %-5level [%thread] %logger{36} [%file:%line] %msg%n </Pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <FileNamePattern>logs/perf4j.%d{yyyy-MM-dd}.log</FileNamePattern> </rollingPolicy> </appender> <logger name="org.perf4j.TimingLogger" additivity="false"> <level value="DEBUG" /> <appender-ref ref="CoalescingStatistics" /> <appender-ref ref="perf4jFileAppender" /> <appender-ref ref="RootConsoleAppender" /> </logger> </configuration>
3. In your spring configuration file, you need to add the aspectj tag, which will allow @Profiled to annotate perf4j.
**> (Note: what is @Profiled annotation: you add this tag to all
in all classes called from a spring instance or using dependency injection. The object should basically be the spring context, and the method should be called by the object that is registered in the spring context. Once I spent money thinking about why my method was not registered, then I realized that the object I tested was not part of the spring context. **
OK the code that you need to add to spring configuration xml is. <beans> β¦. <aop:aspectj-autoproxy> <aop:include name="timingAspect" /> </aop:aspectj-autoproxy> <bean id="timingAspect" class="org.perf4j.slf4j.aop.TimingAspect" /> <bean class="com.perf4jexample.Test" /> β¦. </beans>
4. Create a Test class that will implement the @Profiled annotation.
public class Test { private String testVal; public Test() {
5.Ok now you configure everything that remains, remains a test class that will launch the spring context and load perf4j with it.
public class Test(){ public static void main(){ AbstractApplicationContext context = new ClassPathXmlApplicationContext( "spring-context.xml"); context.start(); Test bean = context.getBean(Test.class); bean.testing(); }
I hope, following these settings, you can transfer the console appender to display a single line on the console.
Perf4j Monitoring command in the log To generate performance statistics, log in your path java -jar perf4j-0.9.16.jar myLogger.log
To generate graphs java -jar perf4j-0.9.16.jar --graph perfGraphs.out myLogger.log
Hope this tutorial helps you integrate Spring, perf4j, logback with profiled annotation.