View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.beanutils2;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.junit.jupiter.api.AfterEach;
24  import org.junit.jupiter.api.BeforeEach;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * JUnit Test Case containing microbenchmarks for PropertyUtils.
29   */
30  public class PropertyUtilsBenchCase {
31  
32      // Basic loop counter
33      private long counter = 100000;
34  
35      // DynaClass for inDyna and outDyna
36      private DynaClass dynaClass;
37  
38      // Input objects that have identical sets of properties and values.
39      private BenchBean inBean;
40      private DynaBean inDyna;
41      private Map<String, Object> inMap;
42  
43      // Output objects that have identical sets of properties.
44      private BenchBean outBean;
45      private DynaBean outDyna;
46  
47      // PropertyUtilsBean instance to be used
48      private PropertyUtilsBean pu;
49  
50      /**
51       * Sets up instance variables required by this test case.
52       */
53      @BeforeEach
54      public void setUp() throws Exception {
55  
56          // Set up loop counter (if property specified)
57          final String prop = System.getProperty("counter");
58          if (prop != null) {
59              counter = Long.parseLong(prop);
60          }
61  
62          // Set up DynaClass for our DynaBean instances
63          dynaClass = new BasicDynaClass("BenchDynaClass", null,
64                  new DynaProperty[] { new DynaProperty("booleanProperty", Boolean.TYPE), new DynaProperty("byteProperty", Byte.TYPE),
65                          new DynaProperty("doubleProperty", Double.TYPE), new DynaProperty("floatProperty", Float.TYPE),
66                          new DynaProperty("intProperty", Integer.TYPE), new DynaProperty("longProperty", Long.TYPE),
67                          new DynaProperty("shortProperty", Short.TYPE), new DynaProperty("stringProperty", String.class), });
68  
69          // Create input instances
70          inBean = new BenchBean();
71          inMap = new HashMap<>();
72          inMap.put("booleanProperty", Boolean.valueOf(inBean.getBooleanProperty()));
73          inMap.put("byteProperty", Byte.valueOf(inBean.getByteProperty()));
74          inMap.put("doubleProperty", Double.valueOf(inBean.getDoubleProperty()));
75          inMap.put("floatProperty", Float.valueOf(inBean.getFloatProperty()));
76          inMap.put("intProperty", Integer.valueOf(inBean.getIntProperty()));
77          inMap.put("longProperty", Long.valueOf(inBean.getLongProperty()));
78          inMap.put("shortProperty", Short.valueOf(inBean.getShortProperty()));
79          inMap.put("stringProperty", inBean.getStringProperty());
80          inDyna = dynaClass.newInstance();
81          for (final Map.Entry<String, Object> e : inMap.entrySet()) {
82              inDyna.set(e.getKey(), e.getValue());
83          }
84  
85          // Create output instances
86          outBean = new BenchBean();
87          outDyna = dynaClass.newInstance();
88          inDyna = dynaClass.newInstance();
89          for (final Map.Entry<String, Object> e : inMap.entrySet()) {
90              outDyna.set(e.getKey(), e.getValue());
91          }
92  
93          // Set up PropertyUtilsBean instance we will use
94          pu = PropertyUtilsBean.getInstance();
95  
96      }
97  
98      /**
99       * Tear down instance variables required by this test case.
100      */
101     @AfterEach
102     public void tearDown() {
103 
104         dynaClass = null;
105         inBean = null;
106         inDyna = null;
107         inMap = null;
108         outBean = null;
109         outDyna = null;
110         pu = null;
111 
112     }
113 
114     // Time copyProperties() from a bean
115     @Test
116     public void testCopyPropertiesBean() throws Exception {
117 
118         long startMillis;
119         long stopMillis;
120 
121         // Bean->Bean
122         for (long i = 0; i < counter; i++) {
123             pu.copyProperties(outBean, inBean);
124         }
125         startMillis = System.currentTimeMillis();
126         for (long i = 0; i < counter; i++) {
127             pu.copyProperties(outBean, inBean);
128         }
129         stopMillis = System.currentTimeMillis();
130         System.err.println("PU.copyProperties(bean,bean), count=" + counter + ", time=" + (stopMillis - startMillis));
131 
132         // Bean->Dyna
133         for (long i = 0; i < counter; i++) {
134             pu.copyProperties(outDyna, inBean);
135         }
136         startMillis = System.currentTimeMillis();
137         for (long i = 0; i < counter; i++) {
138             pu.copyProperties(outDyna, inBean);
139         }
140         stopMillis = System.currentTimeMillis();
141         System.err.println("PU.copyProperties(dyna,bean), count=" + counter + ", time=" + (stopMillis - startMillis));
142 
143     }
144 
145     // Time copyProperties() from a DynaBean
146     @Test
147     public void testCopyPropertiesDyna() throws Exception {
148 
149         long startMillis;
150         long stopMillis;
151 
152         // Dyna->Bean
153         for (long i = 0; i < counter; i++) {
154             pu.copyProperties(outBean, inDyna);
155         }
156         startMillis = System.currentTimeMillis();
157         for (long i = 0; i < counter; i++) {
158             pu.copyProperties(outBean, inDyna);
159         }
160         stopMillis = System.currentTimeMillis();
161         System.err.println("PU.copyProperties(bean,dyna), count=" + counter + ", time=" + (stopMillis - startMillis));
162 
163         // Dyna->Dyna
164         for (long i = 0; i < counter; i++) {
165             pu.copyProperties(outDyna, inDyna);
166         }
167         startMillis = System.currentTimeMillis();
168         for (long i = 0; i < counter; i++) {
169             pu.copyProperties(outDyna, inDyna);
170         }
171         stopMillis = System.currentTimeMillis();
172         System.err.println("PU.copyProperties(dyna,dyna), count=" + counter + ", time=" + (stopMillis - startMillis));
173 
174     }
175 
176     // Time copyProperties() from a Map
177     @Test
178     public void testCopyPropertiesMap() throws Exception {
179 
180         long startMillis;
181         long stopMillis;
182 
183         // Dyna->Bean
184         for (long i = 0; i < counter; i++) {
185             pu.copyProperties(outBean, inMap);
186         }
187         startMillis = System.currentTimeMillis();
188         for (long i = 0; i < counter; i++) {
189             pu.copyProperties(outBean, inMap);
190         }
191         stopMillis = System.currentTimeMillis();
192         System.err.println("PU.copyProperties(bean, map), count=" + counter + ", time=" + (stopMillis - startMillis));
193 
194         // Dyna->Dyna
195         for (long i = 0; i < counter; i++) {
196             pu.copyProperties(outDyna, inMap);
197         }
198         startMillis = System.currentTimeMillis();
199         for (long i = 0; i < counter; i++) {
200             pu.copyProperties(outDyna, inMap);
201         }
202         stopMillis = System.currentTimeMillis();
203         System.err.println("PU.copyProperties(dyna, map), count=" + counter + ", time=" + (stopMillis - startMillis));
204 
205     }
206 
207 }