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.Map;
24  
25  import junit.framework.Test;
26  import junit.framework.TestCase;
27  import junit.framework.TestSuite;
28  
29  
30  /**
31   * JUnit Test Case containing microbenchmarks for PropertyUtils.
32   *
33   * @version $Id$
34   */
35  
36  public class PropertyUtilsBenchCase extends TestCase {
37  
38  
39      // ------------------------------------------------------------ Constructors
40  
41  
42      /**
43       * Construct a new instance of this test case.
44       *
45       * @param name Name of the test case
46       */
47      public PropertyUtilsBenchCase(final String name) {
48  
49          super(name);
50  
51      }
52  
53  
54      // ------------------------------------------------------ Instance Variables
55  
56  
57      // Basic loop counter
58      private long counter = 100000;
59  
60      // DynaClass for inDyna and outDyna
61      private DynaClass dynaClass = null;
62  
63      // Input objects that have identical sets of properties and values.
64      private BenchBean inBean = null;
65      private DynaBean inDyna = null;
66      private Map<String, Object> inMap = null;
67  
68      // Output objects that have identical sets of properties.
69      private BenchBean outBean = null;
70      private DynaBean outDyna = null;
71  
72      // PropertyUtilsBean instance to be used
73      private PropertyUtilsBean pu = null;
74  
75  
76      // ---------------------------------------------------- Overall Test Methods
77  
78  
79      /**
80       * Set up instance variables required by this test case.
81       */
82      @Override
83      public void setUp() throws Exception {
84  
85          // Set up loop counter (if property specified)
86          final String prop = System.getProperty("counter");
87          if (prop != null) {
88              counter = Long.parseLong(prop);
89          }
90  
91          // Set up DynaClass for our DynaBean instances
92          dynaClass = new BasicDynaClass
93              ("BenchDynaClass", null,
94               new DynaProperty[]{
95                   new DynaProperty("booleanProperty", Boolean.TYPE),
96                   new DynaProperty("byteProperty", Byte.TYPE),
97                   new DynaProperty("doubleProperty", Double.TYPE),
98                   new DynaProperty("floatProperty", Float.TYPE),
99                   new DynaProperty("intProperty", Integer.TYPE),
100                  new DynaProperty("longProperty", Long.TYPE),
101                  new DynaProperty("shortProperty", Short.TYPE),
102                  new DynaProperty("stringProperty", String.class),
103              });
104 
105         // Create input instances
106         inBean = new BenchBean();
107         inMap = new HashMap<String, Object>();
108         inMap.put("booleanProperty", new Boolean(inBean.getBooleanProperty()));
109         inMap.put("byteProperty", new Byte(inBean.getByteProperty()));
110         inMap.put("doubleProperty", new Double(inBean.getDoubleProperty()));
111         inMap.put("floatProperty", new Float(inBean.getFloatProperty()));
112         inMap.put("intProperty", new Integer(inBean.getIntProperty()));
113         inMap.put("longProperty", new Long(inBean.getLongProperty()));
114         inMap.put("shortProperty", new Short(inBean.getShortProperty()));
115         inMap.put("stringProperty", inBean.getStringProperty());
116         inDyna = dynaClass.newInstance();
117         for (final Map.Entry<String, Object> e : inMap.entrySet()) {
118             inDyna.set(e.getKey(), e.getValue());
119         }
120 
121         // Create output instances
122         outBean = new BenchBean();
123         outDyna = dynaClass.newInstance();
124         inDyna = dynaClass.newInstance();
125         for (final Map.Entry<String, Object> e : inMap.entrySet()) {
126             outDyna.set(e.getKey(), e.getValue());
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     @Override
149     public void tearDown() {
150 
151         dynaClass = null;
152         inBean = null;
153         inDyna = null;
154         inMap = null;
155         outBean = null;
156         outDyna = null;
157         pu = null;
158 
159     }
160 
161 
162 
163     // ------------------------------------------------- Individual Test Methods
164 
165 
166     // Time copyProperties() from a bean
167     public void testCopyPropertiesBean() throws Exception {
168 
169         long start;
170         long stop;
171 
172         // Bean->Bean
173         for (long i = 0; i < counter; i++) {
174             pu.copyProperties(outBean, inBean);
175         }
176         start = System.currentTimeMillis();
177         for (long i = 0; i < counter; i++) {
178             pu.copyProperties(outBean, inBean);
179         }
180         stop = System.currentTimeMillis();
181         System.err.println("PU.copyProperties(bean,bean), count=" + counter +
182                            ", time=" + (stop - start));
183 
184         // Bean->Dyna
185         for (long i = 0; i < counter; i++) {
186             pu.copyProperties(outDyna, inBean);
187         }
188         start = System.currentTimeMillis();
189         for (long i = 0; i < counter; i++) {
190             pu.copyProperties(outDyna, inBean);
191         }
192         stop = System.currentTimeMillis();
193         System.err.println("PU.copyProperties(dyna,bean), count=" + counter +
194                            ", time=" + (stop - start));
195 
196     }
197 
198 
199     // Time copyProperties() from a DynaBean
200     public void testCopyPropertiesDyna() throws Exception {
201 
202         long start;
203         long stop;
204 
205         // Dyna->Bean
206         for (long i = 0; i < counter; i++) {
207             pu.copyProperties(outBean, inDyna);
208         }
209         start = System.currentTimeMillis();
210         for (long i = 0; i < counter; i++) {
211             pu.copyProperties(outBean, inDyna);
212         }
213         stop = System.currentTimeMillis();
214         System.err.println("PU.copyProperties(bean,dyna), count=" + counter +
215                            ", time=" + (stop - start));
216 
217         // Dyna->Dyna
218         for (long i = 0; i < counter; i++) {
219             pu.copyProperties(outDyna, inDyna);
220         }
221         start = System.currentTimeMillis();
222         for (long i = 0; i < counter; i++) {
223             pu.copyProperties(outDyna, inDyna);
224         }
225         stop = System.currentTimeMillis();
226         System.err.println("PU.copyProperties(dyna,dyna), count=" + counter +
227                            ", time=" + (stop - start));
228 
229     }
230 
231 
232     // Time copyProperties() from a Map
233     public void testCopyPropertiesMap() throws Exception {
234 
235         long start;
236         long stop;
237 
238         // Dyna->Bean
239         for (long i = 0; i < counter; i++) {
240             pu.copyProperties(outBean, inMap);
241         }
242         start = System.currentTimeMillis();
243         for (long i = 0; i < counter; i++) {
244             pu.copyProperties(outBean, inMap);
245         }
246         stop = System.currentTimeMillis();
247         System.err.println("PU.copyProperties(bean, map), count=" + counter +
248                            ", time=" + (stop - start));
249 
250         // Dyna->Dyna
251         for (long i = 0; i < counter; i++) {
252             pu.copyProperties(outDyna, inMap);
253         }
254         start = System.currentTimeMillis();
255         for (long i = 0; i < counter; i++) {
256             pu.copyProperties(outDyna, inMap);
257         }
258         stop = System.currentTimeMillis();
259         System.err.println("PU.copyProperties(dyna, map), count=" + counter +
260                            ", time=" + (stop - start));
261 
262     }
263 
264 
265     // --------------------------------------------------------- Support Methods
266 
267 
268 }