1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
33
34
35
36
37 public class BeanUtilsBenchCase extends TestCase {
38
39
40
41
42
43
44
45
46
47
48 public BeanUtilsBenchCase(final String name) {
49
50 super(name);
51
52 }
53
54
55
56
57
58
59 private long counter = 100000;
60
61
62 private DynaClass dynaClass = null;
63
64
65 private BenchBean inBean = null;
66 private DynaBean inDyna = null;
67 private Map<String, Object> inMap = null;
68 private Map<String, String> inStrs = null;
69
70
71 private BenchBean outBean = null;
72 private DynaBean outDyna = null;
73
74
75 private BeanUtilsBean bu = null;
76
77
78
79
80
81
82
83
84 @Override
85 public void setUp() throws Exception {
86
87
88 final String prop = System.getProperty("counter");
89 if (prop != null) {
90 counter = Long.parseLong(prop);
91 }
92
93
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
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
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
141 bu = BeanUtilsBean.getInstance();
142
143 }
144
145
146
147
148
149 public static Test suite() {
150
151 return (new TestSuite(BeanUtilsBenchCase.class));
152
153 }
154
155
156
157
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
175
176
177
178 public void testCopyPropertiesBean() throws Exception {
179
180 long start;
181 long stop;
182
183
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
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
211 public void testCopyPropertiesDyna() throws Exception {
212
213 long start;
214 long stop;
215
216
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
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
244 public void testCopyPropertiesMap() throws Exception {
245
246 long start;
247 long stop;
248
249
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
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
277 public void testCopyPropertiesStrs() throws Exception {
278
279 long start;
280 long stop;
281
282
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
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
310 public void testPopulateMap() throws Exception {
311
312 long start;
313 long stop;
314
315
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
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
343
344 public void testPopulateStrs() throws Exception {
345
346 long start;
347 long stop;
348
349
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
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
377
378
379 }