KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > metadata > PersistentFieldPerfTest


1 package org.apache.ojb.broker.metadata;
2
3 import org.apache.commons.lang.ClassUtils;
4 import org.apache.commons.lang.SystemUtils;
5 import org.apache.ojb.broker.NestedFieldsTest;
6 import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
7 import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldAutoProxyImpl;
8 import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldDirectImpl;
9 import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldIntrospectorImpl;
10 import org.apache.ojb.broker.metadata.fieldaccess.PersistentFieldPrivilegedImpl;
11 import org.apache.ojb.broker.util.ClassHelper;
12 import org.apache.ojb.junit.OJBTestCase;
13
14 /* Copyright 2002-2005 The Apache Software Foundation
15  *
16  * Licensed under the Apache License, Version 2.0 (the "License");
17  * you may not use this file except in compliance with the License.
18  * You may obtain a copy of the License at
19  *
20  * http://www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an "AS IS" BASIS,
24  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  */

28
29 /**
30  * This is a developer test and NOT part of the test suite.
31  * This test help to test the performance of the different
32  * {@link org.apache.ojb.broker.metadata.fieldaccess.PersistentField}
33  * implementations.
34  *
35  * @author <a HREF="mailto:arminw@apache.org">Armin Waibel</a>
36  * @version $Id: PersistentFieldPerfTest.java,v 1.4.2.2 2005/12/21 22:31:24 tomdz Exp $
37  */

