1 package testproxy; 2 3 import java.io.Serializable; 4 import java.lang.reflect.Method; 5 /** 6 import net.sf.cglib.proxy.CallbackFilter; 7 import net.sf.cglib.proxy.Enhancer; 8 import net.sf.cglib.proxy.InvocationHandler; 9 import net.sf.cglib.proxy.NoOp; 10 */ 11 import javassist.util.proxy.MethodFilter; 12 import javassist.util.proxy.MethodHandler; 13 import javassist.util.proxy.ProxyFactory; 14 15 import junit.framework.Test; 16 import junit.framework.TestCase; 17 import junit.framework.TestSuite; 18 import junit.textui.TestRunner; 19 20 @SuppressWarnings({"rawtypes","unchecked", "unused"}) 21 public class ProxyFactoryPerformanceTest extends TestCase { 22 23 public static final int COUNT = 100; 24 25 public static final int MAX_THREADS = 30; 26 27 static Throwable error = null; 28 ProxyFactoryPerformanceTest()29 public ProxyFactoryPerformanceTest() {} 30 ProxyFactoryPerformanceTest(String name)31 public ProxyFactoryPerformanceTest(String name) { 32 super(name); 33 } 34 testJavassist()35 public void testJavassist() throws Throwable { 36 callCreateClass("javassist", ProxyMaker.class); 37 } 38 39 /** 40 public void testCglib() throws Exception { 41 callCreateClass("cglib", EnhancerUser.class); 42 } 43 */ 44 callCreateClass(String translator, Class cl)45 public void callCreateClass(String translator, Class cl) throws Throwable { 46 error = null; 47 Thread[] threads = new Thread[MAX_THREADS]; 48 for (int i = 0; i < threads.length; ++i) { 49 threads[i] = (Thread)cl.getDeclaredConstructor().newInstance(); 50 } 51 long time = System.currentTimeMillis(); 52 for (int i = 0; i < threads.length; ++i) { 53 threads[i].start(); 54 } 55 for (int i = 0; i < threads.length; ++i) { 56 threads[i].join(); 57 } 58 time = System.currentTimeMillis() - time; 59 System.out.println("ProxyFactoryPerformanceTest: " + translator + " time: " + time); 60 if (error != null) 61 throw error; 62 } 63 suite()64 public static Test suite() { 65 return new TestSuite(ProxyFactoryPerformanceTest.class); 66 } 67 callOnce()68 public static void callOnce() { 69 try { 70 Thread t = new ProxyMaker(); 71 t.start(); 72 t.join(); 73 } 74 catch (InterruptedException e) { 75 e.printStackTrace(); 76 } 77 78 System.out.println("** Done"); 79 } 80 main(String[] args)81 public static void main(String[] args) { 82 // callOnce(); 83 ProxyFactory.useCache = args.length == 0; 84 TestRunner.run(suite()); 85 } 86 } 87 88 @SuppressWarnings({"rawtypes","unused"}) 89 class ProxyMaker extends Thread implements MethodHandler { 90 private static final MethodFilter FINALIZE_FILTER = new MethodFilter() { 91 public boolean isHandled(Method m) { 92 // skip finalize methods 93 return !( m.getParameterTypes().length == 0 && m.getName().equals( "finalize" ) ); 94 } 95 }; 96 run()97 public void run() { 98 for (int i = 0; i < ProxyFactoryPerformanceTest.COUNT; ++i) { 99 callCreateClass(); 100 } 101 } 102 callCreateClass()103 public void callCreateClass() { 104 try { 105 ProxyFactory factory = new ProxyFactory(); 106 factory.setSuperclass(SampleBean.class); 107 factory.setInterfaces(SampleBean.class.getInterfaces()); 108 factory.setFilter(FINALIZE_FILTER); 109 // factory.setHandler(this); 110 111 Class proxyClass = factory.createClass(); 112 //System.out.println("proxy name: " + proxyClass.getName()); 113 } catch (Throwable e) { 114 e.printStackTrace(); 115 ProxyFactoryPerformanceTest.error = e; 116 } 117 } 118 invoke(Object arg0, Method arg1, Method arg2, Object[] arg3)119 public Object invoke(Object arg0, Method arg1, Method arg2, Object[] arg3) throws Throwable { 120 return null; 121 } 122 } 123 124 /** 125 class EnhancerUser extends Thread implements InvocationHandler { 126 private static final CallbackFilter FINALIZE_FILTER = new CallbackFilter() { 127 public int accept(Method method) { 128 if ( method.getParameterTypes().length == 0 && method.getName().equals("finalize") ){ 129 return 1; 130 } 131 else { 132 return 0; 133 } 134 } 135 }; 136 137 public void run() { 138 for (int i = 0; i < ProxyFactoryPerformanceTest.COUNT; ++i) { 139 callCreateClass(); 140 } 141 } 142 143 public void callCreateClass() { 144 try { 145 Enhancer enhancer = new Enhancer(); 146 enhancer.setSuperclass(SampleBean.class); 147 enhancer.setInterfaces(SampleBean.class.getInterfaces()); 148 enhancer.setCallbackTypes(new Class[] { InvocationHandler.class, NoOp.class }); 149 enhancer.setCallbackFilter(FINALIZE_FILTER); 150 enhancer.setInterceptDuringConstruction(false); 151 // TODO 152 enhancer.setUseCache(false); 153 enhancer.setUseFactory(false); 154 Class proxyClass = enhancer.createClass(); 155 //System.out.println("proxy name: " + proxyClass.getName()); 156 } catch (Exception e) { 157 e.printStackTrace(); 158 } 159 } 160 161 public Object invoke(Object arg0, Method arg1, Object[] arg2) throws Throwable { 162 return null; 163 } 164 } 165 */ 166 167 class SampleBean implements Serializable { 168 /** default serialVersionUID */ 169 private static final long serialVersionUID = 1L; 170 171 long oid; 172 173 int version; 174 175 SampleBean bean; 176 setOid(long _oid)177 public void setOid(long _oid) { 178 oid = _oid; 179 } 180 getOid()181 public long getOid() { 182 return oid; 183 } 184 setVersion(int _ver)185 public void setVersion(int _ver) { 186 version = _ver; 187 } 188 getVersion()189 public int getVersion() { 190 return version; 191 } 192 setBean(SampleBean _bean)193 public void setBean(SampleBean _bean) { 194 bean = _bean; 195 } 196 getBean()197 public SampleBean getBean() { 198 return bean; 199 } 200 } 201