1 package tests.jfun.yan.benchmark; 6 7 import junit.framework.TestCase; 8 9 import org.springframework.beans.factory.xml.XmlBeanFactory; 10 import org.springframework.core.io.ClassPathResource; 11 12 import tests.jfun.yan.benchmark.models.Foo; 13 import tests.jfun.yan.benchmark.models.Soo; 14 15 16 public class SpringTest extends TestCase { 17 private static final long LOOP = 200000; 18 private XmlBeanFactory factory = null; 19 protected void setUp() throws Exception { 20 super.setUp(); 21 ClassPathResource res = new ClassPathResource("/tests/jfun/yan/benchmark/spring_component_config.xml"); 22 factory = new XmlBeanFactory(res); 23 assertNotNull(factory); 24 Thread.sleep(100); 25 System.gc(); 26 Thread.sleep(100); 27 System.gc(); 28 Thread.sleep(100); 29 } 30 31 protected void tearDown() throws Exception { 32 super.tearDown(); 33 } 34 35 public void testBenchCreateComponentInstance() throws Exception { 36 new Benchmark("Spring: Create bean without injection", LOOP){ 37 public void run() throws Exception { 38 factory.getBean("bar"); 39 } 40 }.start(true); 41 42 Soo soo = (Soo)factory.getBean("soo"); 43 assertNotNull(soo.getBar()); 44 } 45 46 public void testBenchConstructorInjection() throws Exception { 47 new Benchmark("Spring: Create bean with Constructor Dependency Injection", LOOP){ 48 public void run() throws Exception { 49 factory.getBean("foo"); 50 } 51 }.start(true); 52 Foo foo = (Foo)factory.getBean("foo"); 53 assertNotNull(foo.getBar()); 54 } 55 56 public void testBenchSetterInjectio() throws Exception { 57 new Benchmark("Spring: Create bean with Setter Dependency Injection", LOOP){ 58 public void run() throws Exception { 59 factory.getBean("soo"); 60 } 61 }.start(true); 62 Soo soo = (Soo)factory.getBean("soo"); 63 assertNotNull(soo.getBar()); 64 } 65 public void testBenchAutowiredSetterInjection() throws Exception { 66 new Benchmark("Spring: Create bean with bytype autowiring and Setter Dependency Injection", LOOP){ 67 public void run() throws Exception { 68 factory.getBean("auto_soo"); 69 } 70 }.start(true); 71 Soo soo = (Soo)factory.getBean("auto_soo"); 72 assertNotNull(soo.getBar()); 73 } 74 public void testBenchSingleton() throws Exception { 75 new Benchmark("Spring: Create singleton bean with Setter Dependency Injection", LOOP*10){ 76 public void run() throws Exception { 77 factory.getBean("ssoo"); 78 } 79 }.start(true); 80 Soo soo = (Soo)factory.getBean("ssoo"); 81 assertNotNull(soo.getBar()); 82 } 83 84 public void testBenchEmptyInterceptor() throws Exception { 85 Benchmark bench = new Benchmark("Spring: Bean method invocation with empty interceptor applied", LOOP * 100){ 86 Soo soo = (Soo)factory.getBean("sooProxy"); 87 public void run() throws Exception { 88 soo.noop(); 89 } 90 }; 91 bench.start(true); 92 } 93 94 public void testBenchCreateAsectizedBean() throws Exception { 95 new Benchmark("Spring: Create aspectized bean", LOOP/10 ){ 96 public void run() throws Exception { 97 factory.getBean("sooProxy"); 98 } 99 }.start(true); 100 } 101 } 102 | Popular Tags |