1

I am trying to implement an aspect to time method execution for NIO.

Basically, I need to get the time when a method is initially called and then when a callback is triggered (the callback is always either a failed() or succeeded() call - so I need to associate the initial method call with the appropriate callback.

I have the following implementation but it will not work unless calls are made serially which of course will not be the case.

Any help would be great

public aspect TimerAJ {
    private long timer = 0;
    private String methodName;
    private static Logger logger = LoggerFactory.getLogger(TimerAJ.class);

    //the call to the call back method
    pointcut timerCall1() : target(io.vertx.core.AsyncResult) && (call( * io.vertx.core.AsyncResult.failed(..)) || call( * io.vertx.core.AsyncResult.succeeded(..)));

    //the call to the method itself
    pointcut timerCall2() : execution(@Timer * *(..));

    before() : timerCall2() {
        timer = System.currentTimeMillis();
        methodName = methodSignature.getName();
    }
    after() : timerCall1() {
        logger.info(methodName + " " + String.format("%s took %d ms", thisEnclosingJoinPointStaticPart.getSourceLocation().getWithinType().getName(), (System.currentTimeMillis() - timer)));
    }
}
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
zzztop
  • 23
  • 5

1 Answers1

1

You need to reconsider this approach, as it simply won't work with Vert.x.

What you try to do is start counting when your @Timer method is called, and stop counting when any AsyncResult is returned.

What you can do: use vertx.eventBus().publish() to send start/stop metrics. Use another Verticle to gather metrics with vertx.eventBus().consumer() and display them.

Also, please take a look at Vert.x Dropwizard Metrics. If all you want is actual statistic, they'll provide it for you, without much implementation from your side.

Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
  • Seems like that you may be right on that - what about using javassist to inject source code at the appropriate areas within each method - I wrote a quick demo which seems to work - but have not dug deep enough there – zzztop Aug 14 '16 at 12:03
  • If it's something you're doing for yourself, and only once, you can try that. But for production uses such modifications will create unpredictable results. – Alexey Soshin Aug 16 '16 at 16:41
  • seems like ASM may be the only solution due to the dynamic aspect of the lambda methods created – zzztop Aug 17 '16 at 06:14