001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.beanutils;
019:
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023: import junit.framework.TestCase;
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: /**
028: * JUnit Test Case containing microbenchmarks for BeanUtils.
029: */
030:
031: public class BeanUtilsBenchCase extends TestCase {
032:
033: // ------------------------------------------------------------ Constructors
034:
035: /**
036: * Construct a new instance of this test case.
037: *
038: * @param name Name of the test case
039: */
040: public BeanUtilsBenchCase(String name) {
041:
042: super (name);
043:
044: }
045:
046: // ------------------------------------------------------ Instance Variables
047:
048: // Basic loop counter
049: private long counter = 100000;
050:
051: // DynaClass for inDyna and outDyna
052: private DynaClass dynaClass = null;
053:
054: // Input objects that have identical sets of properties and values.
055: private BenchBean inBean = null;
056: private DynaBean inDyna = null;
057: private Map inMap = null; // Map of Objects requiring no conversion
058: private Map inStrs = null; // Map of Strings requiring conversion
059:
060: // Output objects that have identical sets of properties.
061: private BenchBean outBean = null;
062: private DynaBean outDyna = null;
063:
064: // BeanUtilsBean instance to be used
065: private BeanUtilsBean bu = null;
066:
067: // ---------------------------------------------------- Overall Test Methods
068:
069: /**
070: * Set up instance variables required by this test case.
071: */
072: public void setUp() throws Exception {
073:
074: // Set up loop counter (if property specified)
075: String prop = System.getProperty("counter");
076: if (prop != null) {
077: counter = Long.parseLong(prop);
078: }
079:
080: // Set up DynaClass for our DynaBean instances
081: dynaClass = new BasicDynaClass(
082: "BenchDynaClass",
083: null,
084: new DynaProperty[] {
085: new DynaProperty("booleanProperty",
086: Boolean.TYPE),
087: new DynaProperty("byteProperty", Byte.TYPE),
088: new DynaProperty("doubleProperty", Double.TYPE),
089: new DynaProperty("floatProperty", Float.TYPE),
090: new DynaProperty("intProperty", Integer.TYPE),
091: new DynaProperty("longProperty", Long.TYPE),
092: new DynaProperty("shortProperty", Short.TYPE),
093: new DynaProperty("stringProperty", String.class), });
094:
095: // Create input instances
096: inBean = new BenchBean();
097: inMap = new HashMap();
098: inMap.put("booleanProperty", new Boolean(inBean
099: .getBooleanProperty()));
100: inMap.put("byteProperty", new Byte(inBean.getByteProperty()));
101: inMap.put("doubleProperty", new Double(inBean
102: .getDoubleProperty()));
103: inMap
104: .put("floatProperty", new Float(inBean
105: .getFloatProperty()));
106: inMap.put("intProperty", new Integer(inBean.getIntProperty()));
107: inMap.put("longProperty", new Long(inBean.getLongProperty()));
108: inMap
109: .put("shortProperty", new Short(inBean
110: .getShortProperty()));
111: inMap.put("stringProperty", inBean.getStringProperty());
112: inDyna = dynaClass.newInstance();
113: Iterator inKeys = inMap.keySet().iterator();
114: while (inKeys.hasNext()) {
115: String inKey = (String) inKeys.next();
116: inDyna.set(inKey, inMap.get(inKey));
117: }
118: inStrs = new HashMap();
119: inKeys = inMap.keySet().iterator();
120: while (inKeys.hasNext()) {
121: String inKey = (String) inKeys.next();
122: inStrs.put(inKey, inMap.get(inKey).toString());
123: }
124:
125: // Create output instances
126: outBean = new BenchBean();
127: outDyna = dynaClass.newInstance();
128: Iterator outKeys = inMap.keySet().iterator();
129: while (outKeys.hasNext()) {
130: String outKey = (String) outKeys.next();
131: outDyna.set(outKey, inMap.get(outKey));
132: }
133:
134: // Set up BeanUtilsBean instance we will use
135: bu = BeanUtilsBean.getInstance();
136:
137: }
138:
139: /**
140: * Return the tests included in this test suite.
141: */
142: public static Test suite() {
143:
144: return (new TestSuite(BeanUtilsBenchCase.class));
145:
146: }
147:
148: /**
149: * Tear down instance variables required by this test case.
150: */
151: public void tearDown() {
152:
153: dynaClass = null;
154: inBean = null;
155: inDyna = null;
156: inMap = null;
157: outBean = null;
158: outDyna = null;
159: bu = null;
160:
161: }
162:
163: // ------------------------------------------------- Individual Test Methods
164:
165: // Time copyProperties() from a bean
166: public void testCopyPropertiesBean() throws Exception {
167:
168: long start;
169: long stop;
170:
171: // Bean->Bean
172: for (long i = 0; i < counter; i++) {
173: bu.copyProperties(outBean, inBean);
174: }
175: start = System.currentTimeMillis();
176: for (long i = 0; i < counter; i++) {
177: bu.copyProperties(outBean, inBean);
178: }
179: stop = System.currentTimeMillis();
180: System.err.println("BU.copyProperties(bean,bean), count="
181: + counter + ", time=" + (stop - start));
182:
183: // Bean->Dyna
184: for (long i = 0; i < counter; i++) {
185: bu.copyProperties(outDyna, inBean);
186: }
187: start = System.currentTimeMillis();
188: for (long i = 0; i < counter; i++) {
189: bu.copyProperties(outDyna, inBean);
190: }
191: stop = System.currentTimeMillis();
192: System.err.println("BU.copyProperties(dyna,bean), count="
193: + counter + ", time=" + (stop - start));
194:
195: }
196:
197: // Time copyProperties() from a DynaBean
198: public void testCopyPropertiesDyna() throws Exception {
199:
200: long start;
201: long stop;
202:
203: // Dyna->Bean
204: for (long i = 0; i < counter; i++) {
205: bu.copyProperties(outBean, inDyna);
206: }
207: start = System.currentTimeMillis();
208: for (long i = 0; i < counter; i++) {
209: bu.copyProperties(outBean, inDyna);
210: }
211: stop = System.currentTimeMillis();
212: System.err.println("BU.copyProperties(bean,dyna), count="
213: + counter + ", time=" + (stop - start));
214:
215: // Dyna->Dyna
216: for (long i = 0; i < counter; i++) {
217: bu.copyProperties(outDyna, inDyna);
218: }
219: start = System.currentTimeMillis();
220: for (long i = 0; i < counter; i++) {
221: bu.copyProperties(outDyna, inDyna);
222: }
223: stop = System.currentTimeMillis();
224: System.err.println("BU.copyProperties(dyna,dyna), count="
225: + counter + ", time=" + (stop - start));
226:
227: }
228:
229: // Time copyProperties() from a Map of Objects
230: public void testCopyPropertiesMap() throws Exception {
231:
232: long start;
233: long stop;
234:
235: // Map->Bean
236: for (long i = 0; i < counter; i++) {
237: bu.copyProperties(outBean, inMap);
238: }
239: start = System.currentTimeMillis();
240: for (long i = 0; i < counter; i++) {
241: bu.copyProperties(outBean, inMap);
242: }
243: stop = System.currentTimeMillis();
244: System.err.println("BU.copyProperties(bean, map), count="
245: + counter + ", time=" + (stop - start));
246:
247: // Map->Dyna
248: for (long i = 0; i < counter; i++) {
249: bu.copyProperties(outDyna, inMap);
250: }
251: start = System.currentTimeMillis();
252: for (long i = 0; i < counter; i++) {
253: bu.copyProperties(outDyna, inMap);
254: }
255: stop = System.currentTimeMillis();
256: System.err.println("BU.copyProperties(dyna, map), count="
257: + counter + ", time=" + (stop - start));
258:
259: }
260:
261: // Time copyProperties() from a Map of Strings
262: public void testCopyPropertiesStrs() throws Exception {
263:
264: long start;
265: long stop;
266:
267: // Strs->Bean
268: for (long i = 0; i < counter; i++) {
269: bu.copyProperties(outBean, inStrs);
270: }
271: start = System.currentTimeMillis();
272: for (long i = 0; i < counter; i++) {
273: bu.copyProperties(outBean, inStrs);
274: }
275: stop = System.currentTimeMillis();
276: System.err.println("BU.copyProperties(bean,strs), count="
277: + counter + ", time=" + (stop - start));
278:
279: // Strs->Dyna
280: for (long i = 0; i < counter; i++) {
281: bu.copyProperties(outDyna, inStrs);
282: }
283: start = System.currentTimeMillis();
284: for (long i = 0; i < counter; i++) {
285: bu.copyProperties(outDyna, inStrs);
286: }
287: stop = System.currentTimeMillis();
288: System.err.println("BU.copyProperties(dyna,strs), count="
289: + counter + ", time=" + (stop - start));
290:
291: }
292:
293: // Time populate() from a Map of Objects
294: public void testPopulateMap() throws Exception {
295:
296: long start;
297: long stop;
298:
299: // Map->Bean
300: for (long i = 0; i < counter; i++) {
301: bu.populate(outBean, inMap);
302: }
303: start = System.currentTimeMillis();
304: for (long i = 0; i < counter; i++) {
305: bu.populate(outBean, inMap);
306: }
307: stop = System.currentTimeMillis();
308: System.err.println("BU.populate(bean, map), count=" + counter
309: + ", time=" + (stop - start));
310:
311: // Map->Dyna
312: for (long i = 0; i < counter; i++) {
313: bu.populate(outDyna, inMap);
314: }
315: start = System.currentTimeMillis();
316: for (long i = 0; i < counter; i++) {
317: bu.populate(outDyna, inMap);
318: }
319: stop = System.currentTimeMillis();
320: System.err.println("BU.populate(dyna, map), count=" + counter
321: + ", time=" + (stop - start));
322:
323: }
324:
325: // Time populate() from a Map of Strings
326: // NOTE - This simulates what Struts does when processing form beans
327: public void testPopulateStrs() throws Exception {
328:
329: long start;
330: long stop;
331:
332: // Strs->Bean
333: for (long i = 0; i < counter; i++) {
334: bu.populate(outBean, inStrs);
335: }
336: start = System.currentTimeMillis();
337: for (long i = 0; i < counter; i++) {
338: bu.populate(outBean, inStrs);
339: }
340: stop = System.currentTimeMillis();
341: System.err.println("BU.populate(bean,strs), count=" + counter
342: + ", time=" + (stop - start));
343:
344: // Strs->Dyna
345: for (long i = 0; i < counter; i++) {
346: bu.populate(outDyna, inStrs);
347: }
348: start = System.currentTimeMillis();
349: for (long i = 0; i < counter; i++) {
350: bu.populate(outDyna, inStrs);
351: }
352: stop = System.currentTimeMillis();
353: System.err.println("BU.populate(dyna,strs), count=" + counter
354: + ", time=" + (stop - start));
355:
356: }
357:
358: // --------------------------------------------------------- Support Methods
359:
360: }
|