Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-dev] Performance timer Aspect


Hello,
I am new to AspectJ development, and am implementing our requirements for method level performance measurement with an Aspect.
We have a Timer object that must be instantiated at the start of every method. When the method finishes, the timer is stopped. The timer also needs an argument of type ParamObj, which is passed around with every method. The resulting methods look something like this:

public void method(ParamObj o)
{
        Timer timer = null;

        try
        {
                timer = new Timer(o);
                timer.start();

                // method body
        }

        catch(Exception e)
        {
                timer.stop("failure");
        }
       
        finally
        {
                timer.stop("success");
        }        
}

My first attempt at writing an aspect for this looks something like this:

public aspect Timer perthis( timer(ParamObj) )
{
        Timer timer = null;
       
        pointcut timer(ParamObj param) :
                execution(* *(..)) && args(param, ..);
                       
        before(ParamObj param): timer(param)
        {
                timer.start(param);
        }
       
        after(ParamObj param): returning: timer(param)
        {
                timer.stop("success");
        }
       
        after(ParamObj param): throwing: timer(param)
        {
                timer.stop("failure");
        }
}

I am using the -preprocess option of Ajc to generate woven code, as we are not confident enough to let Ajc produce the final bytecode.

My question to the list: is this the recommended approach? I couldn't think of another way of having a reference to the same timer object in both the before and the after Advice. How would this normally be done? Should the timer object be inserted into the code, or should it stay in the Aspect? Removing the "perthis" directive causes the timer to be static, which leads to incorrect behaviour.

A further issue is the code the weaver is generating into my EJBs.

public void setSessionContext(javax.ejb.SessionContext ctx) {
    {
      if ((ctx instanceof ParamObj)) {
        de.seb.ipm.aspects.Timer.bind$ajc(this);
      }
    }
......... etc

ParamObj (extends Object, implements nothing) can clearly not be an instance of  javax.ejb.SessionContext.  Is there some way of helping the weaver to recognise this? instanceof is an expensive operation and I'd rather avoid having it in every method that starts with an interface argument. Also, why is it calling de.seb.ipm.aspects.Timer.bind$ajc(this); in *every* method call when my pointcut defines methods starting with ParamObj only? this seems very wasteful and performance hungry.

Any help would be appreciated,

Best regards / Med vänlig älsning,

Rany Keddo

Back to the top