/************************************************
 * Copyright (c) Shen Li. All rights reserved.  *
 * http://joyaop.sourceforge.net                *
 * -------------------------------------------- *
 * Distributable under LGPL license.            *
 * See terms of license at gnu.org.             *
 ************************************************/
package test.performance;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import net.sf.joyaop.framework.AspectRuntime;
import net.sf.joyaop.framework.InterceptorAspect;
import net.sf.joyaop.impl.AspectRuntimeImpl;
import net.sf.joyaop.impl.InterceptorAspectImpl;
import net.sf.joyaop.impl.Pointcuts;

/**
 * @author Shen Li
 */
public class PerformanceTest extends TestCase {
    private int numberOfInvocations = 1000000;

    public void  testNoInterceptor() {
        Foo foo = new FooImpl();
        long start = System.currentTimeMillis();
        for (int i = 0; i < numberOfInvocations; i++) {
            foo.foo();
        }
        long end = System.currentTimeMillis();
        System.out.println("No interceptor: " + (end - start) / (double) numberOfInvocations);
    }

    public void testJvmScopeInterceptor() {
        AspectRuntime aspectRuntime = new AspectRuntimeImpl();
        InterceptorAspect interceptor = new InterceptorAspectImpl();
        interceptor.setAspectClass(FooInterceptor.class);
        interceptor.setPointcut(Pointcuts.byRegexp(FooImpl.class.getName(), "foo"));
        aspectRuntime.addInterceptor(interceptor);
        Foo foo = (Foo) aspectRuntime.newInstance(FooImpl.class);
        long start = System.currentTimeMillis();
        for (int i = 0; i < numberOfInvocations; i++) {
            foo.foo();
        }
        long end = System.currentTimeMillis();
        System.out.println("Interceptor: " + (end - start) / (double) numberOfInvocations);
    }

    public void testJvmScopeDecorator() {
        AspectRuntime aspectRuntime = new AspectRuntimeImpl();
        InterceptorAspect decorator = new InterceptorAspectImpl();
        decorator.setAspectClass(FooDecorator.class);
        decorator.setPointcut(Pointcuts.byInterface(Foo.class));
        aspectRuntime.addInterceptor(decorator);
        Foo foo = (Foo) aspectRuntime.newInstance(FooImpl.class);
        long start = System.currentTimeMillis();
        for (int i = 0; i < numberOfInvocations; i++) {
            foo.foo();
        }
        long end = System.currentTimeMillis();
        System.out.println("Decorator: " + (end - start) / (double) numberOfInvocations);
    }

    public void testJvmScopeAbstractInterceptor() {
        AspectRuntime aspectRuntime = new AspectRuntimeImpl();
        InterceptorAspect abstractInterceptor = new InterceptorAspectImpl();
        abstractInterceptor.setAspectClass(AbstractFooInterceptor.class);
        abstractInterceptor.setPointcut(Pointcuts.byInterface(Foo.class));
        aspectRuntime.addInterceptor(abstractInterceptor);
        Foo foo = (Foo) aspectRuntime.newInstance(FooImpl.class);
        long start = System.currentTimeMillis();
        for (int i = 0; i < numberOfInvocations; i++) {
            foo.foo();
        }
        long end = System.currentTimeMillis();
        System.out.println("Abstract interceptor: " + (end - start) / (double) numberOfInvocations);
    }

    public static void main(String args[]) {
        junit.textui.TestRunner.run(suite());
    }

    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTestSuite(PerformanceTest.class);
        return suite;
    }
}
