1 7 package com.tirsen.nanning; 8 9 import com.tirsen.nanning.attribute.AbstractAttributesTest; 10 import com.tirsen.nanning.attribute.Attributes; 11 import com.tirsen.nanning.attribute.AttributesTestClass; 12 import com.tirsen.nanning.samples.StopWatch; 13 14 import java.lang.reflect.Method ; 15 16 24 public class PerformanceTest extends AbstractAttributesTest { 25 public void testInvocation() throws IllegalAccessException , InstantiationException { 26 double maxMemoryPerInvocation = 8; 28 double timesSlowerTolerance = 22; 29 double maxTimePerInvocation = 0.014; 30 31 int numberOfInvocations = 100000; 32 33 Intf intf = new IntfImpl(); 34 35 StopWatch ordinary = new StopWatch(); 37 38 for (int i = 0; i < numberOfInvocations; i++) { 39 intf.call(); 40 } 41 42 ordinary.stop(); 43 45 AspectInstance instance = new AspectInstance(); 46 MixinInstance mixin = new MixinInstance(Intf.class, new IntfImpl()); 47 mixin.addInterceptor(new NullInterceptor()); 48 mixin.addInterceptor(new NullInterceptor()); 49 instance.addMixin(mixin); 50 51 intf = (Intf) instance.getProxy(); 52 53 StopWatch aspect = new StopWatch(); 55 56 for (int i = 0; i < numberOfInvocations; i++) { 57 intf.call(); 58 } 59 60 aspect.stop(); 61 63 double timesSlower = aspect.getTimeSpent() / ordinary.getTimeSpent(); 64 double timesMoreMemory = aspect.getMemoryUsed() / ordinary.getMemoryUsed(); 65 66 System.out.println(); 67 System.out.println("timesSlower = " + timesSlower); 68 System.out.println("timesMoreMemory = " + timesMoreMemory); 69 System.out.println("timePerInvocation = " + aspect.getTimeSpent(numberOfInvocations)); 75 76 assertTrue("memory per invocation exceeded", aspect.getMemoryUsed(numberOfInvocations) < maxMemoryPerInvocation); 77 assertTrue("time per invocation exceeded", aspect.getTimeSpent(numberOfInvocations) < maxTimePerInvocation); 78 assertTrue("time per invocation exceeded", timesSlowerTolerance > timesSlower); 79 } 80 81 public void testAttributes() throws NoSuchMethodException { 82 long maxTime = 63; 83 long maxMemory = 0; 84 85 Attributes.getAttribute(AttributesTestClass.class, "class.attribute"); 87 Method method = AttributesTestClass.class.getMethod("method", null); 88 StopWatch stopWatch = new StopWatch(true); 89 90 for (int i = 0; i < 1000; i++) { 91 assertEquals("classValue", Attributes.getAttribute(AttributesTestClass.class, "class.attribute")); 92 assertEquals("methodValue", Attributes.getAttribute(method, "method.attribute")); 93 } 94 95 stopWatch.stop(); 96 System.out.println(); 97 System.out.println("time spent " + stopWatch.getTimeSpent()); 98 System.out.println("memory used " + stopWatch.getMemoryUsed()); 99 assertTrue("time exceeded", stopWatch.getTimeSpent() <= maxTime); 100 assertTrue("memory exceeded", stopWatch.getMemoryUsed() <= maxMemory); 101 } 102 103 public void testInheritedAttributes() throws NoSuchMethodException { 104 long maxTime = 17; 105 long maxMemory = 180000; 106 107 Attributes.getInheritedAttribute(AttributesTestClass.class, "class.attribute"); 109 Method method = AttributesTestClass.class.getMethod("method", null); 110 StopWatch stopWatch = new StopWatch(true); 111 112 for (int i = 0; i < 1000; i++) { 113 assertEquals("classValue", Attributes.getInheritedAttribute(AttributesTestClass.class, "class.attribute")); 114 assertEquals("methodValue", Attributes.getAttribute(method, "method.attribute")); 115 } 116 117 stopWatch.stop(); 118 System.out.println(); 119 System.out.println("time spent " + stopWatch.getTimeSpent()); 120 System.out.println("memory used " + stopWatch.getMemoryUsed()); 121 assertTrue("time exceeded", stopWatch.getTimeSpent() < maxTime); 122 assertTrue("memory exceeded", stopWatch.getMemoryUsed() < maxMemory); 123 } 124 } 125 | Popular Tags |