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
019 package org.apache.commons.beanutils;
020
021
022 import java.util.HashMap;
023 import java.util.Iterator;
024 import java.util.Map;
025 import junit.framework.TestCase;
026 import junit.framework.Test;
027 import junit.framework.TestSuite;
028
029
030 /**
031 * JUnit Test Case containing microbenchmarks for PropertyUtils.
032 */
033
034 public class PropertyUtilsBenchCase extends TestCase {
035
036
037 // ------------------------------------------------------------ Constructors
038
039
040 /**
041 * Construct a new instance of this test case.
042 *
043 * @param name Name of the test case
044 */
045 public PropertyUtilsBenchCase(String name) {
046
047 super(name);
048
049 }
050
051
052 // ------------------------------------------------------ Instance Variables
053
054
055 // Basic loop counter
056 private long counter = 100000;
057
058 // DynaClass for inDyna and outDyna
059 private DynaClass dynaClass = null;
060
061 // Input objects that have identical sets of properties and values.
062 private BenchBean inBean = null;
063 private DynaBean inDyna = null;
064 private Map inMap = null;
065
066 // Output objects that have identical sets of properties.
067 private BenchBean outBean = null;
068 private DynaBean outDyna = null;
069
070 // PropertyUtilsBean instance to be used
071 private PropertyUtilsBean pu = null;
072
073
074 // ---------------------------------------------------- Overall Test Methods
075
076
077 /**
078 * Set up instance variables required by this test case.
079 */
080 public void setUp() throws Exception {
081
082 // Set up loop counter (if property specified)
083 String prop = System.getProperty("counter");
084 if (prop != null) {
085 counter = Long.parseLong(prop);
086 }
087
088 // Set up DynaClass for our DynaBean instances
089 dynaClass = new BasicDynaClass
090 ("BenchDynaClass", null,
091 new DynaProperty[]{
092 new DynaProperty("booleanProperty", Boolean.TYPE),
093 new DynaProperty("byteProperty", Byte.TYPE),
094 new DynaProperty("doubleProperty", Double.TYPE),
095 new DynaProperty("floatProperty", Float.TYPE),
096 new DynaProperty("intProperty", Integer.TYPE),
097 new DynaProperty("longProperty", Long.TYPE),
098 new DynaProperty("shortProperty", Short.TYPE),
099 new DynaProperty("stringProperty", String.class),
100 });
101
102 // Create input instances
103 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
120 // Create output instances
121 outBean = new BenchBean();
122 outDyna = dynaClass.newInstance();
123 Iterator outKeys = inMap.keySet().iterator();
124 while (outKeys.hasNext()) {
125 String outKey = (String) outKeys.next();
126 outDyna.set(outKey, inMap.get(outKey));
127 }
128
129 // Set up PropertyUtilsBean instance we will use
130 pu = PropertyUtilsBean.getInstance();
131
132 }
133
134
135 /**
136 * Return the tests included in this test suite.
137 */
138 public static Test suite() {
139
140 return (new TestSuite(PropertyUtilsBenchCase.class));
141
142 }
143
144
145 /**
146 * Tear down instance variables required by this test case.
147 */
148 public void tearDown() {
149
150 dynaClass = null;
151 inBean = null;
152 inDyna = null;
153 inMap = null;
154 outBean = null;
155 outDyna = null;
156 pu = null;
157
158 }
159
160
161
162 // ------------------------------------------------- Individual Test Methods
163
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 pu.copyProperties(outBean, inBean);
174 }
175 start = System.currentTimeMillis();
176 for (long i = 0; i < counter; i++) {
177 pu.copyProperties(outBean, inBean);
178 }
179 stop = System.currentTimeMillis();
180 System.err.println("PU.copyProperties(bean,bean), count=" + counter +
181 ", time=" + (stop - start));
182
183 // Bean->Dyna
184 for (long i = 0; i < counter; i++) {
185 pu.copyProperties(outDyna, inBean);
186 }
187 start = System.currentTimeMillis();
188 for (long i = 0; i < counter; i++) {
189 pu.copyProperties(outDyna, inBean);
190 }
191 stop = System.currentTimeMillis();
192 System.err.println("PU.copyProperties(dyna,bean), count=" + counter +
193 ", time=" + (stop - start));
194
195 }
196
197
198 // Time copyProperties() from a DynaBean
199 public void testCopyPropertiesDyna() throws Exception {
200
201 long start;
202 long stop;
203
204 // Dyna->Bean
205 for (long i = 0; i < counter; i++) {
206 pu.copyProperties(outBean, inDyna);
207 }
208 start = System.currentTimeMillis();
209 for (long i = 0; i < counter; i++) {
210 pu.copyProperties(outBean, inDyna);
211 }
212 stop = System.currentTimeMillis();
213 System.err.println("PU.copyProperties(bean,dyna), count=" + counter +
214 ", time=" + (stop - start));
215
216 // Dyna->Dyna
217 for (long i = 0; i < counter; i++) {
218 pu.copyProperties(outDyna, inDyna);
219 }
220 start = System.currentTimeMillis();
221 for (long i = 0; i < counter; i++) {
222 pu.copyProperties(outDyna, inDyna);
223 }
224 stop = System.currentTimeMillis();
225 System.err.println("PU.copyProperties(dyna,dyna), count=" + counter +
226 ", time=" + (stop - start));
227
228 }
229
230
231 // Time copyProperties() from a Map
232 public void testCopyPropertiesMap() throws Exception {
233
234 long start;
235 long stop;
236
237 // Dyna->Bean
238 for (long i = 0; i < counter; i++) {
239 pu.copyProperties(outBean, inMap);
240 }
241 start = System.currentTimeMillis();
242 for (long i = 0; i < counter; i++) {
243 pu.copyProperties(outBean, inMap);
244 }
245 stop = System.currentTimeMillis();
246 System.err.println("PU.copyProperties(bean, map), count=" + counter +
247 ", time=" + (stop - start));
248
249 // Dyna->Dyna
250 for (long i = 0; i < counter; i++) {
251 pu.copyProperties(outDyna, inMap);
252 }
253 start = System.currentTimeMillis();
254 for (long i = 0; i < counter; i++) {
255 pu.copyProperties(outDyna, inMap);
256 }
257 stop = System.currentTimeMillis();
258 System.err.println("PU.copyProperties(dyna, map), count=" + counter +
259 ", time=" + (stop - start));
260
261 }
262
263
264 // --------------------------------------------------------- Support Methods
265
266
267 }