Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Thursday, November 22, 2007

java code for Proxy Perf Test

/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/



///////////////////////////////////////////////////////////////////////////////////////
public interface ISimpleBean {

public void advised();
public void unadvised();

}


///////////////////////////////////////////////////////////////////////////////////////

public class SimpleBean implements ISimpleBean {

private long dummy = 0;

public void advised() {
dummy = System.currentTimeMillis();
}

public void unadvised() {
dummy = System.currentTimeMillis();
}
}

///////////////////////////////////////////////////////////////////////////////////////

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class NoOpBeforeAdvice implements MethodBeforeAdvice {

public void before(Method method, Object[] args, Object target) throws Throwable {
// no-op
}
}


///////////////////////////////////////////////////////////////////////////////////////

import java.lang.reflect.Method;

import org.springframework.aop.support.StaticMethodMatcherPointcut;

public class TestPointcut extends StaticMethodMatcherPointcut {

public boolean matches(Method method, Class cls) {
return ("advised".equals(method.getName()));
}

}


///////////////////////////////////////////////////////////////////////////////////////

import org.springframework.aop.Advisor;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;

public class ProxyPerfTest {

public static void main(String[] args) {
ISimpleBean target = new SimpleBean();

Advisor advisor = new DefaultPointcutAdvisor(new TestPointcut(),
new NoOpBeforeAdvice());

runCglibTests(advisor, target);
runCglibFrozenTests(advisor, target);
runJdkTests(advisor, target);
}

private static void runCglibTests(Advisor advisor, ISimpleBean target) {
ProxyFactory pf = new ProxyFactory();
pf.setTarget(target);
pf.addAdvisor(advisor);

ISimpleBean proxy = (ISimpleBean)pf.getProxy();
System.out.println("Running CGLIB (Standard) Tests");
test(proxy);
}

private static void runCglibFrozenTests(Advisor advisor, ISimpleBean target) {
ProxyFactory pf = new ProxyFactory();
pf.setTarget(target);
pf.addAdvisor(advisor);
pf.setFrozen(true);

ISimpleBean proxy = (ISimpleBean)pf.getProxy();
System.out.println("Running CGLIB (Frozen) Tests");
test(proxy);
}

private static void runJdkTests(Advisor advisor, ISimpleBean target) {
ProxyFactory pf = new ProxyFactory();
pf.setTarget(target);
pf.addAdvisor(advisor);
pf.setInterfaces(new Class[]{ISimpleBean.class});

ISimpleBean proxy = (ISimpleBean)pf.getProxy();
System.out.println("Running JDK Tests");
test(proxy);
}

private static void test(ISimpleBean bean) {
long before = 0;
long after = 0;

// test advised method
System.out.println("Testing Advised Method");
before = System.currentTimeMillis();
for(int x = 0; x < 500000; x++) {
bean.advised();
}
after = System.currentTimeMillis();;

System.out.println("Took " + (after - before) + " ms");

// testing unadvised method
System.out.println("Testing Unadvised Method");
before = System.currentTimeMillis();
for(int x = 0; x < 500000; x++) {
bean.unadvised();
}
after = System.currentTimeMillis();;

System.out.println("Took " + (after - before) + " ms");

// testing equals() method
System.out.println("Testing equals() Method");
before = System.currentTimeMillis();
for(int x = 0; x < 500000; x++) {
bean.equals(bean);
}
after = System.currentTimeMillis();;

System.out.println("Took " + (after - before) + " ms");

// testing hashCode() method
System.out.println("Testing hashCode() Method");
before = System.currentTimeMillis();
for(int x = 0; x < 500000; x++) {
bean.hashCode();
}
after = System.currentTimeMillis();;

System.out.println("Took " + (after - before) + " ms");

// testing method on Advised
Advised advised = (Advised)bean;

System.out.println("Testing Advised.getProxyTargetClass() Method");
before = System.currentTimeMillis();
for(int x = 0; x < 500000; x++) {
advised.getProxyTargetClass();
}
after = System.currentTimeMillis();;

System.out.println("Took " + (after - before) + " ms");

System.out.println(">>>\n");
}
}

No comments: