KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > beanutils > BeanUtilsBenchCase


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.beanutils;
19
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import junit.framework.TestCase;
25 import junit.framework.Test;
26 import junit.framework.TestSuite;
27
28
29 /**
30  * JUnit Test Case containing microbenchmarks for BeanUtils.
31  */

32
33 public class BeanUtilsBenchCase extends TestCase {
34
35
36     // ------------------------------------------------------------ Constructors
37

38
39     /**
40      * Construct a new instance of this test case.
41      *
42      * @param name Name of the test case
43      */

44     public BeanUtilsBenchCase(String JavaDoc name) {
45
46         super(name);
47
48     }
49
50
51     // ------------------------------------------------------ Instance Variables
52

53
54     // Basic loop counter
55
private long counter = 100000;
56
57     // DynaClass for inDyna and outDyna
58
private DynaClass dynaClass = null;
59
60     // Input objects that have identical sets of properties and values.
61
private BenchBean inBean = null;
62     private DynaBean inDyna = null;
63     private Map JavaDoc inMap = null; // Map of Objects requiring no conversion
64
private Map JavaDoc inStrs = null; // Map of Strings requiring conversion
65

66     // Output objects that have identical sets of properties.
67
private BenchBean outBean = null;
68     private DynaBean outDyna = null;
69
70     // BeanUtilsBean instance to be used
71
private BeanUtilsBean bu = null;
72
73
74     // ---------------------------------------------------- Overall Test Methods
75

76
77     /**
78      * Set up instance variables required by this test case.
79      */

80     public void setUp() throws Exception JavaDoc {
81
82         // Set up loop counter (if property specified)
83
String JavaDoc prop = System.getProperty("counter");
84         if (prop != null) {
85             counter = Long.parseLong(prop);
86         }
87
88         // Set up DynaClass for our DynaBean instances
89
dynaClass = new BasicDynaClass
90             ("BenchDynaClass", null,
91              new DynaProperty[]{
92                  new DynaProperty("booleanProperty", Boolean.TYPE),
93                  new DynaProperty("byteProperty", Byte.TYPE),
94                  new DynaProperty("doubleProperty", Double.TYPE),
95                  new DynaProperty("floatProperty", Float.TYPE),
96                  new DynaProperty("intProperty", Integer.TYPE),
97                  new DynaProperty("longProperty", Long.TYPE),
98                  new DynaProperty("shortProperty", Short.TYPE),
99                  new DynaProperty("stringProperty", String JavaDoc.class),
100              });
101
102         // Create input instances
103
inBean = new BenchBean();
104         inMap = new HashMap JavaDoc();
105         inMap.put("booleanProperty", new Boolean JavaDoc(inBean.getBooleanProperty()));
106         inMap.put("byteProperty", new Byte JavaDoc(inBean.getByteProperty()));
107         inMap.put("doubleProperty", new Double JavaDoc(inBean.getDoubleProperty()));
108         inMap.put("floatProperty", new Float JavaDoc(inBean.getFloatProperty()));
109         inMap.put("intProperty", new Integer JavaDoc(inBean.getIntProperty()));
110         inMap.put("longProperty", new Long JavaDoc(inBean.getLongProperty()));
111         inMap.put("shortProperty", new Short JavaDoc(inBean.getShortProperty()));
112         inMap.put("stringProperty", inBean.getStringProperty());
113         inDyna = dynaClass.newInstance();
114         Iterator JavaDoc inKeys = inMap.keySet().iterator();
115         while (inKeys.hasNext()) {
116             String JavaDoc inKey = (String JavaDoc) inKeys.next();
117             inDyna.set(inKey, inMap.get(inKey));
118         }
119         inStrs = new HashMap JavaDoc();
120         inKeys = inMap.keySet().iterator();
121         while (inKeys.hasNext()) {
122             String JavaDoc inKey = (String JavaDoc) inKeys.next();
123             inStrs.put(inKey, inMap.get(inKey).toString());
124         }
125
126         // Create output instances
127
outBean = new BenchBean();
128         outDyna = dynaClass.newInstance();
129         Iterator JavaDoc outKeys = inMap.keySet().iterator();
130         while (outKeys.hasNext()) {
131             String JavaDoc outKey = (String JavaDoc) outKeys.next();
132             outDyna.set(outKey, inMap.get(outKey));
133         }
134
135         // Set up BeanUtilsBean instance we will use
136
bu = BeanUtilsBean.getInstance();
137
138     }
139
140
141     /**
142      * Return the tests included in this test suite.
143      */

144     public static Test suite() {
145
146         return (new TestSuite(BeanUtilsBenchCase.class));
147
148     }
149
150
151     /**
152      * Tear down instance variables required by this test case.
153      */

154     public void tearDown() {
155
156         dynaClass = null;
157         inBean = null;
158         inDyna = null;
159         inMap = null;
160         outBean = null;
161         outDyna = null;
162         bu = null;
163
164     }
165
166
167
168     // ------------------------------------------------- Individual Test Methods
169

170
171     // Time copyProperties() from a bean
172
public void testCopyPropertiesBean() throws Exception JavaDoc {
173
174         long start;
175         long stop;
176
177         // Bean->Bean
178
for (long i = 0; i < counter; i++) {
179             bu.copyProperties(outBean, inBean);
180         }
181         start = System.currentTimeMillis();
182         for (long i = 0; i < counter; i++) {
183             bu.copyProperties(outBean, inBean);
184         }
185         stop = System.currentTimeMillis();
186         System.err.println("BU.copyProperties(bean,bean), count=" + counter +
187                            ", time=" + (stop - start));
188
189         // Bean->Dyna
190
for (long i = 0; i < counter; i++) {
191             bu.copyProperties(outDyna, inBean);
192         }
193         start = System.currentTimeMillis();
194         for (long i = 0; i < counter; i++) {
195             bu.copyProperties(outDyna, inBean);
196         }
197         stop = System.currentTimeMillis();
198         System.err.println("BU.copyProperties(dyna,bean), count=" + counter +
199                            ", time=" + (stop - start));
200
201     }
202
203
204     // Time copyProperties() from a DynaBean
205
public void testCopyPropertiesDyna() throws Exception JavaDoc {
206
207         long start;
208         long stop;
209
210         // Dyna->Bean
211
for (long i = 0; i < counter; i++) {
212             bu.copyProperties(outBean, inDyna);
213         }
214         start = System.currentTimeMillis();
215         for (long i = 0; i < counter; i++) {
216             bu.copyProperties(outBean, inDyna);
217         }
218         stop = System.currentTimeMillis();
219         System.err.println("BU.copyProperties(bean,dyna), count=" + counter +
220                            ", time=" + (stop - start));
221
222         // Dyna->Dyna
223
for (long i = 0; i < counter; i++) {
224             bu.copyProperties(outDyna, inDyna);
225         }
226         start = System.currentTimeMillis();
227         for (long i = 0; i < counter; i++) {
228             bu.copyProperties(outDyna, inDyna);
229         }
230         stop = System.currentTimeMillis();
231         System.err.println("BU.copyProperties(dyna,dyna), count=" + counter +
232                            ", time=" + (stop - start));
233
234     }
235
236
237     // Time copyProperties() from a Map of Objects
238
public void testCopyPropertiesMap() throws Exception JavaDoc {
239
240         long start;
241         long stop;
242
243         // Map->Bean
244
for (long i = 0; i < counter; i++) {
245             bu.copyProperties(outBean, inMap);
246         }
247         start = System.currentTimeMillis();
248         for (long i = 0; i < counter; i++) {
249             bu.copyProperties(outBean, inMap);
250         }
251         stop = System.currentTimeMillis();
252         System.err.println("BU.copyProperties(bean, map), count=" + counter +
253                            ", time=" + (stop - start));
254
255         // Map->Dyna
256
for (long i = 0; i < counter; i++) {
257             bu.copyProperties(outDyna, inMap);
258         }
259         start = System.currentTimeMillis();
260         for (long i = 0; i < counter; i++) {
261             bu.copyProperties(outDyna, inMap);
262         }
263         stop = System.currentTimeMillis();
264         System.err.println("BU.copyProperties(dyna, map), count=" + counter +
265                            ", time=" + (stop - start));
266
267     }
268
269
270     // Time copyProperties() from a Map of Strings
271
public void testCopyPropertiesStrs() throws Exception JavaDoc {
272
273         long start;
274         long stop;
275
276         // Strs->Bean
277
for (long i = 0; i < counter; i++) {
278             bu.copyProperties(outBean, inStrs);
279         }
280         start = System.currentTimeMillis();
281         for (long i = 0; i < counter; i++) {
282             bu.copyProperties(outBean, inStrs);
283         }
284         stop = System.currentTimeMillis();
285         System.err.println("BU.copyProperties(bean,strs), count=" + counter +
286                            ", time=" + (stop - start));
287
288         // Strs->Dyna
289
for (long i = 0; i < counter; i++) {
290             bu.copyProperties(outDyna, inStrs);
291         }
292         start = System.currentTimeMillis();
293         for (long i = 0; i < counter; i++) {
294             bu.copyProperties(outDyna, inStrs);
295         }
296         stop = System.currentTimeMillis();
297         System.err.println("BU.copyProperties(dyna,strs), count=" + counter +
298                            ", time=" + (stop - start));
299
300     }
301
302
303     // Time populate() from a Map of Objects
304
public void testPopulateMap() throws Exception JavaDoc {
305
306         long start;
307         long stop;
308
309         // Map->Bean
310
for (long i = 0; i < counter; i++) {
311             bu.populate(outBean, inMap);
312         }
313         start = System.currentTimeMillis();
314         for (long i = 0; i < counter; i++) {
315             bu.populate(outBean, inMap);
316         }
317         stop = System.currentTimeMillis();
318         System.err.println("BU.populate(bean, map), count=" + counter +
319                            ", time=" + (stop - start));
320
321         // Map->Dyna
322
for (long i = 0; i < counter; i++) {
323             bu.populate(outDyna, inMap);
324         }
325         start = System.currentTimeMillis();
326         for (long i = 0; i < counter; i++) {
327             bu.populate(outDyna, inMap);
328         }
329         stop = System.currentTimeMillis();
330         System.err.println("BU.populate(dyna, map), count=" + counter +
331                            ", time=" + (stop - start));
332
333     }
334
335
336     // Time populate() from a Map of Strings
337
// NOTE - This simulates what Struts does when processing form beans
338
public void testPopulateStrs() throws Exception JavaDoc {
339
340         long start;
341         long stop;
342
343         // Strs->Bean
344
for (long i = 0; i < counter; i++) {
345             bu.populate(outBean, inStrs);
346         }
347         start = System.currentTimeMillis();
348         for (long i = 0; i < counter; i++) {
349             bu.populate(outBean, inStrs);
350         }
351         stop = System.currentTimeMillis();
352         System.err.println("BU.populate(bean,strs), count=" + counter +
353                            ", time=" + (stop - start));
354
355         // Strs->Dyna
356
for (long i = 0; i < counter; i++) {
357             bu.populate(outDyna, inStrs);
358         }
359         start = System.currentTimeMillis();
360         for (long i = 0; i < counter; i++) {
361             bu.populate(outDyna, inStrs);
362         }
363         stop = System.currentTimeMillis();
364         System.err.println("BU.populate(dyna,strs), count=" + counter +
365                            ", time=" + (stop - start));
366
367     }
368
369
370     // --------------------------------------------------------- Support Methods
371

372
373 }
374
Popular Tags