1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.beanutils;
19
20
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
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
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 name) {
45
46 super(name);
47
48 }
49
50
51
52
53
54
55 private long counter = 100000;
56
57
58 private DynaClass dynaClass = null;
59
60
61 private BenchBean inBean = null;
62 private DynaBean inDyna = null;
63 private Map inMap = null;
64 private Map inStrs = null;
65
66
67 private BenchBean outBean = null;
68 private DynaBean outDyna = null;
69
70
71 private BeanUtilsBean bu = null;
72
73
74
75
76
77 /**
78 * Set up instance variables required by this test case.
79 */
80 public void setUp() throws Exception {
81
82
83 String prop = System.getProperty("counter");
84 if (prop != null) {
85 counter = Long.parseLong(prop);
86 }
87
88
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.class),
100 });
101
102
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 inStrs = new HashMap();
120 inKeys = inMap.keySet().iterator();
121 while (inKeys.hasNext()) {
122 String inKey = (String) inKeys.next();
123 inStrs.put(inKey, inMap.get(inKey).toString());
124 }
125
126
127 outBean = new BenchBean();
128 outDyna = dynaClass.newInstance();
129 Iterator outKeys = inMap.keySet().iterator();
130 while (outKeys.hasNext()) {
131 String outKey = (String) outKeys.next();
132 outDyna.set(outKey, inMap.get(outKey));
133 }
134
135
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
169
170
171
172 public void testCopyPropertiesBean() throws Exception {
173
174 long start;
175 long stop;
176
177
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
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
205 public void testCopyPropertiesDyna() throws Exception {
206
207 long start;
208 long stop;
209
210
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
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
238 public void testCopyPropertiesMap() throws Exception {
239
240 long start;
241 long stop;
242
243
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
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
271 public void testCopyPropertiesStrs() throws Exception {
272
273 long start;
274 long stop;
275
276
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
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
304 public void testPopulateMap() throws Exception {
305
306 long start;
307 long stop;
308
309
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
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
337
338 public void testPopulateStrs() throws Exception {
339
340 long start;
341 long stop;
342
343
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
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
371
372
373 }