001: //$Id$
002: //=====================================================================
003: //
004: //(history at end)
005: //
006:
007: package measurements.suites;
008:
009: import java.lang.reflect.Field;
010: import java.lang.reflect.Method;
011:
012: import junit.framework.Test;
013: import ch.ethz.inf.util.junit.PerformanceTest;
014: import ch.ethz.inf.util.junit.PerformanceTestSuite;
015: import ch.ethz.jvmai.*;
016:
017: /**
018: * Measure the join point costs.
019: *
020: * @version $Revision$
021: * @author Johann Gyger
022: */
023: public class JoinPointCostsMeasurement extends PerformanceTest {
024:
025: public static final boolean CHECK_ASSERT = false;
026:
027: public static final boolean USE_PROSE = false;
028:
029: public static int fieldAccessCount;
030: public static int fieldModificationCount;
031: public static int methodEntryCount;
032: public static int methodExitCount;
033: public static int exceptionThrowCount;
034: public static int exceptionCatchCount;
035:
036: public static Test suite() {
037: return new PerformanceTestSuite(JoinPointCostsMeasurement.class);
038: }
039:
040: protected TestHook hook;
041:
042: protected JVMAspectInterface aspectInterface;
043:
044: protected MyTestClass testObject = new MyTestClass();
045:
046: protected Method methodEntry;
047:
048: protected Method methodExit;
049:
050: protected Field field;
051:
052: public JoinPointCostsMeasurement(String name) {
053: super (name);
054: RANGE = new int[] { 10000000 };
055: }
056:
057: protected void setUp() throws Exception {
058: if (USE_PROSE) {
059: String providerName = System
060: .getProperty("ch.ethz.prose.JVMAIProvider");
061: Class providerClass = Class.forName(providerName);
062: Provider provider = (Provider) providerClass.newInstance();
063:
064: aspectInterface = provider.getAspectInterface();
065: aspectInterface.startup(null, true);
066:
067: hook = new TestHook();
068: aspectInterface.setJoinPointHook(hook);
069:
070: methodEntry = MyTestClass.class.getDeclaredMethod(
071: "myMethodEntry", new Class[] {});
072: methodExit = MyTestClass.class.getDeclaredMethod(
073: "myMethodExit", new Class[] {});
074: field = MyTestClass.class.getDeclaredField("myField");
075: }
076:
077: fieldAccessCount = 0;
078: fieldModificationCount = 0;
079: methodEntryCount = 0;
080: methodExitCount = 0;
081: exceptionThrowCount = 0;
082: exceptionCatchCount = 0;
083: }
084:
085: protected void tearDown() {
086: if (USE_PROSE)
087: aspectInterface.teardown();
088: }
089:
090: public void testMethod0() {
091: testObject.myMethod(RUNS);
092: }
093:
094: public void testMethodEntry() {
095: if (USE_PROSE) {
096: aspectInterface.setMethodEntryWatch(methodEntry,
097: new Object());
098: aspectInterface.resumeNotification(Thread.currentThread());
099: }
100:
101: testObject.myMethodEntry(RUNS);
102:
103: if (CHECK_ASSERT)
104: assertEquals("Hook notifications", RUNS, methodEntryCount);
105: }
106:
107: public void testMethodExit() {
108: if (USE_PROSE) {
109: aspectInterface
110: .setMethodExitWatch(methodExit, new Object());
111: aspectInterface.resumeNotification(Thread.currentThread());
112: }
113:
114: testObject.myMethodExit(RUNS);
115:
116: if (CHECK_ASSERT)
117: assertEquals("Hook notifications", RUNS, methodExitCount);
118: }
119:
120: public void testFieldAccess0() {
121: testObject.myFieldAccess(RUNS);
122: }
123:
124: public void testFieldAccess() {
125: if (USE_PROSE) {
126: aspectInterface.setFieldAccessWatch(field, new Object());
127: aspectInterface.resumeNotification(Thread.currentThread());
128: }
129:
130: testObject.myFieldAccess(RUNS);
131:
132: if (CHECK_ASSERT)
133: assertEquals("Hook notifications", RUNS, fieldAccessCount);
134: }
135:
136: public void testFieldModification0() {
137: testObject.myFieldModification(RUNS);
138: }
139:
140: public void testFieldModification() {
141: if (USE_PROSE) {
142: aspectInterface.setFieldModificationWatch(field,
143: new Object());
144: aspectInterface.resumeNotification(Thread.currentThread());
145: }
146:
147: testObject.myFieldModification(RUNS);
148:
149: if (CHECK_ASSERT)
150: assertEquals("Hook notifications", RUNS,
151: fieldModificationCount);
152: }
153:
154: public void testException0() {
155: RUNS = 1000000;
156: testObject.myException(RUNS);
157: }
158:
159: public void testExceptionThrow() {
160: if (USE_PROSE) {
161: aspectInterface.setExceptionThrowWatch(MyException.class,
162: new Object());
163: aspectInterface.resumeNotification(Thread.currentThread());
164: }
165:
166: RUNS = 1000000;
167: testObject.myException(RUNS);
168:
169: if (CHECK_ASSERT && USE_PROSE)
170: assertEquals("Hook notifications", RUNS,
171: exceptionThrowCount);
172: }
173:
174: public void testExceptionCatch() {
175: if (USE_PROSE) {
176: aspectInterface.setExceptionCatchWatch(MyException.class,
177: new Object());
178: aspectInterface.resumeNotification(Thread.currentThread());
179: }
180:
181: RUNS = 1000000;
182: testObject.myException(RUNS);
183:
184: if (CHECK_ASSERT)
185: assertEquals("Hook notifications", RUNS,
186: exceptionCatchCount);
187: }
188:
189: public static class MyTestClass {
190:
191: public static MyException myException = new MyException();
192:
193: public int myField = 42;
194:
195: public void myMethod() {
196: }
197:
198: public void myMethodEntry() {
199: }
200:
201: public void myMethodExit() {
202: }
203:
204: public void myMethod(int runs) {
205: startChronometer();
206: for (int i = 0; i < runs; i++)
207: myMethod();
208: stopChronometer();
209: }
210:
211: public void myMethodEntry(int runs) {
212: startChronometer();
213: for (int i = 0; i < runs; i++)
214: myMethodEntry();
215: stopChronometer();
216: }
217:
218: public void myMethodExit(int runs) {
219: startChronometer();
220: for (int i = 0; i < runs; i++)
221: myMethodExit();
222: stopChronometer();
223: }
224:
225: public void myFieldAccess(int runs) {
226: int n = 0;
227:
228: startChronometer();
229: for (int i = 0; i < runs; i++)
230: n = myField;
231: stopChronometer();
232: }
233:
234: public void myFieldModification(int runs) {
235: startChronometer();
236: for (int i = 0; i < runs; i++)
237: myField = i;
238: stopChronometer();
239: }
240:
241: public void myExceptionThrow() throws MyException {
242: throw myException;
243: }
244:
245: public void myExceptionCatch() {
246: try {
247: myExceptionThrow();
248: } catch (MyException e) {
249: }
250: }
251:
252: public void myException(int runs) {
253: MyException e = new MyException();
254:
255: startChronometer();
256: for (int i = 0; i < runs; i++)
257: myExceptionCatch();
258: stopChronometer();
259: }
260: }
261:
262: public static class MyException extends Exception {
263: };
264:
265: public static class TestHook extends JoinPointHook {
266:
267: public void onFieldAccess(FieldAccessJoinPoint joinPoint) {
268: fieldAccessCount++;
269: }
270:
271: public void onFieldModification(
272: FieldModificationJoinPoint joinPoint) {
273: fieldModificationCount++;
274: }
275:
276: public void onMethodEntry(MethodEntryJoinPoint joinPoint) {
277: methodEntryCount++;
278: }
279:
280: public void onMethodExit(MethodExitJoinPoint joinPoint) {
281: methodExitCount++;
282: }
283:
284: public void onExceptionThrow(ExceptionJoinPoint joinPoint) {
285: exceptionThrowCount++;
286: }
287:
288: public void onExceptionCatch(ExceptionCatchJoinPoint joinPoint) {
289: exceptionCatchCount++;
290: }
291:
292: public void onClassLoad(Class cls) {
293: }
294:
295: public void onConstructor(ConstructorJoinPoint joinpoint) {
296: }
297:
298: }
299:
300: }
301:
302: //======================================================================
303: //
304: // $Log$
305: //
|