38 public class PersistentFieldPerfTest extends OJBTestCase
39 {
40     String JavaDoc EOL = SystemUtils.LINE_SEPARATOR;
41     Class JavaDoc testClass = NestedFieldsTest.NestedMain.class;
42     String JavaDoc fieldName = "name";
43     String JavaDoc fieldNameNested = "nestedDetail::nestedDetailDetail::realDetailName";
44     int numberOfOperations = 30000;
45     int repeat = 5;
46
47     public static void main(String JavaDoc[] args)
48     {
49         String JavaDoc[] arr = {PersistentFieldPerfTest.class.getName()};
50         junit.textui.TestRunner.main(arr);
51     }
52
53     Class JavaDoc[] persistentFieldClasses = new Class JavaDoc[]{
54         PersistentFieldDirectImpl.class
55         , PersistentFieldIntrospectorImpl.class
56         , PersistentFieldPrivilegedImpl.class
57         , PersistentFieldAutoProxyImpl.class};
58
59
60     private PersistentField newInstance(Class JavaDoc pfClass, Class JavaDoc testClass, String JavaDoc fieldName) throws Exception JavaDoc
61     {
62         Class JavaDoc[] types = new Class JavaDoc[]{Class JavaDoc.class, String JavaDoc.class};
63         Object JavaDoc[] args = new Object JavaDoc[]{testClass, fieldName};
64         return (PersistentField) ClassHelper.newInstance(pfClass, types, args);
65     }
66
67     public void testFieldPerformance() throws Exception JavaDoc
68     {
69         System.out.println();
70         System.out.println("=========================================");
71         System.out.println("Field performance, set/get " + numberOfOperations + " times a field");
72         System.out.println("----------------------------------------");
73         for (int i = 0; i < persistentFieldClasses.length; i++)
74         {
75             Class JavaDoc persistentFieldClass = persistentFieldClasses[i];
76             PersistentField p = newInstance(persistentFieldClass, testClass, fieldName);
77             buildTestFor(p, false);
78         }
79         System.out.println("----------------------------------------");
80         for (int i = 0; i < persistentFieldClasses.length; i++)
81         {
82             Class JavaDoc persistentFieldClass = persistentFieldClasses[i];
83             PersistentField p = newInstance(persistentFieldClass, testClass, fieldName);
84             buildTestFor(p, false);
85         }
86         System.out.println("----------------------------------------");
87     }
88
89     public void testNestedFieldPerformance() throws Exception JavaDoc
90     {
91         System.out.println();
92         System.out.println("=========================================");
93         System.out.println("Nested Field performance, set/get " + numberOfOperations + " times a nested field");
94         System.out.println("----------------------------------------");
95         for (int i = 0; i < persistentFieldClasses.length; i++)
96         {
97             Class JavaDoc persistentFieldClass = persistentFieldClasses[i];
98             PersistentField p = newInstance(persistentFieldClass, testClass, fieldNameNested);
99             buildTestFor(p, true);
100         }
101         System.out.println("----------------------------------------");
102         for (int i = 0; i < persistentFieldClasses.length; i++)
103         {
104             Class JavaDoc persistentFieldClass = persistentFieldClasses[i];
105             PersistentField p = newInstance(persistentFieldClass, testClass, fieldNameNested);
106             buildTestFor(p, true);
107         }
108         System.out.println("----------------------------------------");
109     }
110
111     private void buildTestFor(PersistentField pf, boolean nested) throws Exception JavaDoc
112     {
113         long getter = 0;
114         long setter = 0;
115         for (int i = 0; i < repeat; i++)
116         {
117             System.gc();
118             Thread.sleep(100);
119             getter += nested ? getterPerformanceNestedFor(pf) : getterPerformanceFor(pf);
120         }
121         for (int i = 0; i < repeat; i++)
122         {
123             System.gc();
124             Thread.sleep(100);
125             setter += nested ? setterPerformanceNestedFor(pf) : setterPerformanceFor(pf);
126         }
127         printResult(pf, getter, setter, nested);
128     }
129
130     private void printResult(PersistentField pf, long getterPeriod, long setterPeriod, boolean nested)
131     {
132
133         System.out.println(ClassUtils.getShortClassName(pf.getClass())
134                 + (nested ? ": nestedGetter=" : ": getter=") + getterPeriod
135                 + (nested ? " nestedSetter=" : " setter=") + setterPeriod);
136     }
137
138     private long getterPerformanceFor(PersistentField pf)
139     {
140         String JavaDoc testString = "a test name";
141         NestedFieldsTest.NestedMain testObject = new NestedFieldsTest.NestedMain();
142         testObject.setName(testString);
143         // validate
144
assertEquals(testString, pf.get(testObject));
145
146         long period = System.currentTimeMillis();
147         for (int i = 0; i < numberOfOperations; i++)
148         {
149             pf.get(testObject);
150         }
151         return System.currentTimeMillis() - period;
152     }
153
154     private long setterPerformanceFor(PersistentField pf)
155     {
156         String JavaDoc testString = "a test name";
157         NestedFieldsTest.NestedMain testObject = new NestedFieldsTest.NestedMain();
158         // validate
159
pf.set(testObject, testString);
160         assertEquals(testString, testObject.getName());
161         long period = System.currentTimeMillis();
162         for (int i = 0; i < numberOfOperations; i++)
163         {
164             pf.set(testObject, testString);
165         }
166         return System.currentTimeMillis() - period;
167     }
168
169     private long getterPerformanceNestedFor(PersistentField pf)
170     {
171         String JavaDoc testString = "a test name";
172         NestedFieldsTest.NestedMain testObject = new NestedFieldsTest.NestedMain();
173         NestedFieldsTest.NestedDetail d1 = new NestedFieldsTest.NestedDetail();
174         NestedFieldsTest.NestedDetailDetail d2 = new NestedFieldsTest.NestedDetailDetail();
175         d2.setRealDetailName(testString);
176         d1.setNestedDetailDetail(d2);
177         testObject.setNestedDetail(d1);
178         // validate
179
assertEquals(testString, pf.get(testObject));
180
181         long period = System.currentTimeMillis();
182         for (int i = 0; i < numberOfOperations; i++)
183         {
184             pf.get(testObject);
185         }
186         return System.currentTimeMillis() - period;
187     }
188
189     private long setterPerformanceNestedFor(PersistentField pf)
190     {
191         String JavaDoc testString = "a test name";
192         NestedFieldsTest.NestedMain testObject = new NestedFieldsTest.NestedMain();
193         // validate
194
pf.set(testObject, testString);
195         assertNotNull(testObject.getNestedDetail());
196         assertNotNull(testObject.getNestedDetail().getNestedDetailDetail());
197         assertEquals(testString, testObject.getNestedDetail().getNestedDetailDetail().getRealDetailName());
198         assertEquals(testString, testObject.getNestedDetail().getNestedDetailDetail().getRealDetailName());
199         long period = System.currentTimeMillis();
200         for (int i = 0; i < numberOfOperations; i++)
201         {
202             pf.set(testObject, testString);
203         }
204         return System.currentTimeMillis() - period;
205     }
206 }
207
Popular Tags