1   package org.neo.swarm.core.aop.silc;
2   
3   import junit.framework.TestCase;
4   import net.sf.cglib.proxy.Enhancer;
5   import net.sf.cglib.proxy.MethodProxy;
6   
7   import org.neo.swarm.JellyBean;
8   import org.neo.swarm.core.aop.silc.comp.AspectComponentImpl;
9   import org.neo.swarm.core.aop.silc.comp.CGLibProxyFactory;
10  import org.neo.swarm.core.aop.silc.comp.JavaDynamicProxyFactory;
11  
12  import java.lang.reflect.InvocationHandler;
13  import java.lang.reflect.Method;
14  import java.lang.reflect.Proxy;
15  
16  public class ProxyPerformanceTest extends TestCase {
17      private JellyBean bean;
18  
19      public void testBasicProxies() {
20          bean = new JellyBean();
21  
22          int iterations = 100000;
23  
24          ProxyablePerfTester javaPerf = new ProxyablePerfTester(bean, iterations, "Java");
25          ProxyablePerfTester cglPerf = new ProxyablePerfTester(createStraightCGLibProxy(), iterations, "CGLib Generic Proxy");
26          ProxyablePerfTester acjPerf = new ProxyablePerfTester(createAspectedJProxy(), iterations,  "J AspectComponent Proxy");
27          ProxyablePerfTester cglMPerf = new ProxyablePerfTester(createCGLibMethodProxy(), iterations, "CGLib standard method proxy");
28          ProxyablePerfTester acCGLPerf = new ProxyablePerfTester(createAspectedCgProxy(), iterations, "CGLib AspectComponent Proxy");
29          ProxyablePerfTester javaProxyPerf = new ProxyablePerfTester(createJavaProxy(), iterations, "Java Proxy");
30  
31  
32          ProxyablePerfTester[] tests = {javaPerf, acCGLPerf,acjPerf, cglPerf, cglMPerf, javaProxyPerf};
33  
34  
35  
36          for (int i = 0; i < tests.length; i++) {
37              ProxyablePerfTester test = tests[i];
38              test.run();
39              test.printResults();
40          }
41      }
42  
43      private Proxyable createCGLibMethodProxy() {
44          final JellyBean jb = new JellyBean();
45          Enhancer e = new Enhancer();
46          e.setInterfaces(new Class[]{Proxyable.class});
47          e.setCallback(new net.sf.cglib.proxy.MethodInterceptor() {
48              public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
49                  return methodProxy.invoke(jb, objects);
50              }
51          });
52          return (Proxyable) e.create();
53  
54      }
55  
56      private Proxyable createAspectedJProxy() {
57          JellyBean jb = new JellyBean();
58          AspectComponentImpl ac = new AspectComponentImpl("blah", jb.getClass().getInterfaces(), jb, new JavaDynamicProxyFactory());
59          return (Proxyable) ac.getProxy();
60      }
61  
62      private Proxyable createAspectedCgProxy() {
63          JellyBean jb = new JellyBean();
64          AspectComponentImpl ac = new AspectComponentImpl("blah", jb.getClass().getInterfaces(), jb, new CGLibProxyFactory());
65          return (Proxyable) ac.getCGLibProxy();
66      }
67  
68      public Proxyable createJavaProxy() {
69          return (Proxyable) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{Proxyable.class}, new InvocationHandler() {
70              public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
71                  return method.invoke(bean, objects);
72              }
73          });
74      }
75  
76      public Proxyable createStraightCGLibProxy () {
77          Enhancer e = new Enhancer();
78          e.setInterfaces(new Class[]{Proxyable.class});
79          e.setCallback(new net.sf.cglib.proxy.InvocationHandler() {
80              public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
81                 return method.invoke(bean, objects);
82              }
83          });
84          return (Proxyable) e.create();
85      }
86  
87      class ProxyablePerfTester {
88          private Proxyable proxyAble;
89          private int iterations;
90          private String testName;
91          private long split;
92  
93          public ProxyablePerfTester(Proxyable proxyAble, int iterations, String testName) {
94              this.proxyAble = proxyAble;
95              this.iterations = iterations;
96              this.testName = testName;
97          }
98  
99          public void run() {
100             warmup();
101             long start = System.currentTimeMillis();
102             for (int i = 0; i < iterations; i++) {
103                 proxyAble.doSomeShit();
104             }
105             split = System.currentTimeMillis() - start;
106         }
107 
108         public void printResults() {
109             double invs = split / (double) iterations;
110             double elapsed = split / 1000.0;
111             double invsPerSec = iterations / elapsed;
112 
113             System.out.println(testName + " Elapsed millis:" + split);
114             System.out.println(testName + " Time per Invocation:" + invs);
115             System.out.println(testName + " Invocations per second:" + invsPerSec);
116         }
117 
118         private void warmup() {
119             for (int i = 0; i < 1000000; i++) {
120                 proxyAble.doSomeShit();
121             }
122         }
123 
124     }
125 }