1

I want to create an annotation for sending timer metrics for my methods. I thought about doing something like that:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Timer {

}

And then I want to use AspectJ for doing the metrics collection logic as follows:

@Aspect
public class TimerAspect {

  @Pointcut("@annotation(Timer)")
  public void timerPointcut() {}

  @Pointcut("execution(* com.mycompany.**.*(..))")
  public void methodPointcut() {}


  @Before("methodPointcut() && timerPointcut()")
  public void startTimer() throws Throwable {
    // start timer logic here
  }

  @After("methodPointcut() && timerPointcut()")
  public void sendTimer() throws Throwable {
    // stop timer and send metrics logic here
  }
}

What I would like to understand, and I'm not sure how to benchmark it, is whether I have a performance penalty here when using this annotation.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Avi
  • 21,182
  • 26
  • 82
  • 121
  • Almost same question has been answered here http://stackoverflow.com/questions/433475/performance-impact-of-using-aop/433585#433585 – Sakalya Aug 14 '16 at 11:11
  • @Sakalya - yep, thanks. I saw this answer but it really feels lacking. It's very generic to a specific decision made by a specific individual and the benchmark link is broken :/ I really hope to get a more descriptive answer. – Avi Aug 14 '16 at 11:21

1 Answers1

3

The Spring AOP performance penalty is considerable because dynamic proxies are being used through which each call will be routed.

AspectJ performance is much better, the penalty is really small because no dynamic proxies are needed. You can use native AspectJ from within Spring as described in the Spring manual, chapter "Using AspectJ with Spring applications".

As described in my comments here, you need to be careful what you measure. For instance, it makes no sense to measure the additional time it takes to do the actual timekeeping and logging compared to the original application without any timekeeping/logging because if you would add those manually via scattered timekeeping/logging statements, the penalty would also exist. I am also reasoning about profiling aspects here.

If you want to measure the pure aspect penalty, maybe you could do it similar to my sample code here.

Community
  • 1
  • 1
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • If you are unsure, I suggest you stop speculating and start measuring or at least reading my linked answers. What you said is nothing substantial, rather a truism: Relative overhead depends on method runtime. So far, so unsurprising. If you have lots of slow methods, overhead might not be worth mentioning, but if you measure and log lots of small, quick methods, you will notice the difference between Spring AOP and AspectJ. In any case AspectJ is quicker. BTW, you mean CGLIB, not CGI. But a proxy is a proxy, no matter whether it is created by the JDK or by CGLIB. – kriegaex Aug 14 '16 at 13:41
  • @knegaex - The comment above wasn't meant for me, right? Probably a deleted comment? – Avi Aug 16 '16 at 16:58
  • Exactly, the other comment was deleted. – kriegaex Aug 16 '16 at 19:11