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 28 29 38 public class PersistentFieldPerfTest extends OJBTestCase 39 { 40 String EOL = SystemUtils.LINE_SEPARATOR; 41 Class testClass = NestedFieldsTest.NestedMain.class; 42 String fieldName = "name"; 43 String fieldNameNested = "nestedDetail::nestedDetailDetail::realDetailName"; 44 int numberOfOperations = 30000; 45 int repeat = 5; 46 47 public static void main(String [] args) 48 { 49 String [] arr = {PersistentFieldPerfTest.class.getName()}; 50 junit.textui.TestRunner.main(arr); 51 } 52 53 Class [] persistentFieldClasses = new Class []{ 54 PersistentFieldDirectImpl.class 55 , PersistentFieldIntrospectorImpl.class 56 , PersistentFieldPrivilegedImpl.class 57 , PersistentFieldAutoProxyImpl.class}; 58 59 60 private PersistentField newInstance(Class pfClass, Class testClass, String fieldName) throws Exception 61 { 62 Class [] types = new Class []{Class .class, String .class}; 63 Object [] args = new Object []{testClass, fieldName}; 64 return (PersistentField) ClassHelper.newInstance(pfClass, types, args); 65 } 66 67 public void testFieldPerformance() throws Exception 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 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 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 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 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 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 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 testString = "a test name"; 141 NestedFieldsTest.NestedMain testObject = new NestedFieldsTest.NestedMain(); 142 testObject.setName(testString); 143 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 testString = "a test name"; 157 NestedFieldsTest.NestedMain testObject = new NestedFieldsTest.NestedMain(); 158 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 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 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 testString = "a test name"; 192 NestedFieldsTest.NestedMain testObject = new NestedFieldsTest.NestedMain(); 193 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 |