KickJava   Java API By Example, From Geeks To Geeks.

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


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 PropertyUtils.
31  */

32
33 public class PropertyUtilsBenchCase 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 PropertyUtilsBenchCase(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;
64
65     // Output objects that have identical sets of properties.
66
private BenchBean outBean = null;
67     private DynaBean outDyna = null;
68
69     // PropertyUtilsBean instance to be used
70
private PropertyUtilsBean pu = null;
71
72
73     // ---------------------------------------------------- Overall Test Methods
74

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

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

137     public static Test suite() {
138
139         return (new TestSuite(PropertyUtilsBenchCase.class));
140
141     }
142
143
144     /**
145      * Tear down instance variables required by this test case.
146      */

147     public void tearDown() {
148
149         dynaClass = null;
150         inBean = null;
151         inDyna = null;
152         inMap = null;
153         outBean = null;
154         outDyna = null;
155         pu = null;
156
157     }
158
159
160
161     // ------------------------------------------------- Individual Test Methods
162

163
164     // Time copyProperties() from a bean
165
public void testCopyPropertiesBean() throws Exception JavaDoc {
166
167         long start;
168         long stop;
169
170         // Bean->Bean
171
for (long i = 0; i < counter; i++) {
172             pu.copyProperties(outBean, inBean);
173         }
174         start = System.currentTimeMillis();
175         for (long i = 0; i < counter; i++) {
176             pu.copyProperties(outBean, inBean);
177         }
178         stop = System.currentTimeMillis();
179         System.err.println("PU.copyProperties(bean,bean), count=" + counter +
180                            ", time=" + (stop - start));
181
182         // Bean->Dyna
183
for (long i = 0; i < counter; i++) {
184             pu.copyProperties(outDyna, inBean);
185         }
186         start = System.currentTimeMillis();
187         for (long i = 0; i < counter; i++) {
188             pu.copyProperties(outDyna, inBean);
189         }
190         stop = System.currentTimeMillis();
191         System.err.println("PU.copyProperties(dyna,bean), count=" + counter +
192                            ", time=" + (stop - start));
193
194     }
195
196
197     // Time copyProperties() from a DynaBean
198
public void testCopyPropertiesDyna() throws Exception JavaDoc {
199
200         long start;
201         long stop;
202
203         // Dyna->Bean
204
for (long i = 0; i < counter; i++) {
205             pu.copyProperties(outBean, inDyna);
206         }
207         start = System.currentTimeMillis();
208         for (long i = 0; i < counter; i++) {
209             pu.copyProperties(outBean, inDyna);
210         }
211         stop = System.currentTimeMillis();
212         System.err.println("PU.copyProperties(bean,dyna), count=" + counter +
213                            ", time=" + (stop - start));
214
215         // Dyna->Dyna
216
for (long i = 0; i < counter; i++) {
217             pu.copyProperties(outDyna, inDyna);
218         }
219         start = System.currentTimeMillis();
220         for (long i = 0; i < counter; i++) {
221             pu.copyProperties(outDyna, inDyna);
222         }
223         stop = System.currentTimeMillis();
224         System.err.println("PU.copyProperties(dyna,dyna), count=" + counter +
225                            ", time=" + (stop - start));
226
227     }
228
229
230     // Time copyProperties() from a Map
231
public void testCopyPropertiesMap() throws Exception JavaDoc {
232
233         long start;
234         long stop;
235
236         // Dyna->Bean
237
for (long i = 0; i < counter; i++) {
238             pu.copyProperties(outBean, inMap);
239         }
240         start = System.currentTimeMillis();
241         for (long i = 0; i < counter; i++) {
242             pu.copyProperties(outBean, inMap);
243         }
244         stop = System.currentTimeMillis();
245         System.err.println("PU.copyProperties(bean, map), count=" + counter +
246                            ", time=" + (stop - start));
247
248         // Dyna->Dyna
249
for (long i = 0; i < counter; i++) {
250             pu.copyProperties(outDyna, inMap);
251         }
252         start = System.currentTimeMillis();
253         for (long i = 0; i < counter; i++) {
254             pu.copyProperties(outDyna, inMap);
255         }
256         stop = System.currentTimeMillis();
257         System.err.println("PU.copyProperties(dyna, map), count=" + counter +
258                            ", time=" + (stop - start));
259
260     }
261
262
263     // --------------------------------------------------------- Support Methods
264

265
266 }
267
Popular Tags