1 16 17 18 package org.apache.commons.beanutils; 19 20 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import junit.framework.TestCase; 25 import junit.framework.Test; 26 import junit.framework.TestSuite; 27 28 29 32 33 public class BeanUtilsBenchCase extends TestCase { 34 35 36 38 39 44 public BeanUtilsBenchCase(String name) { 45 46 super(name); 47 48 } 49 50 51 53 54 private long counter = 100000; 56 57 private DynaClass dynaClass = null; 59 60 private BenchBean inBean = null; 62 private DynaBean inDyna = null; 63 private Map inMap = null; private Map inStrs = null; 66 private BenchBean outBean = null; 68 private DynaBean outDyna = null; 69 70 private BeanUtilsBean bu = null; 72 73 74 76 77 80 public void setUp() throws Exception { 81 82 String prop = System.getProperty("counter"); 84 if (prop != null) { 85 counter = Long.parseLong(prop); 86 } 87 88 dynaClass = new BasicDynaClass 90 ("BenchDynaClass", null, 91 new DynaProperty[]{ 92 new DynaProperty("booleanProperty", Boolean.TYPE), 93 new DynaProperty("byteProperty", Byte.TYPE), 94 new DynaProperty("doubleProperty", Double.TYPE), 95 new DynaProperty("floatProperty", Float.TYPE), 96 new DynaProperty("intProperty", Integer.TYPE), 97 new DynaProperty("longProperty", Long.TYPE), 98 new DynaProperty("shortProperty", Short.TYPE), 99 new DynaProperty("stringProperty", String .class), 100 }); 101 102 inBean = new BenchBean(); 104 inMap = new HashMap (); 105 inMap.put("booleanProperty", new Boolean (inBean.getBooleanProperty())); 106 inMap.put("byteProperty", new Byte (inBean.getByteProperty())); 107 inMap.put("doubleProperty", new Double (inBean.getDoubleProperty())); 108 inMap.put("floatProperty", new Float (inBean.getFloatProperty())); 109 inMap.put("intProperty", new Integer (inBean.getIntProperty())); 110 inMap.put("longProperty", new Long (inBean.getLongProperty())); 111 inMap.put("shortProperty", new Short (inBean.getShortProperty())); 112 inMap.put("stringProperty", inBean.getStringProperty()); 113 inDyna = dynaClass.newInstance(); 114 Iterator inKeys = inMap.keySet().iterator(); 115 while (inKeys.hasNext()) { 116 String inKey = (String ) inKeys.next(); 117 inDyna.set(inKey, inMap.get(inKey)); 118 } 119 inStrs = new HashMap (); 120 inKeys = inMap.keySet().iterator(); 121 while (inKeys.hasNext()) { 122 String inKey = (String ) inKeys.next(); 123 inStrs.put(inKey, inMap.get(inKey).toString()); 124 } 125 126 outBean = new BenchBean(); 128 outDyna = dynaClass.newInstance(); 129 Iterator outKeys = inMap.keySet().iterator(); 130 while (outKeys.hasNext()) { 131 String outKey = (String ) outKeys.next(); 132 outDyna.set(outKey, inMap.get(outKey)); 133 } 134 135 bu = BeanUtilsBean.getInstance(); 137 138 } 139 140 141 144 public static Test suite() { 145 146 return (new TestSuite(BeanUtilsBenchCase.class)); 147 148 } 149 150 151 154 public void tearDown() { 155 156 dynaClass = null; 157 inBean = null; 158 inDyna = null; 159 inMap = null; 160 outBean = null; 161 outDyna = null; 162 bu = null; 163 164 } 165 166 167 168 170 171 public void testCopyPropertiesBean() throws Exception { 173 174 long start; 175 long stop; 176 177 for (long i = 0; i < counter; i++) { 179 bu.copyProperties(outBean, inBean); 180 } 181 start = System.currentTimeMillis(); 182 for (long i = 0; i < counter; i++) { 183 bu.copyProperties(outBean, inBean); 184 } 185 stop = System.currentTimeMillis(); 186 System.err.println("BU.copyProperties(bean,bean), count=" + counter + 187 ", time=" + (stop - start)); 188 189 for (long i = 0; i < counter; i++) { 191 bu.copyProperties(outDyna, inBean); 192 } 193 start = System.currentTimeMillis(); 194 for (long i = 0; i < counter; i++) { 195 bu.copyProperties(outDyna, inBean); 196 } 197 stop = System.currentTimeMillis(); 198 System.err.println("BU.copyProperties(dyna,bean), count=" + counter + 199 ", time=" + (stop - start)); 200 201 } 202 203 204 public void testCopyPropertiesDyna() throws Exception { 206 207 long start; 208 long stop; 209 210 for (long i = 0; i < counter; i++) { 212 bu.copyProperties(outBean, inDyna); 213 } 214 start = System.currentTimeMillis(); 215 for (long i = 0; i < counter; i++) { 216 bu.copyProperties(outBean, inDyna); 217 } 218 stop = System.currentTimeMillis(); 219 System.err.println("BU.copyProperties(bean,dyna), count=" + counter + 220 ", time=" + (stop - start)); 221 222 for (long i = 0; i < counter; i++) { 224 bu.copyProperties(outDyna, inDyna); 225 } 226 start = System.currentTimeMillis(); 227 for (long i = 0; i < counter; i++) { 228 bu.copyProperties(outDyna, inDyna); 229 } 230 stop = System.currentTimeMillis(); 231 System.err.println("BU.copyProperties(dyna,dyna), count=" + counter + 232 ", time=" + (stop - start)); 233 234 } 235 236 237 public void testCopyPropertiesMap() throws Exception { 239 240 long start; 241 long stop; 242 243 for (long i = 0; i < counter; i++) { 245 bu.copyProperties(outBean, inMap); 246 } 247 start = System.currentTimeMillis(); 248 for (long i = 0; i < counter; i++) { 249 bu.copyProperties(outBean, inMap); 250 } 251 stop = System.currentTimeMillis(); 252 System.err.println("BU.copyProperties(bean, map), count=" + counter + 253 ", time=" + (stop - start)); 254 255 for (long i = 0; i < counter; i++) { 257 bu.copyProperties(outDyna, inMap); 258 } 259 start = System.currentTimeMillis(); 260 for (long i = 0; i < counter; i++) { 261 bu.copyProperties(outDyna, inMap); 262 } 263 stop = System.currentTimeMillis(); 264 System.err.println("BU.copyProperties(dyna, map), count=" + counter + 265 ", time=" + (stop - start)); 266 267 } 268 269 270 public void testCopyPropertiesStrs() throws Exception { 272 273 long start; 274 long stop; 275 276 for (long i = 0; i < counter; i++) { 278 bu.copyProperties(outBean, inStrs); 279 } 280 start = System.currentTimeMillis(); 281 for (long i = 0; i < counter; i++) { 282 bu.copyProperties(outBean, inStrs); 283 } 284 stop = System.currentTimeMillis(); 285 System.err.println("BU.copyProperties(bean,strs), count=" + counter + 286 ", time=" + (stop - start)); 287 288 for (long i = 0; i < counter; i++) { 290 bu.copyProperties(outDyna, inStrs); 291 } 292 start = System.currentTimeMillis(); 293 for (long i = 0; i < counter; i++) { 294 bu.copyProperties(outDyna, inStrs); 295 } 296 stop = System.currentTimeMillis(); 297 System.err.println("BU.copyProperties(dyna,strs), count=" + counter + 298 ", time=" + (stop - start)); 299 300 } 301 302 303 public void testPopulateMap() throws Exception { 305 306 long start; 307 long stop; 308 309 for (long i = 0; i < counter; i++) { 311 bu.populate(outBean, inMap); 312 } 313 start = System.currentTimeMillis(); 314 for (long i = 0; i < counter; i++) { 315 bu.populate(outBean, inMap); 316 } 317 stop = System.currentTimeMillis(); 318 System.err.println("BU.populate(bean, map), count=" + counter + 319 ", time=" + (stop - start)); 320 321 for (long i = 0; i < counter; i++) { 323 bu.populate(outDyna, inMap); 324 } 325 start = System.currentTimeMillis(); 326 for (long i = 0; i < counter; i++) { 327 bu.populate(outDyna, inMap); 328 } 329 stop = System.currentTimeMillis(); 330 System.err.println("BU.populate(dyna, map), count=" + counter + 331 ", time=" + (stop - start)); 332 333 } 334 335 336 public void testPopulateStrs() throws Exception { 339 340 long start; 341 long stop; 342 343 for (long i = 0; i < counter; i++) { 345 bu.populate(outBean, inStrs); 346 } 347 start = System.currentTimeMillis(); 348 for (long i = 0; i < counter; i++) { 349 bu.populate(outBean, inStrs); 350 } 351 stop = System.currentTimeMillis(); 352 System.err.println("BU.populate(bean,strs), count=" + counter + 353 ", time=" + (stop - start)); 354 355 for (long i = 0; i < counter; i++) { 357 bu.populate(outDyna, inStrs); 358 } 359 start = System.currentTimeMillis(); 360 for (long i = 0; i < counter; i++) { 361 bu.populate(outDyna, inStrs); 362 } 363 stop = System.currentTimeMillis(); 364 System.err.println("BU.populate(dyna,strs), count=" + counter + 365 ", time=" + (stop - start)); 366 367 } 368 369 370 372 373 } 374 | Popular Tags |