KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > PerformanceTest


1 /*
2  * Nanning Aspects
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

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 JavaDoc;
15
16 /**
17  * TODO document PerformanceTest
18  *
19  * <!-- $Id: PerformanceTest.java,v 1.19 2003/05/11 13:40:52 tirsen Exp $ -->
20  *
21  * @author $Author: tirsen $
22  * @version $Revision: 1.19 $
23  */

24 public class PerformanceTest extends AbstractAttributesTest {
25     public void testInvocation() throws IllegalAccessException JavaDoc, InstantiationException JavaDoc {
26         // these are exceptionally high due to Clover...
27
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         ///CLOVER:OFF
36
StopWatch ordinary = new StopWatch();
37
38         for (int i = 0; i < numberOfInvocations; i++) {
39             intf.call();
40         }
41
42         ordinary.stop();
43         ///CLOVER:ON
44

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         ///CLOVER:OFF
54
StopWatch aspect = new StopWatch();
55
56         for (int i = 0; i < numberOfInvocations; i++) {
57             intf.call();
58         }
59
60         aspect.stop();
61         ///CLOVER:ON
62

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("ordinaryTime = " + ordinary.getTimeSpent());
70
// System.out.println("ordinaryMemory = " + ordinary.getMemoryUsed());
71
// System.out.println("aspectsTime = " + aspect.getTimeSpent());
72
// System.out.println("aspectsMemory = " + aspect.getMemoryUsed());
73
// System.out.println("memoryPerInvocation = " + aspect.getMemoryUsed(numberOfInvocations));
74
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 JavaDoc {
82         long maxTime = 63;
83         long maxMemory = 0;
84
85         // let the cache do it's thang
86
Attributes.getAttribute(AttributesTestClass.class, "class.attribute");
87         Method JavaDoc 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 JavaDoc {
104         long maxTime = 17;
105         long maxMemory = 180000;
106
107         // let the cache do it's thang
108
Attributes.getInheritedAttribute(AttributesTestClass.class, "class.attribute");
109         Method JavaDoc 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