View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  
19  package org.apache.commons.beanutils;
20  
21  
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  
30  
31  /**
32   * JUnit Test Case containing microbenchmarks for BeanUtils.
33   *
34   * @version $Id$
35   */
36  
37  public class BeanUtilsBenchCase extends TestCase {
38  
39  
40      // ------------------------------------------------------------ Constructors
41  
42  
43      /**
44       * Construct a new instance of this test case.
45       *
46       * @param name Name of the test case
47       */
48      public BeanUtilsBenchCase(final String name) {
49  
50          super(name);
51  
52      }
53  
54  
55      // ------------------------------------------------------ Instance Variables
56  
57  
58      // Basic loop counter
59      private long counter = 100000;
60  
61      // DynaClass for inDyna and outDyna
62      private DynaClass dynaClass = null;
63  
64      // Input objects that have identical sets of properties and values.
65      private BenchBean inBean = null;
66      private DynaBean inDyna = null;
67      private Map<String, Object> inMap = null;  // Map of Objects requiring no conversion
68      private Map<String, String> inStrs = null; // Map of Strings requiring conversion
69  
70      // Output objects that have identical sets of properties.
71      private BenchBean outBean = null;
72      private DynaBean outDyna = null;
73  
74      // BeanUtilsBean instance to be used
75      private BeanUtilsBean bu = null;
76  
77  
78      // ---------------------------------------------------- Overall Test Methods
79  
80  
81      /**
82       * Set up instance variables required by this test case.
83       */
84      @Override
85      public void setUp() throws Exception {
86  
87          // Set up loop counter (if property specified)
88          final String prop = System.getProperty("counter");
89          if (prop != null) {
90              counter = Long.parseLong(prop);
91          }
92  
93          // Set up DynaClass for our DynaBean instances
94          dynaClass = new BasicDynaClass
95              ("BenchDynaClass", null,
96               new DynaProperty[]{
97                   new DynaProperty("booleanProperty", Boolean.TYPE),
98                   new DynaProperty("byteProperty", Byte.TYPE),
99                   new DynaProperty("doubleProperty", Double.TYPE),
100                  new DynaProperty("floatProperty", Float.TYPE),
101                  new DynaProperty("intProperty", Integer.TYPE),
102                  new DynaProperty("longProperty", Long.TYPE),
103                  new DynaProperty("shortProperty", Short.TYPE),
104                  new DynaProperty("stringProperty", String.class),
105              });
106 
107         // Create input instances
108         inBean = new BenchBean();
109         inMap = new HashMap<String, Object>();
110         inMap.put("booleanProperty", new Boolean(inBean.getBooleanProperty()));
111         inMap.put("byteProperty", new Byte(inBean.getByteProperty()));
112         inMap.put("doubleProperty", new Double(inBean.getDoubleProperty()));
113         inMap.put("floatProperty", new Float(inBean.getFloatProperty()));
114         inMap.put("intProperty", new Integer(inBean.getIntProperty()));
115         inMap.put("longProperty", new Long(inBean.getLongProperty()));
116         inMap.put("shortProperty", new Short(inBean.getShortProperty()));
117         inMap.put("stringProperty", inBean.getStringProperty());
118         inDyna = dynaClass.newInstance();
119         Iterator<String> inKeys = inMap.keySet().iterator();
120         while (inKeys.hasNext()) {
121             final String inKey = inKeys.next();
122             inDyna.set(inKey, inMap.get(inKey));
123         }
124         inStrs = new HashMap<String, String>();
125         inKeys = inMap.keySet().iterator();
126         while (inKeys.hasNext()) {
127             final String inKey = inKeys.next();
128             inStrs.put(inKey, inMap.get(inKey).toString());
129         }
130 
131         // Create output instances
132         outBean = new BenchBean();
133         outDyna = dynaClass.newInstance();
134         final Iterator<String> outKeys = inMap.keySet().iterator();
135         while (outKeys.hasNext()) {
136             final String outKey = outKeys.next();
137             outDyna.set(outKey, inMap.get(outKey));
138         }
139 
140         // Set up BeanUtilsBean instance we will use
141         bu = BeanUtilsBean.getInstance();
142 
143     }
144 
145 
146     /**
147      * Return the tests included in this test suite.
148      */
149     public static Test suite() {
150 
151         return (new TestSuite(BeanUtilsBenchCase.class));
152 
153     }
154 
155 
156     /**
157      * Tear down instance variables required by this test case.
158      */
159     @Override
160     public void tearDown() {
161 
162         dynaClass = null;
163         inBean = null;
164         inDyna = null;
165         inMap = null;
166         outBean = null;
167         outDyna = null;
168         bu = null;
169 
170     }
171 
172 
173 
174     // ------------------------------------------------- Individual Test Methods
175 
176 
177     // Time copyProperties() from a bean
178     public void testCopyPropertiesBean() throws Exception {
179 
180         long start;
181         long stop;
182 
183         // Bean->Bean
184         for (long i = 0; i < counter; i++) {
185             bu.copyProperties(outBean, inBean);
186         }
187         start = System.currentTimeMillis();
188         for (long i = 0; i < counter; i++) {
189             bu.copyProperties(outBean, inBean);
190         }
191         stop = System.currentTimeMillis();
192         System.err.println("BU.copyProperties(bean,bean), count=" + counter +
193                            ", time=" + (stop - start));
194 
195         // Bean->Dyna
196         for (long i = 0; i < counter; i++) {
197             bu.copyProperties(outDyna, inBean);
198         }
199         start = System.currentTimeMillis();
200         for (long i = 0; i < counter; i++) {
201             bu.copyProperties(outDyna, inBean);
202         }
203         stop = System.currentTimeMillis();
204         System.err.println("BU.copyProperties(dyna,bean), count=" + counter +
205                            ", time=" + (stop - start));
206 
207     }
208 
209 
210     // Time copyProperties() from a DynaBean
211     public void testCopyPropertiesDyna() throws Exception {
212 
213         long start;
214         long stop;
215 
216         // Dyna->Bean
217         for (long i = 0; i < counter; i++) {
218             bu.copyProperties(outBean, inDyna);
219         }
220         start = System.currentTimeMillis();
221         for (long i = 0; i < counter; i++) {
222             bu.copyProperties(outBean, inDyna);
223         }
224         stop = System.currentTimeMillis();
225         System.err.println("BU.copyProperties(bean,dyna), count=" + counter +
226                            ", time=" + (stop - start));
227 
228         // Dyna->Dyna
229         for (long i = 0; i < counter; i++) {
230             bu.copyProperties(outDyna, inDyna);
231         }
232         start = System.currentTimeMillis();
233         for (long i = 0; i < counter; i++) {
234             bu.copyProperties(outDyna, inDyna);
235         }
236         stop = System.currentTimeMillis();
237         System.err.println("BU.copyProperties(dyna,dyna), count=" + counter +
238                            ", time=" + (stop - start));
239 
240     }
241 
242 
243     // Time copyProperties() from a Map of Objects
244     public void testCopyPropertiesMap() throws Exception {
245 
246         long start;
247         long stop;
248 
249         // Map->Bean
250         for (long i = 0; i < counter; i++) {
251             bu.copyProperties(outBean, inMap);
252         }
253         start = System.currentTimeMillis();
254         for (long i = 0; i < counter; i++) {
255             bu.copyProperties(outBean, inMap);
256         }
257         stop = System.currentTimeMillis();
258         System.err.println("BU.copyProperties(bean, map), count=" + counter +
259                            ", time=" + (stop - start));
260 
261         // Map->Dyna
262         for (long i = 0; i < counter; i++) {
263             bu.copyProperties(outDyna, inMap);
264         }
265         start = System.currentTimeMillis();
266         for (long i = 0; i < counter; i++) {
267             bu.copyProperties(outDyna, inMap);
268         }
269         stop = System.currentTimeMillis();
270         System.err.println("BU.copyProperties(dyna, map), count=" + counter +
271                            ", time=" + (stop - start));
272 
273     }
274 
275 
276     // Time copyProperties() from a Map of Strings
277     public void testCopyPropertiesStrs() throws Exception {
278 
279         long start;
280         long stop;
281 
282         // Strs->Bean
283         for (long i = 0; i < counter; i++) {
284             bu.copyProperties(outBean, inStrs);
285         }
286         start = System.currentTimeMillis();
287         for (long i = 0; i < counter; i++) {
288             bu.copyProperties(outBean, inStrs);
289         }
290         stop = System.currentTimeMillis();
291         System.err.println("BU.copyProperties(bean,strs), count=" + counter +
292                            ", time=" + (stop - start));
293 
294         // Strs->Dyna
295         for (long i = 0; i < counter; i++) {
296             bu.copyProperties(outDyna, inStrs);
297         }
298         start = System.currentTimeMillis();
299         for (long i = 0; i < counter; i++) {
300             bu.copyProperties(outDyna, inStrs);
301         }
302         stop = System.currentTimeMillis();
303         System.err.println("BU.copyProperties(dyna,strs), count=" + counter +
304                            ", time=" + (stop - start));
305 
306     }
307 
308 
309     // Time populate() from a Map of Objects
310     public void testPopulateMap() throws Exception {
311 
312         long start;
313         long stop;
314 
315         // Map->Bean
316         for (long i = 0; i < counter; i++) {
317             bu.populate(outBean, inMap);
318         }
319         start = System.currentTimeMillis();
320         for (long i = 0; i < counter; i++) {
321             bu.populate(outBean, inMap);
322         }
323         stop = System.currentTimeMillis();
324         System.err.println("BU.populate(bean, map), count=" + counter +
325                            ", time=" + (stop - start));
326 
327         // Map->Dyna
328         for (long i = 0; i < counter; i++) {
329             bu.populate(outDyna, inMap);
330         }
331         start = System.currentTimeMillis();
332         for (long i = 0; i < counter; i++) {
333             bu.populate(outDyna, inMap);
334         }
335         stop = System.currentTimeMillis();
336         System.err.println("BU.populate(dyna, map), count=" + counter +
337                            ", time=" + (stop - start));
338 
339     }
340 
341 
342     // Time populate() from a Map of Strings
343     // NOTE - This simulates what Struts does when processing form beans
344     public void testPopulateStrs() throws Exception {
345 
346         long start;
347         long stop;
348 
349         // Strs->Bean
350         for (long i = 0; i < counter; i++) {
351             bu.populate(outBean, inStrs);
352         }
353         start = System.currentTimeMillis();
354         for (long i = 0; i < counter; i++) {
355             bu.populate(outBean, inStrs);
356         }
357         stop = System.currentTimeMillis();
358         System.err.println("BU.populate(bean,strs), count=" + counter +
359                            ", time=" + (stop - start));
360 
361         // Strs->Dyna
362         for (long i = 0; i < counter; i++) {
363             bu.populate(outDyna, inStrs);
364         }
365         start = System.currentTimeMillis();
366         for (long i = 0; i < counter; i++) {
367             bu.populate(outDyna, inStrs);
368         }
369         stop = System.currentTimeMillis();
370         System.err.println("BU.populate(dyna,strs), count=" + counter +
371                            ", time=" + (stop - start));
372 
373     }
374 
375 
376     // --------------------------------------------------------- Support Methods
377 
378 
379 }