001: package org.apache.ojb.broker.metadata;
002:
003: import org.apache.commons.lang.ClassUtils;
004: import org.apache.commons.lang.SystemUtils;
005: import org.apache.ojb.broker.NestedFieldsTest;
006: import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
007: import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldAutoProxyImpl;
008: import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDirectImpl;
009: import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldIntrospectorImpl;
010: import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldPrivilegedImpl;
011: import org.apache.ojb.broker.util.ClassHelper;
012: import org.apache.ojb.junit.OJBTestCase;
013:
014: /* Copyright 2002-2005 The Apache Software Foundation
015: *
016: * Licensed under the Apache License, Version 2.0 (the "License");
017: * you may not use this file except in compliance with the License.
018: * You may obtain a copy of the License at
019: *
020: * http://www.apache.org/licenses/LICENSE-2.0
021: *
022: * Unless required by applicable law or agreed to in writing, software
023: * distributed under the License is distributed on an "AS IS" BASIS,
024: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
025: * See the License for the specific language governing permissions and
026: * limitations under the License.
027: */
028:
029: /**
030: * This is a developer test and NOT part of the test suite.
031: * This test help to test the performance of the different
032: * {@link org.apache.ojb.broker.metadata.fieldaccess.PersistentField}
033: * implementations.
034: *
035: * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
036: * @version $Id: PersistentFieldPerfTest.java,v 1.4.2.2 2005/12/21 22:31:24 tomdz Exp $
037: */
038: public class PersistentFieldPerfTest extends OJBTestCase {
039: String EOL = SystemUtils.LINE_SEPARATOR;
040: Class testClass = NestedFieldsTest.NestedMain.class;
041: String fieldName = "name";
042: String fieldNameNested = "nestedDetail::nestedDetailDetail::realDetailName";
043: int numberOfOperations = 30000;
044: int repeat = 5;
045:
046: public static void main(String[] args) {
047: String[] arr = { PersistentFieldPerfTest.class.getName() };
048: junit.textui.TestRunner.main(arr);
049: }
050:
051: Class[] persistentFieldClasses = new Class[] {
052: PersistentFieldDirectImpl.class,
053: PersistentFieldIntrospectorImpl.class,
054: PersistentFieldPrivilegedImpl.class,
055: PersistentFieldAutoProxyImpl.class };
056:
057: private PersistentField newInstance(Class pfClass, Class testClass,
058: String fieldName) throws Exception {
059: Class[] types = new Class[] { Class.class, String.class };
060: Object[] args = new Object[] { testClass, fieldName };
061: return (PersistentField) ClassHelper.newInstance(pfClass,
062: types, args);
063: }
064:
065: public void testFieldPerformance() throws Exception {
066: System.out.println();
067: System.out.println("=========================================");
068: System.out.println("Field performance, set/get "
069: + numberOfOperations + " times a field");
070: System.out.println("----------------------------------------");
071: for (int i = 0; i < persistentFieldClasses.length; i++) {
072: Class persistentFieldClass = persistentFieldClasses[i];
073: PersistentField p = newInstance(persistentFieldClass,
074: testClass, fieldName);
075: buildTestFor(p, false);
076: }
077: System.out.println("----------------------------------------");
078: for (int i = 0; i < persistentFieldClasses.length; i++) {
079: Class persistentFieldClass = persistentFieldClasses[i];
080: PersistentField p = newInstance(persistentFieldClass,
081: testClass, fieldName);
082: buildTestFor(p, false);
083: }
084: System.out.println("----------------------------------------");
085: }
086:
087: public void testNestedFieldPerformance() throws Exception {
088: System.out.println();
089: System.out.println("=========================================");
090: System.out.println("Nested Field performance, set/get "
091: + numberOfOperations + " times a nested field");
092: System.out.println("----------------------------------------");
093: for (int i = 0; i < persistentFieldClasses.length; i++) {
094: Class persistentFieldClass = persistentFieldClasses[i];
095: PersistentField p = newInstance(persistentFieldClass,
096: testClass, fieldNameNested);
097: buildTestFor(p, true);
098: }
099: System.out.println("----------------------------------------");
100: for (int i = 0; i < persistentFieldClasses.length; i++) {
101: Class persistentFieldClass = persistentFieldClasses[i];
102: PersistentField p = newInstance(persistentFieldClass,
103: testClass, fieldNameNested);
104: buildTestFor(p, true);
105: }
106: System.out.println("----------------------------------------");
107: }
108:
109: private void buildTestFor(PersistentField pf, boolean nested)
110: throws Exception {
111: long getter = 0;
112: long setter = 0;
113: for (int i = 0; i < repeat; i++) {
114: System.gc();
115: Thread.sleep(100);
116: getter += nested ? getterPerformanceNestedFor(pf)
117: : getterPerformanceFor(pf);
118: }
119: for (int i = 0; i < repeat; i++) {
120: System.gc();
121: Thread.sleep(100);
122: setter += nested ? setterPerformanceNestedFor(pf)
123: : setterPerformanceFor(pf);
124: }
125: printResult(pf, getter, setter, nested);
126: }
127:
128: private void printResult(PersistentField pf, long getterPeriod,
129: long setterPeriod, boolean nested) {
130:
131: System.out.println(ClassUtils.getShortClassName(pf.getClass())
132: + (nested ? ": nestedGetter=" : ": getter=")
133: + getterPeriod
134: + (nested ? " nestedSetter=" : " setter=")
135: + setterPeriod);
136: }
137:
138: private long getterPerformanceFor(PersistentField pf) {
139: String testString = "a test name";
140: NestedFieldsTest.NestedMain testObject = new NestedFieldsTest.NestedMain();
141: testObject.setName(testString);
142: // validate
143: assertEquals(testString, pf.get(testObject));
144:
145: long period = System.currentTimeMillis();
146: for (int i = 0; i < numberOfOperations; i++) {
147: pf.get(testObject);
148: }
149: return System.currentTimeMillis() - period;
150: }
151:
152: private long setterPerformanceFor(PersistentField pf) {
153: String testString = "a test name";
154: NestedFieldsTest.NestedMain testObject = new NestedFieldsTest.NestedMain();
155: // validate
156: pf.set(testObject, testString);
157: assertEquals(testString, testObject.getName());
158: long period = System.currentTimeMillis();
159: for (int i = 0; i < numberOfOperations; i++) {
160: pf.set(testObject, testString);
161: }
162: return System.currentTimeMillis() - period;
163: }
164:
165: private long getterPerformanceNestedFor(PersistentField pf) {
166: String testString = "a test name";
167: NestedFieldsTest.NestedMain testObject = new NestedFieldsTest.NestedMain();
168: NestedFieldsTest.NestedDetail d1 = new NestedFieldsTest.NestedDetail();
169: NestedFieldsTest.NestedDetailDetail d2 = new NestedFieldsTest.NestedDetailDetail();
170: d2.setRealDetailName(testString);
171: d1.setNestedDetailDetail(d2);
172: testObject.setNestedDetail(d1);
173: // validate
174: assertEquals(testString, pf.get(testObject));
175:
176: long period = System.currentTimeMillis();
177: for (int i = 0; i < numberOfOperations; i++) {
178: pf.get(testObject);
179: }
180: return System.currentTimeMillis() - period;
181: }
182:
183: private long setterPerformanceNestedFor(PersistentField pf) {
184: String testString = "a test name";
185: NestedFieldsTest.NestedMain testObject = new NestedFieldsTest.NestedMain();
186: // validate
187: pf.set(testObject, testString);
188: assertNotNull(testObject.getNestedDetail());
189: assertNotNull(testObject.getNestedDetail()
190: .getNestedDetailDetail());
191: assertEquals(testString, testObject.getNestedDetail()
192: .getNestedDetailDetail().getRealDetailName());
193: assertEquals(testString, testObject.getNestedDetail()
194: .getNestedDetailDetail().getRealDetailName());
195: long period = System.currentTimeMillis();
196: for (int i = 0; i < numberOfOperations; i++) {
197: pf.set(testObject, testString);
198: }
199: return System.currentTimeMillis() - period;
200: }
201: }
|