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.Map;
24
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29
30
31
32
33
34
35
36 public class PropertyUtilsBenchCase extends TestCase {
37
38
39
40
41
42
43
44
45
46
47 public PropertyUtilsBenchCase(final String name) {
48
49 super(name);
50
51 }
52
53
54
55
56
57
58 private long counter = 100000;
59
60
61 private DynaClass dynaClass = null;
62
63
64 private BenchBean inBean = null;
65 private DynaBean inDyna = null;
66 private Map<String, Object> inMap = null;
67
68
69 private BenchBean outBean = null;
70 private DynaBean outDyna = null;
71
72
73 private PropertyUtilsBean pu = null;
74
75
76
77
78
79
80
81
82 @Override
83 public void setUp() throws Exception {
84
85
86 final String prop = System.getProperty("counter");
87 if (prop != null) {
88 counter = Long.parseLong(prop);
89 }
90
91
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
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
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
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 @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
164
165
166
167 public void testCopyPropertiesBean() throws Exception {
168
169 long start;
170 long stop;
171
172
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
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
200 public void testCopyPropertiesDyna() throws Exception {
201
202 long start;
203 long stop;
204
205
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
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
233 public void testCopyPropertiesMap() throws Exception {
234
235 long start;
236 long stop;
237
238
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
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
266
267
268 }