KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > measurements > suites > JoinPointCostsMeasurement


1 package measurements.suites;
2
3 import java.lang.reflect.Field JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5
6 import junit.framework.Test;
7 import ch.ethz.inf.util.junit.PerformanceTest;
8 import ch.ethz.inf.util.junit.PerformanceTestSuite;
9 import ch.ethz.jvmai.*;
10
11 /**
12  * Measure the join point costs.
13  *
14  * @author Johann Gyger
15  */

16 public class JoinPointCostsMeasurement extends PerformanceTest {
17
18   public static final boolean CHECK_ASSERT = false;
19
20   public static final boolean USE_PROSE = false;
21
22   public static int fieldAccessCount;
23   public static int fieldModificationCount;
24   public static int methodEntryCount;
25   public static int methodExitCount;
26   public static int exceptionThrowCount;
27   public static int exceptionCatchCount;
28
29   public static Test suite() {
30     return new PerformanceTestSuite(JoinPointCostsMeasurement.class);
31   }
32
33   protected TestHook hook;
34
35   protected JVMAspectInterface aspectInterface;
36
37   protected MyTestClass testObject = new MyTestClass();
38
39   protected Method JavaDoc methodEntry;
40
41   protected Method JavaDoc methodExit;
42
43   protected Field JavaDoc field;
44
45   public JoinPointCostsMeasurement(String JavaDoc name) {
46     super(name);
47     RANGE = new int[] { 10000000 };
48   }
49
50   protected void setUp() throws Exception JavaDoc {
51     if (USE_PROSE) {
52       String JavaDoc providerName = System.getProperty("ch.ethz.prose.JVMAIProvider");
53       Class JavaDoc providerClass = Class.forName(providerName);
54       Provider provider = (Provider) providerClass.newInstance();
55
56       aspectInterface = provider.getAspectInterface();
57       aspectInterface.startup(null, true);
58
59       hook = new TestHook();
60       aspectInterface.setJoinPointHook(hook);
61
62       methodEntry = MyTestClass.class.getDeclaredMethod("myMethodEntry", new Class JavaDoc[] {});
63       methodExit = MyTestClass.class.getDeclaredMethod("myMethodExit", new Class JavaDoc[] {});
64       field = MyTestClass.class.getDeclaredField("myField");
65     }
66
67     fieldAccessCount = 0;
68     fieldModificationCount = 0;
69     methodEntryCount = 0;
70     methodExitCount = 0;
71     exceptionThrowCount = 0;
72     exceptionCatchCount = 0;
73   }
74
75   protected void tearDown() {
76     if (USE_PROSE)
77       aspectInterface.teardown();
78   }
79
80   public void testMethod0() {
81     testObject.myMethod(RUNS);
82   }
83
84   public void testMethodEntry() {
85     if (USE_PROSE) {
86       aspectInterface.setMethodEntryWatch(methodEntry, new Object JavaDoc());
87       aspectInterface.resumeNotification(Thread.currentThread());
88     }
89
90     testObject.myMethodEntry(RUNS);
91
92     if (CHECK_ASSERT)
93       assertEquals("Hook notifications", RUNS, methodEntryCount);
94   }
95
96   public void testMethodExit() {
97     if (USE_PROSE) {
98       aspectInterface.setMethodExitWatch(methodExit, new Object JavaDoc());
99       aspectInterface.resumeNotification(Thread.currentThread());
100     }
101
102     testObject.myMethodExit(RUNS);
103
104     if (CHECK_ASSERT)
105       assertEquals("Hook notifications", RUNS, methodExitCount);
106   }
107
108   public void testFieldAccess0() {
109     testObject.myFieldAccess(RUNS);
110   }
111
112   public void testFieldAccess() {
113     if (USE_PROSE) {
114       aspectInterface.setFieldAccessWatch(field, new Object JavaDoc());
115       aspectInterface.resumeNotification(Thread.currentThread());
116     }
117
118     testObject.myFieldAccess(RUNS);
119
120     if (CHECK_ASSERT)
121       assertEquals("Hook notifications", RUNS, fieldAccessCount);
122   }
123
124   public void testFieldModification0() {
125     testObject.myFieldModification(RUNS);
126   }
127
128   public void testFieldModification() {
129     if (USE_PROSE) {
130       aspectInterface.setFieldModificationWatch(field, new Object JavaDoc());
131       aspectInterface.resumeNotification(Thread.currentThread());
132     }
133
134     testObject.myFieldModification(RUNS);
135
136     if (CHECK_ASSERT)
137       assertEquals("Hook notifications", RUNS, fieldModificationCount);
138   }
139
140   public void testException0() {
141     RUNS = 1000000;
142     testObject.myException(RUNS);
143   }
144
145   public void testExceptionThrow() {
146     if (USE_PROSE) {
147       aspectInterface.setExceptionThrowWatch(MyException.class, new Object JavaDoc());
148       aspectInterface.resumeNotification(Thread.currentThread());
149     }
150
151     RUNS = 1000000;
152     testObject.myException(RUNS);
153
154     if (CHECK_ASSERT && USE_PROSE)
155       assertEquals("Hook notifications", RUNS, exceptionThrowCount);
156   }
157
158   public void testExceptionCatch() {
159     if (USE_PROSE) {
160       aspectInterface.setExceptionCatchWatch(MyException.class, new Object JavaDoc());
161       aspectInterface.resumeNotification(Thread.currentThread());
162     }
163
164     RUNS = 1000000;
165     testObject.myException(RUNS);
166
167     if (CHECK_ASSERT)
168       assertEquals("Hook notifications", RUNS, exceptionCatchCount);
169   }
170
171   public static class MyTestClass {
172
173     public static MyException myException = new MyException();
174
175     public int myField = 42;
176
177     public void myMethod() {}
178
179     public void myMethodEntry() {}
180
181     public void myMethodExit() {}
182
183     public void myMethod(int runs) {
184       startChronometer();
185       for (int i = 0; i < runs; i++)
186         myMethod();
187       stopChronometer();
188     }
189
190     public void myMethodEntry(int runs) {
191       startChronometer();
192       for (int i = 0; i < runs; i++)
193         myMethodEntry();
194       stopChronometer();
195     }
196
197     public void myMethodExit(int runs) {
198       startChronometer();
199       for (int i = 0; i < runs; i++)
200         myMethodExit();
201       stopChronometer();
202     }
203
204     public void myFieldAccess(int runs) {
205       int n = 0;
206
207       startChronometer();
208       for (int i = 0; i < runs; i++)
209         n = myField;
210       stopChronometer();
211     }
212
213     public void myFieldModification(int runs) {
214       startChronometer();
215       for (int i = 0; i < runs; i++)
216         myField = i;
217       stopChronometer();
218     }
219
220     public void myExceptionThrow() throws MyException {
221       throw myException;
222     }
223
224     public void myExceptionCatch() {
225       try {
226         myExceptionThrow();
227       } catch (MyException e) {}
228     }
229
230     public void myException(int runs) {
231       MyException e = new MyException();
232
233       startChronometer();
234       for (int i = 0; i < runs; i++)
235         myExceptionCatch();
236       stopChronometer();
237     }
238   }
239
240   public static class MyException extends Exception JavaDoc {};
241
242   public static class TestHook extends JoinPointHook {
243
244     public void onFieldAccess(FieldAccessJoinPoint joinPoint) {
245       fieldAccessCount++;
246     }
247
248     public void onFieldModification(FieldModificationJoinPoint joinPoint) {
249       fieldModificationCount++;
250     }
251
252     public void onMethodEntry(MethodEntryJoinPoint joinPoint) {
253       methodEntryCount++;
254     }
255
256     public void onMethodExit(MethodExitJoinPoint joinPoint) {
257       methodExitCount++;
258     }
259
260     public void onExceptionThrow(ExceptionJoinPoint joinPoint) {
261       exceptionThrowCount++;
262     }
263
264     public void onExceptionCatch(ExceptionCatchJoinPoint joinPoint) {
265       exceptionCatchCount++;
266     }
267
268     public void onClassLoad(Class JavaDoc cls) {}
269
270   }
271
272 }
273
Popular Tags