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 import junit.framework.TestCase;
26 import junit.framework.Test;
27 import junit.framework.TestSuite;
28
29
30
31
32
33
34 public class PropertyUtilsBenchCase extends TestCase {
35
36
37
38
39
40
41
42
43
44
45 public PropertyUtilsBenchCase(String name) {
46
47 super(name);
48
49 }
50
51
52
53
54
55
56 private long counter = 100000;
57
58
59 private DynaClass dynaClass = null;
60
61
62 private BenchBean inBean = null;
63 private DynaBean inDyna = null;
64 private Map inMap = null;
65
66
67 private BenchBean outBean = null;
68 private DynaBean outDyna = null;
69
70
71 private PropertyUtilsBean pu = null;
72
73
74
75
76
77
78
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
120
121 outBean = new BenchBean();
122 outDyna = dynaClass.newInstance();
123 Iterator outKeys = inMap.keySet().iterator();
124 while (outKeys.hasNext()) {
125 String outKey = (String) outKeys.next();
126 outDyna.set(outKey, inMap.get(outKey));
127 }
128
129
130 pu = PropertyUtilsBean.getInstance();
131
132 }
133
134
135
136
137
138 public static Test suite() {
139
140 return (new TestSuite(PropertyUtilsBenchCase.class));
141
142 }
143
144
145
146
147
148 public void tearDown() {
149
150 dynaClass = null;
151 inBean = null;
152 inDyna = null;
153 inMap = null;
154 outBean = null;
155 outDyna = null;
156 pu = null;
157
158 }
159
160
161
162
163
164
165
166 public void testCopyPropertiesBean() throws Exception {
167
168 long start;
169 long stop;
170
171
172 for (long i = 0; i < counter; i++) {
173 pu.copyProperties(outBean, inBean);
174 }
175 start = System.currentTimeMillis();
176 for (long i = 0; i < counter; i++) {
177 pu.copyProperties(outBean, inBean);
178 }
179 stop = System.currentTimeMillis();
180 System.err.println("PU.copyProperties(bean,bean), count=" + counter +
181 ", time=" + (stop - start));
182
183
184 for (long i = 0; i < counter; i++) {
185 pu.copyProperties(outDyna, inBean);
186 }
187 start = System.currentTimeMillis();
188 for (long i = 0; i < counter; i++) {
189 pu.copyProperties(outDyna, inBean);
190 }
191 stop = System.currentTimeMillis();
192 System.err.println("PU.copyProperties(dyna,bean), count=" + counter +
193 ", time=" + (stop - start));
194
195 }
196
197
198
199 public void testCopyPropertiesDyna() throws Exception {
200
201 long start;
202 long stop;
203
204
205 for (long i = 0; i < counter; i++) {
206 pu.copyProperties(outBean, inDyna);
207 }
208 start = System.currentTimeMillis();
209 for (long i = 0; i < counter; i++) {
210 pu.copyProperties(outBean, inDyna);
211 }
212 stop = System.currentTimeMillis();
213 System.err.println("PU.copyProperties(bean,dyna), count=" + counter +
214 ", time=" + (stop - start));
215
216
217 for (long i = 0; i < counter; i++) {
218 pu.copyProperties(outDyna, inDyna);
219 }
220 start = System.currentTimeMillis();
221 for (long i = 0; i < counter; i++) {
222 pu.copyProperties(outDyna, inDyna);
223 }
224 stop = System.currentTimeMillis();
225 System.err.println("PU.copyProperties(dyna,dyna), count=" + counter +
226 ", time=" + (stop - start));
227
228 }
229
230
231
232 public void testCopyPropertiesMap() throws Exception {
233
234 long start;
235 long stop;
236
237
238 for (long i = 0; i < counter; i++) {
239 pu.copyProperties(outBean, inMap);
240 }
241 start = System.currentTimeMillis();
242 for (long i = 0; i < counter; i++) {
243 pu.copyProperties(outBean, inMap);
244 }
245 stop = System.currentTimeMillis();
246 System.err.println("PU.copyProperties(bean, map), count=" + counter +
247 ", time=" + (stop - start));
248
249
250 for (long i = 0; i < counter; i++) {
251 pu.copyProperties(outDyna, inMap);
252 }
253 start = System.currentTimeMillis();
254 for (long i = 0; i < counter; i++) {
255 pu.copyProperties(outDyna, inMap);
256 }
257 stop = System.currentTimeMillis();
258 System.err.println("PU.copyProperties(dyna, map), count=" + counter +
259 ", time=" + (stop - start));
260
261 }
262
263
264
265
266
267 }