1 package tests.jfun.yan.benchmark; 2 3 import java.lang.reflect.Constructor ; 4 5 import org.springframework.beans.factory.FactoryBean; 6 7 import jfun.util.Misc; 8 import jfun.yan.Component; 9 import jfun.yan.Container; 10 import jfun.yan.etc.injection.Injection; 11 import jfun.yan.spring.SpringAdapter; 12 import jfun.yan.xml.NutsProcessor; 13 import jfun.yan.xml.nuts.spring.SpringNuts; 14 import junit.framework.TestCase; 15 16 import tests.jfun.yan.benchmark.models.Bar; 17 import tests.jfun.yan.benchmark.models.Foo; 18 import tests.jfun.yan.benchmark.models.Noop; 19 import tests.jfun.yan.benchmark.models.Soo; 20 21 public class NutsTest extends TestCase { 22 private static final boolean spring_involved = true; 23 private static final long LOOP = 200000; 24 private Container yan = null; 25 protected void setUp() throws Exception { 26 super.setUp(); 27 final NutsProcessor processor = new NutsProcessor(getClass().getClassLoader()); 28 if(spring_involved) 29 SpringNuts.setSpringAware("spring integration", processor); 30 processor.processResource("tests/jfun/yan/benchmark/yan_component_config.xml"); 31 this.yan = processor.getContainer(); 32 Thread.sleep(100); 33 System.gc(); 34 Thread.sleep(100); 35 System.gc(); 36 Thread.sleep(100); 37 } 38 39 protected void tearDown() throws Exception { 40 super.tearDown(); 41 } 42 43 public void testBenchCreateComponentInstance() throws Exception { 44 new Benchmark("Nuts: Create bean without injection", LOOP){ 45 public void run() throws Exception { 46 yan.getInstance("foo0"); 47 } 48 }.start(true); 49 50 Soo soo = (Soo)yan.getInstance("soo"); 51 assertNotNull(soo.getBar()); 52 } 53 public void testBenchCreateProxyInstance() throws Exception { 54 new Benchmark("Nuts: Create proxy without injection", LOOP){ 55 public void run() throws Exception { 56 yan.getInstance("barproxy"); 57 } 58 }.start(true); 59 60 Noop bar = (Noop)yan.getInstance("barproxy"); 61 assertNotNull(bar); 62 } 63 public void testBenchCreateComplexProxyInstance() throws Exception { 64 new Benchmark("Nuts: Create complex proxy without injection", LOOP){ 65 public void run() throws Exception { 66 yan.getInstance("complex_proxy"); 67 } 68 }.start(true); 69 70 Noop bar = (Noop)yan.getInstance("complex_proxy"); 71 assertNotNull(bar); 72 } 73 public void testBenchInjectionOnly() throws Exception { 74 final Soo soo = new Soo(); 75 new Benchmark("Nuts: <injection>", LOOP){ 76 public void run() throws Exception { 77 Injection inj = (Injection)yan.getInstance("injection"); 78 inj.inject(soo); 79 } 80 }.start(true); 81 82 Noop bar = (Noop)yan.getInstance("complex_proxy"); 83 assertNotNull(bar); 84 } 85 public void testBenchCreateComponentInstanceWithFactory() throws Exception { 86 final jfun.yan.factory.Factory factory = yan.getFactory("bar"); 87 new Benchmark("Nuts: Create bean without injection with Factory", LOOP){ 88 public void run() throws Exception { 89 factory.create(); 90 } 91 }.start(true); 92 } 93 public void testBenchPlainNew() throws Exception { 94 new Benchmark("Nuts: new Bar()", LOOP){ 95 public void run() throws Exception { 96 new Bar(); 97 } 98 }.start(true); 99 } 100 public void testBenchPlainReflection() throws Exception { 101 new Benchmark("Nuts: Bar.class.newInstance()", LOOP){ 102 public void run() throws Exception { 103 Bar.class.newInstance(); 104 } 105 }.start(true); 106 } 107 108 public void testBenchConstructorReflectionCall() throws Exception { 109 final Constructor ctor = Bar.class.getConstructor(null); 110 new Benchmark("Nuts: BarConstructor.newInstance()", LOOP){ 111 public void run() throws Exception { 112 ctor.newInstance(null); 113 } 114 }.start(true); 115 } 116 public void testBenchConstructorInjection() throws Exception { 117 new Benchmark("Nuts: Create bean with Constructor Dependency Injection", LOOP){ 118 public void run() throws Exception { 119 yan.getInstance("foo"); 120 } 121 }.start(true); 122 Foo foo = (Foo)yan.getInstance("foo"); 123 assertNotNull(foo.getBar()); 124 } 125 126 public void testBenchSetterInjectio() throws Exception { 127 new Benchmark("Nuts: Create bean with Setter Dependency Injection", LOOP){ 128 public void run() throws Exception { 129 yan.getInstance("soo"); 130 } 131 }.start(true); 132 Soo soo = (Soo)yan.getInstance("soo"); 133 assertNotNull(soo.getBar()); 134 } 135 public void testBenchBytypeSetterInjectio() throws Exception { 136 new Benchmark("Nuts: Create bean with bytype autowiring and Setter Dependency Injection", LOOP){ 137 public void run() throws Exception { 138 yan.getInstance("auto_soo"); 139 } 140 }.start(true); 141 Soo soo = (Soo)yan.getInstance("auto_soo"); 142 assertNotNull(soo.getBar()); 143 } 144 public void testBenchSingleton() throws Exception { 145 new Benchmark("Nuts: Create singleton bean with Setter Dependency Injection", LOOP*10){ 146 public void run() throws Exception { 147 yan.getInstance("ssoo"); 148 } 149 }.start(true); 150 Soo soo = (Soo)yan.getInstance("ssoo"); 151 assertNotNull(soo.getBar()); 152 } 153 public void testBenchSingletonFactory() throws Exception { 154 final jfun.yan.factory.Factory factory = yan.getFactory("ssoo"); 155 Benchmark bench = new Benchmark("Nuts: Singleton Bean with Factory", LOOP*10){ 156 157 public void run() throws Exception { 158 final Soo foo = (Soo)factory.create(); 159 foo.noop(); 160 } 161 }; 162 bench.start(true); 163 } 164 public void testBenchSingletonCustomFactory() throws Exception { 165 final Component ssoo = yan.getComponent("ssoo").factory(MyFactory.class); 166 final MyFactory factory = (MyFactory)yan.instantiateComponent(ssoo); 167 Benchmark bench = new Benchmark("Nuts: Singleton Bean with custom factory", LOOP*10){ 168 169 public void run() throws Exception { 170 final Soo foo = (Soo)factory.create(); 171 foo.noop(); 172 } 173 }; 174 bench.start(true); 175 } 176 public void testCtorFactory() throws Exception { 177 final jfun.yan.factory.Factory factory = yan.getFactory("foo"); 178 Benchmark bench = new Benchmark("Nuts: Constructor Injection with Factory", LOOP){ 179 180 public void run() throws Exception { 181 final Foo foo = (Foo)factory.create(); 182 foo.noop(); 183 } 184 }; 185 bench.start(true); 186 } 187 public void testSetterFactory() throws Exception { 188 final jfun.yan.factory.Factory factory = yan.getFactory("soo"); 189 Benchmark bench = new Benchmark("Nuts: Setter Injection with Factory", LOOP){ 190 191 public void run() throws Exception { 192 final Soo foo = (Soo)factory.create(); 193 foo.noop(); 194 } 195 }; 196 bench.start(true); 197 } 198 199 public void testBenchEmptyInterceptor() throws Exception { 200 if(!spring_involved) return; 201 Benchmark bench = new Benchmark("Nuts: Bean method invocation with empty interceptor applied", LOOP * 100){ 202 Soo foo = (Soo)yan.getInstance("sooProxy"); 203 public void run() throws Exception { 204 foo.noop(); 205 } 206 }; 207 bench.start(true); 208 FactoryBean fb = SpringAdapter.getFactoryBean(yan, "fooProxy"); 209 assertNotNull(fb); 210 } 211 212 public void testBenchCreateAsectizedBean() throws Exception { 213 if(!spring_involved) return; 214 new Benchmark("Nuts: Create aspectized bean", LOOP/10 ){ 215 private Object old = null; 216 public void run() throws Exception { 217 final Soo nw = (Soo)yan.getInstance("sooProxy"); 218 if(old!=null){ 219 assertNotSame(old, nw); 220 } 221 old = nw; 222 } 223 }.start(true); 224 } 225 } 226 | Popular Tags |