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 PropertyUtils.
029: */
030:
031: public class PropertyUtilsBenchCase 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 PropertyUtilsBenchCase(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;
058:
059: // Output objects that have identical sets of properties.
060: private BenchBean outBean = null;
061: private DynaBean outDyna = null;
062:
063: // PropertyUtilsBean instance to be used
064: private PropertyUtilsBean pu = null;
065:
066: // ---------------------------------------------------- Overall Test Methods
067:
068: /**
069: * Set up instance variables required by this test case.
070: */
071: public void setUp() throws Exception {
072:
073: // Set up loop counter (if property specified)
074: String prop = System.getProperty("counter");
075: if (prop != null) {
076: counter = Long.parseLong(prop);
077: }
078:
079: // Set up DynaClass for our DynaBean instances
080: dynaClass = new BasicDynaClass(
081: "BenchDynaClass",
082: null,
083: new DynaProperty[] {
084: new DynaProperty("booleanProperty",
085: Boolean.TYPE),
086: new DynaProperty("byteProperty", Byte.TYPE),
087: new DynaProperty("doubleProperty", Double.TYPE),
088: new DynaProperty("floatProperty", Float.TYPE),
089: new DynaProperty("intProperty", Integer.TYPE),
090: new DynaProperty("longProperty", Long.TYPE),
091: new DynaProperty("shortProperty", Short.TYPE),
092: new DynaProperty("stringProperty", String.class), });
093:
094: // Create input instances
095: inBean = new BenchBean();
096: inMap = new HashMap();
097: inMap.put("booleanProperty", new Boolean(inBean
098: .getBooleanProperty()));
099: inMap.put("byteProperty", new Byte(inBean.getByteProperty()));
100: inMap.put("doubleProperty", new Double(inBean
101: .getDoubleProperty()));
102: inMap
103: .put("floatProperty", new Float(inBean
104: .getFloatProperty()));
105: inMap.put("intProperty", new Integer(inBean.getIntProperty()));
106: inMap.put("longProperty", new Long(inBean.getLongProperty()));
107: inMap
108: .put("shortProperty", new Short(inBean
109: .getShortProperty()));
110: inMap.put("stringProperty", inBean.getStringProperty());
111: inDyna = dynaClass.newInstance();
112: Iterator inKeys = inMap.keySet().iterator();
113: while (inKeys.hasNext()) {
114: String inKey = (String) inKeys.next();
115: inDyna.set(inKey, inMap.get(inKey));
116: }
117:
118: // Create output instances
119: outBean = new BenchBean();
120: outDyna = dynaClass.newInstance();
121: Iterator outKeys = inMap.keySet().iterator();
122: while (outKeys.hasNext()) {
123: String outKey = (String) outKeys.next();
124: outDyna.set(outKey, inMap.get(outKey));
125: }
126:
127: // Set up PropertyUtilsBean instance we will use
128: pu = PropertyUtilsBean.getInstance();
129:
130: }
131:
132: /**
133: * Return the tests included in this test suite.
134: */
135: public static Test suite() {
136:
137: return (new TestSuite(PropertyUtilsBenchCase.class));
138:
139: }
140:
141: /**
142: * Tear down instance variables required by this test case.
143: */
144: public void tearDown() {
145:
146: dynaClass = null;
147: inBean = null;
148: inDyna = null;
149: inMap = null;
150: outBean = null;
151: outDyna = null;
152: pu = null;
153:
154: }
155:
156: // ------------------------------------------------- Individual Test Methods
157:
158: // Time copyProperties() from a bean
159: public void testCopyPropertiesBean() throws Exception {
160:
161: long start;
162: long stop;
163:
164: // Bean->Bean
165: for (long i = 0; i < counter; i++) {
166: pu.copyProperties(outBean, inBean);
167: }
168: start = System.currentTimeMillis();
169: for (long i = 0; i < counter; i++) {
170: pu.copyProperties(outBean, inBean);
171: }
172: stop = System.currentTimeMillis();
173: System.err.println("PU.copyProperties(bean,bean), count="
174: + counter + ", time=" + (stop - start));
175:
176: // Bean->Dyna
177: for (long i = 0; i < counter; i++) {
178: pu.copyProperties(outDyna, inBean);
179: }
180: start = System.currentTimeMillis();
181: for (long i = 0; i < counter; i++) {
182: pu.copyProperties(outDyna, inBean);
183: }
184: stop = System.currentTimeMillis();
185: System.err.println("PU.copyProperties(dyna,bean), count="
186: + counter + ", time=" + (stop - start));
187:
188: }
189:
190: // Time copyProperties() from a DynaBean
191: public void testCopyPropertiesDyna() throws Exception {
192:
193: long start;
194: long stop;
195:
196: // Dyna->Bean
197: for (long i = 0; i < counter; i++) {
198: pu.copyProperties(outBean, inDyna);
199: }
200: start = System.currentTimeMillis();
201: for (long i = 0; i < counter; i++) {
202: pu.copyProperties(outBean, inDyna);
203: }
204: stop = System.currentTimeMillis();
205: System.err.println("PU.copyProperties(bean,dyna), count="
206: + counter + ", time=" + (stop - start));
207:
208: // Dyna->Dyna
209: for (long i = 0; i < counter; i++) {
210: pu.copyProperties(outDyna, inDyna);
211: }
212: start = System.currentTimeMillis();
213: for (long i = 0; i < counter; i++) {
214: pu.copyProperties(outDyna, inDyna);
215: }
216: stop = System.currentTimeMillis();
217: System.err.println("PU.copyProperties(dyna,dyna), count="
218: + counter + ", time=" + (stop - start));
219:
220: }
221:
222: // Time copyProperties() from a Map
223: public void testCopyPropertiesMap() throws Exception {
224:
225: long start;
226: long stop;
227:
228: // Dyna->Bean
229: for (long i = 0; i < counter; i++) {
230: pu.copyProperties(outBean, inMap);
231: }
232: start = System.currentTimeMillis();
233: for (long i = 0; i < counter; i++) {
234: pu.copyProperties(outBean, inMap);
235: }
236: stop = System.currentTimeMillis();
237: System.err.println("PU.copyProperties(bean, map), count="
238: + counter + ", time=" + (stop - start));
239:
240: // Dyna->Dyna
241: for (long i = 0; i < counter; i++) {
242: pu.copyProperties(outDyna, inMap);
243: }
244: start = System.currentTimeMillis();
245: for (long i = 0; i < counter; i++) {
246: pu.copyProperties(outDyna, inMap);
247: }
248: stop = System.currentTimeMillis();
249: System.err.println("PU.copyProperties(dyna, map), count="
250: + counter + ", time=" + (stop - start));
251:
252: }
253:
254: // --------------------------------------------------------- Support Methods
255:
256: }
|