001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest;
005:
006: import com.tc.object.config.ConfigVisitor;
007: import com.tc.object.config.DSOClientConfigHelper;
008: import com.tc.object.config.TransparencyClassSpec;
009: import com.tc.simulator.app.ApplicationConfig;
010: import com.tc.simulator.listener.ListenerProvider;
011: import com.tctest.runner.AbstractTransparentApp;
012:
013: import java.awt.Color;
014: import java.lang.reflect.Array;
015: import java.lang.reflect.Field;
016:
017: /*
018: * The test cases defined in this class is for measuring the overhead of the instrumented Field class. For correctness
019: * tests for instrumented Field class, refer to the ReflectionFieldTestApp.
020: */
021: public class ReflectionPerformanceTestApp extends
022: AbstractTransparentApp {
023:
024: private final static int COUNT = 1000000;
025:
026: private final DataRoot dataRoot = new DataRoot();
027: private final DataRoot nonSharedObject = new DataRoot();
028: private final NonInstrumentedTestObject nonInstrumentedObject = new NonInstrumentedTestObject();
029:
030: public ReflectionPerformanceTestApp(String appId,
031: ApplicationConfig cfg, ListenerProvider listenerProvider) {
032: super (appId, cfg, listenerProvider);
033: }
034:
035: public void run() {
036: reflectionFieldPerformanceTest();
037: reflectionArrayPerformanceTest();
038: }
039:
040: private void reflectionArrayPerformanceTest() {
041: System.out
042: .println("==========Performance Tests for java.lang.reflect.Array begin==========");
043: System.out
044: .println("==========Performance Tests on Non-Instrumented Array begin==========");
045: modifyNonInstrumentedPrimitiveArrayTest();
046: System.gc();
047: modifyNonInstrumentedReferenceArrayTest();
048: System.out
049: .println("==========Performance Tests on Non-Instrumented Array end==========");
050: System.out.println();
051: System.gc();
052:
053: System.out
054: .println("==========Performance Tests on Non-Shared Array begin==========");
055: modifyNonSharedPrimitiveTest();
056: System.gc();
057: modifyNonSharedReferenceArrayTest();
058: System.out
059: .println("==========Performance Tests on Non-Shared Array end==========");
060: System.out.println();
061: System.gc();
062:
063: System.out
064: .println("==========Performance Tests on Shared Array begin==========");
065: modifySharedPrimitiveTest();
066: System.gc();
067: modifySharedReferenceArrayTest();
068: System.out
069: .println("==========Performance Tests on Shared Array end==========");
070: System.out
071: .println("==========Performance Tests for java.lang.reflect.Array end==========");
072: }
073:
074: private void modifySharedPrimitiveTest() {
075: long[] longArray = dataRoot.getLongArray();
076:
077: synchronized (dataRoot) {
078: long start = System.currentTimeMillis();
079: for (int i = 0; i < COUNT; i++) {
080: Array.setLong(longArray, 0, Long.MAX_VALUE);
081: }
082: long end = System.currentTimeMillis();
083: long elapsed = end - start;
084: System.out
085: .println("Elapsed time for modifying shared primitive array: "
086: + elapsed + "msec.");
087: }
088: }
089:
090: private void modifySharedReferenceArrayTest() {
091: Object[] objectArray = dataRoot.getObjectArray();
092: Object newValue = new Object();
093:
094: synchronized (dataRoot) {
095: long start = System.currentTimeMillis();
096: for (int i = 0; i < COUNT; i++) {
097: Array.set(objectArray, 0, newValue);
098: }
099: long end = System.currentTimeMillis();
100: long elapsed = end - start;
101: System.out
102: .println("Elapsed time for modifying shared reference array: "
103: + elapsed + "msec.");
104: }
105: }
106:
107: private void modifyNonSharedPrimitiveTest() {
108: long[] longArray = nonSharedObject.getLongArray();
109:
110: long start = System.currentTimeMillis();
111: for (int i = 0; i < COUNT; i++) {
112: Array.setLong(longArray, 0, Long.MAX_VALUE);
113: }
114: long end = System.currentTimeMillis();
115: long elapsed = end - start;
116: System.out
117: .println("Elapsed time for modifying non shared primitive array: "
118: + elapsed + "msec.");
119: }
120:
121: private void modifyNonSharedReferenceArrayTest() {
122: Object[] objectArray = nonSharedObject.getObjectArray();
123: Object newValue = new Object();
124:
125: long start = System.currentTimeMillis();
126: for (int i = 0; i < COUNT; i++) {
127: Array.set(objectArray, 0, newValue);
128: }
129: long end = System.currentTimeMillis();
130: long elapsed = end - start;
131: System.out
132: .println("Elapsed time for modifying non shared reference array: "
133: + elapsed + "msec.");
134: }
135:
136: private void modifyNonInstrumentedPrimitiveArrayTest() {
137: long[] longArray = nonInstrumentedObject.getLongArray();
138: long start = System.currentTimeMillis();
139: for (int i = 0; i < COUNT; i++) {
140: Array.setLong(longArray, 0, Long.MAX_VALUE);
141: }
142: long end = System.currentTimeMillis();
143: long elapsed = end - start;
144: System.out
145: .println("Elapsed time for modifying non instrumented primitive array: "
146: + elapsed + "msec.");
147: }
148:
149: private void modifyNonInstrumentedReferenceArrayTest() {
150: Object[] objectArray = nonInstrumentedObject.getObjectArray();
151: Object newObject = new Object();
152: long start = System.currentTimeMillis();
153: for (int i = 0; i < COUNT; i++) {
154: Array.set(objectArray, 0, newObject);
155: }
156: long end = System.currentTimeMillis();
157: long elapsed = end - start;
158: System.out
159: .println("Elapsed time for modifying non instrumented reference array: "
160: + elapsed + "msec.");
161: }
162:
163: private void reflectionFieldPerformanceTest() {
164: System.out
165: .println("==========Performance Tests for java.lang.reflect.Field begin==========");
166: System.out
167: .println("==========Performance Tests on Non-Instrumented Objects begin==========");
168: modifyNonInstrumentedObjectTest();
169: System.gc();
170: modifyNonInstrumentedObjectReferenceTest();
171: System.gc();
172: retrieveNonInstrumentedObjectTest();
173: System.out
174: .println("==========Performance Tests on Non-Instrumented Objects end==========");
175: System.out.println();
176: System.gc();
177:
178: System.out
179: .println("==========Performance Tests on Non-Shared Objects begin==========");
180: modifyNonSharedObjectTest();
181: System.gc();
182: modifyNonSharedObjectReferenceTest();
183: System.gc();
184: retrieveNonSharedObjectTest();
185: System.out
186: .println("==========Performance Tests on Non-Shared Objects end==========");
187: System.out.println();
188: System.gc();
189:
190: System.out
191: .println("==========Performance Tests on Shared Objects begin==========");
192: modifySharedObjectTest();
193: System.gc();
194: modifySharedObjectReferenceTest();
195: System.gc();
196: retrieveSharedObjectTest();
197: System.out
198: .println("==========Performance Tests on Shared Objects end==========");
199: System.out
200: .println("==========Performance Tests for java.lang.reflect.Field end==========");
201: System.out.println();
202: }
203:
204: private void modifyNonInstrumentedObjectTest() {
205: try {
206: Field longValueField = nonInstrumentedObject.getClass()
207: .getDeclaredField("longValue");
208: longValueField.setAccessible(true);
209: long start = System.currentTimeMillis();
210: for (int i = 0; i < COUNT; i++) {
211: longValueField.setLong(nonInstrumentedObject,
212: Long.MAX_VALUE);
213: }
214: long end = System.currentTimeMillis();
215: long elapsed = end - start;
216: System.out
217: .println("Elapsed time for modifying non instrumented objects: "
218: + elapsed + "msec.");
219: } catch (IllegalAccessException iae) {
220: // ignore IllegalAccessException in test.
221: } catch (NoSuchFieldException e) {
222: // ignore NoSuchFieldException in test.
223: }
224:
225: }
226:
227: private void modifyNonInstrumentedObjectReferenceTest() {
228: Color newColor = new Color(200, true);
229:
230: try {
231: Field colorField = nonInstrumentedObject.getClass()
232: .getDeclaredField("color");
233: colorField.setAccessible(true);
234: long start = System.currentTimeMillis();
235: for (int i = 0; i < COUNT; i++) {
236: colorField.set(nonInstrumentedObject, newColor);
237: }
238: long end = System.currentTimeMillis();
239: long elapsed = end - start;
240: System.out
241: .println("Elapsed time for modifying non instrumented object reference: "
242: + elapsed + "msec.");
243: } catch (IllegalAccessException iae) {
244: // ignore IllegalAccessException in test.
245: } catch (NoSuchFieldException e) {
246: // ignore NoSuchFieldException in test.
247: }
248:
249: }
250:
251: private void retrieveNonInstrumentedObjectTest() {
252: try {
253: Field colorField = nonInstrumentedObject.getClass()
254: .getDeclaredField("color");
255: colorField.setAccessible(true);
256: long start = System.currentTimeMillis();
257: for (int i = 0; i < COUNT; i++) {
258: colorField.get(nonInstrumentedObject);
259: }
260: long end = System.currentTimeMillis();
261: long elapsed = end - start;
262: System.out
263: .println("Elapsed time for retrieving non instrumented objects: "
264: + elapsed + "msec.");
265: } catch (IllegalAccessException iae) {
266: // ignore IllegalAccessException in test.
267: } catch (NoSuchFieldException e) {
268: // ignore NoSuchFieldException in test.
269: }
270:
271: }
272:
273: private void modifyNonSharedObjectTest() {
274: try {
275: Field longValueField = nonSharedObject.getClass()
276: .getDeclaredField("longValue");
277: longValueField.setAccessible(true);
278: long start = System.currentTimeMillis();
279: for (int i = 0; i < COUNT; i++) {
280: longValueField.setLong(nonSharedObject, Long.MAX_VALUE);
281: }
282: long end = System.currentTimeMillis();
283: long elapsed = end - start;
284: System.out
285: .println("Elapsed time for modifying non shared objects: "
286: + elapsed + "msec.");
287: } catch (IllegalAccessException iae) {
288: // ignore IllegalAccessException in test.
289: } catch (NoSuchFieldException e) {
290: // ignore NoSuchFieldException in test.
291: }
292:
293: }
294:
295: private void modifyNonSharedObjectReferenceTest() {
296: Color newColor = new Color(200, true);
297:
298: try {
299: Field colorField = nonSharedObject.getClass()
300: .getDeclaredField("color");
301: colorField.setAccessible(true);
302: long start = System.currentTimeMillis();
303: for (int i = 0; i < COUNT; i++) {
304: colorField.set(nonSharedObject, newColor);
305: }
306: long end = System.currentTimeMillis();
307: long elapsed = end - start;
308: System.out
309: .println("Elapsed time for modifying non shared object reference: "
310: + elapsed + "msec.");
311: } catch (IllegalAccessException iae) {
312: // ignore IllegalAccessException in test.
313: } catch (NoSuchFieldException e) {
314: // ignore NoSuchFieldException in test.
315: }
316:
317: }
318:
319: private void retrieveNonSharedObjectTest() {
320: try {
321: Field colorField = nonSharedObject.getClass()
322: .getDeclaredField("color");
323: colorField.setAccessible(true);
324: long start = System.currentTimeMillis();
325: for (int i = 0; i < COUNT; i++) {
326: colorField.get(nonSharedObject);
327: }
328: long end = System.currentTimeMillis();
329: long elapsed = end - start;
330: System.out
331: .println("Elapsed time for retrieving non shared objects: "
332: + elapsed + "msec.");
333: } catch (IllegalAccessException iae) {
334: // ignore IllegalAccessException in test.
335: } catch (NoSuchFieldException e) {
336: // ignore NoSuchFieldException in test.
337: }
338:
339: }
340:
341: private void modifySharedObjectTest() {
342: synchronized (dataRoot) {
343:
344: try {
345: Field longValueField = dataRoot.getClass()
346: .getDeclaredField("longValue");
347: longValueField.setAccessible(true);
348: long start = System.currentTimeMillis();
349: for (int i = 0; i < COUNT; i++) {
350: longValueField.setLong(dataRoot, Long.MAX_VALUE);
351: }
352: long end = System.currentTimeMillis();
353: long elapsed = end - start;
354: System.out
355: .println("Elapsed time for modifying shared objects: "
356: + elapsed + "msec.");
357: } catch (IllegalAccessException iae) {
358: // ignore IllegalAccessException in test.
359: } catch (NoSuchFieldException e) {
360: // ignore NoSuchFieldException in test.
361: }
362:
363: }
364: }
365:
366: private void modifySharedObjectReferenceTest() {
367: Color newColor = new Color(200, true);
368:
369: synchronized (dataRoot) {
370:
371: try {
372: Field colorField = dataRoot.getClass()
373: .getDeclaredField("color");
374: colorField.setAccessible(true);
375: long start = System.currentTimeMillis();
376: for (int i = 0; i < COUNT; i++) {
377: colorField.set(dataRoot, newColor);
378: }
379: long end = System.currentTimeMillis();
380: long elapsed = end - start;
381: System.out
382: .println("Elapsed time for modifying shared object reference: "
383: + elapsed + "msec.");
384: } catch (IllegalAccessException iae) {
385: // ignore IllegalAccessException in test.
386: } catch (NoSuchFieldException e) {
387: // ignore NoSuchFieldException in test.
388: }
389:
390: }
391: }
392:
393: private void retrieveSharedObjectTest() {
394: try {
395: Field colorField = dataRoot.getClass().getDeclaredField(
396: "color");
397: colorField.setAccessible(true);
398: long start = System.currentTimeMillis();
399: for (int i = 0; i < COUNT; i++) {
400: colorField.get(dataRoot);
401: }
402: long end = System.currentTimeMillis();
403: long elapsed = end - start;
404: System.out
405: .println("Elapsed time for retrieving shared objects: "
406: + elapsed + "msec.");
407: } catch (IllegalAccessException iae) {
408: // ignore IllegalAccessException in test.
409: } catch (NoSuchFieldException e) {
410: // ignore NoSuchFieldException in test.
411: }
412:
413: }
414:
415: public static void visitL1DSOConfig(ConfigVisitor visitor,
416: DSOClientConfigHelper config) {
417: String testClass = ReflectionPerformanceTestApp.class.getName();
418: String methodExpression = "* " + testClass + ".*(..)";
419: config.addWriteAutolock(methodExpression);
420: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
421: spec.addRoot("dataRoot", "dataRoot");
422: config.addIncludePattern(DataRoot.class.getName());
423: }
424:
425: private static class DataRoot {
426: private long[] longArray = new long[2];
427: private Object[] objectArray = new Object[2];
428: private Color color = new Color(100, true);
429: private long longValue = Long.MIN_VALUE;
430:
431: public DataRoot() {
432: super ();
433: }
434:
435: protected Color getColor() {
436: return color;
437: }
438:
439: protected void setColor(Color color) {
440: this .color = color;
441: }
442:
443: protected long getLongValue() {
444: return longValue;
445: }
446:
447: protected void setLongValue(long longValue) {
448: this .longValue = longValue;
449: }
450:
451: public long[] getLongArray() {
452: return longArray;
453: }
454:
455: public void setLongArray(long[] longArray) {
456: this .longArray = longArray;
457: }
458:
459: public Object[] getObjectArray() {
460: return objectArray;
461: }
462:
463: public void setObjectArray(Object[] objectArray) {
464: this.objectArray = objectArray;
465: }
466:
467: }
468:
469: }
|