Thursday, January 30, 2014

Aspect oriented programming with Java

I started working with AspectJ and Perf4J recently. I did not have any prior AOP experience. I was looking for an example which did not use Spring AOP but I did not find a good sample which I could use to get my work done. Therefore, once I completed my project, thought of writing this sample up. There are a lot of tutorials out there which describes what AOP is. Therefore, I’m not going to introduce it here. You can do a Google search and find it out.

What I wanted was to measure the method runtimes without doing any modifications to the existing source. AspectJ helped me to achieve that.

Following are my sample source code. Following is my test class which I'm using to measure method runtimes.
import org.apache.log4j.Logger;

public class TestTarget {

    static Logger logger = Logger.getLogger(TestTarget.class);

    public static void main(String[] args) throws Exception {
        // Test case 01

        // Test case 02


    public static void testCountSlow(int value) {

    public static void testCountFast(int value) {

    private static void count(int value, int delay) {
        for (int i=0;i<value;i++) {
            try {
            } catch (Exception e) {
                logger.error("Error occurred while sleeping", e);

Following is my Aspect class which uses AspectJ and Perf4J.
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.perf4j.StopWatch;
import org.perf4j.log4j.Log4JStopWatch;

public class Perf4JAspect {
    static Logger logger = Logger.getLogger(Perf4JAspect.class);

    @Around("execution (**(..))")
    public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
                + " MainAspect.measureExecutionTime() called on " + joinPoint.getSignature());

        long startNanoTime = System.nanoTime();
        StopWatch stopWatch = new Log4JStopWatch();
        Object proceed = joinPoint.proceed();

        long endNanoTime = System.nanoTime();
        long executionNanoTime = endNanoTime - startNanoTime; + " "
                + joinPoint.getSignature()+" took "+ executionNanoTime/1000000.0 +"ms");
        return proceed;
My aop.xml configuration is as follows.
        <aspect name=""/>
        <aspect name=""/>

    <weaver options="-verbose">
        <include within="com.aspectj.*"/>
My log4j.xml looks as follows.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration debug="false" xmlns:log4j="">

    <appender name="aspectjFileAppender" class="org.apache.log4j.FileAppender">
        <param name="File" value="aspectj_stats.log"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%m%n"/>
    <appender name="CoalescingStatistics"
        <param name="TimeSlice" value="10000"/>
        <appender-ref ref="fileAppender"/>

    <appender name="fileAppender" class="org.apache.log4j.FileAppender">
        <param name="File" value="perf_stats.log"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%m%n"/>

     <logger name="org.perf4j.TimingLogger" additivity="false">
        <level value="INFO"/>
        <appender-ref ref="CoalescingStatistics"/>

        <level value="INFO"/>
        <appender-ref ref="aspectjFileAppender"/>
Once the sample completes running, I get the following logs at my log files.
2014-01-30 13:19:25 MainAspect.measureExecutionTime() called on void
2014-01-30 13:19:25 void took 0.904ms
2014-01-30 13:19:25 MainAspect.measureExecutionTime() called on void
2014-01-30 13:19:31 void took 5608.996ms
Performance Statistics   2014-01-30 13:19:20 - 2014-01-30 13:19:30
Tag                                                                        Avg(ms)         Min         Max     Std Dev       Count
public static void     0.0           0           0         0.0           1
public static void  5608.0        5608        5608         0.0           1
As you can see, we can use AspectJ and Perf4J to generate some nice statistics.

No comments: