    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    *      http://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
    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   * JUnit Test Case containing microbenchmarks for BeanUtils.
    32   */
    33
    34  public class BeanUtilsBenchCase extends TestCase {
    35
    36
    37      // ------------------------------------------------------------ Constructors
    38
    39
    40      /***
    41       * Construct a new instance of this test case.
    42       *
    43       * @param name Name of the test case
    44       */
    45      public BeanUtilsBenchCase(String name) {
    46
    47          super(name);
    48
    49      }
    50
    51
    52      // ------------------------------------------------------ Instance Variables
    53
    54
    55      // Basic loop counter
    56      private long counter = 100000;
    57
    58      // DynaClass for inDyna and outDyna
    59      private DynaClass dynaClass = null;
    60
    61      // Input objects that have identical sets of properties and values.
    62      private BenchBean inBean = null;
    63      private DynaBean inDyna = null;
    64      private Map inMap = null;  // Map of Objects requiring no conversion
    65      private Map inStrs = null; // Map of Strings requiring conversion
    66
    67      // Output objects that have identical sets of properties.
    68      private BenchBean outBean = null;
    69      private DynaBean outDyna = null;
    70
    71      // BeanUtilsBean instance to be used
    72      private BeanUtilsBean bu = null;
    73
    74
    75      // ---------------------------------------------------- Overall Test Methods
    76
    77
    78      /***
    79       * Set up instance variables required by this test case.
    80       */
    81      public void setUp() throws Exception {
    82
    83          // Set up loop counter (if property specified)
    84          String prop = System.getProperty("counter");
    85          if (prop != null) {
    86              counter = Long.parseLong(prop);
    87          }
    88
    89          // Set up DynaClass for our DynaBean instances
    90          dynaClass = new BasicDynaClass
    91              ("BenchDynaClass", null,
    92               new DynaProperty[]{
    93                   new DynaProperty("booleanProperty", Boolean.TYPE),
    94                   new DynaProperty("byteProperty", Byte.TYPE),
    95                   new DynaProperty("doubleProperty", Double.TYPE),
    96                   new DynaProperty("floatProperty", Float.TYPE),
    97                   new DynaProperty("intProperty", Integer.TYPE),
    98                   new DynaProperty("longProperty", Long.TYPE),
    99                   new DynaProperty("shortProperty", Short.TYPE),
    100                  new DynaProperty("stringProperty", String.class),
    101              });
    102
    103         // Create input instances
    104         inBean = new BenchBean();
    105         inMap = new HashMap();
    106         inMap.put("booleanProperty", new Boolean(inBean.getBooleanProperty()));
    107         inMap.put("byteProperty", new Byte(inBean.getByteProperty()));
    108         inMap.put("doubleProperty", new Double(inBean.getDoubleProperty()));
    109         inMap.put("floatProperty", new Float(inBean.getFloatProperty()));
    110         inMap.put("intProperty", new Integer(inBean.getIntProperty()));
    111         inMap.put("longProperty", new Long(inBean.getLongProperty()));
    112         inMap.put("shortProperty", new Short(inBean.getShortProperty()));
    113         inMap.put("stringProperty", inBean.getStringProperty());
    114         inDyna = dynaClass.newInstance();
    115         Iterator inKeys = inMap.keySet().iterator();
    116         while (inKeys.hasNext()) {
    117             String inKey = (String) inKeys.next();
    118             inDyna.set(inKey, inMap.get(inKey));
    119         }
    120         inStrs = new HashMap();
    121         inKeys = inMap.keySet().iterator();
    122         while (inKeys.hasNext()) {
    123             String inKey = (String) inKeys.next();
    124             inStrs.put(inKey, inMap.get(inKey).toString());
    125         }
    126
    127         // Create output instances
    128         outBean = new BenchBean();
    129         outDyna = dynaClass.newInstance();
    130         Iterator outKeys = inMap.keySet().iterator();
    131         while (outKeys.hasNext()) {
    132             String outKey = (String) outKeys.next();
    133             outDyna.set(outKey, inMap.get(outKey));
    134         }
    135
    136         // Set up BeanUtilsBean instance we will use
    137         bu = BeanUtilsBean.getInstance();
    138
    139     }
    140
    141
    142     /***
    143      * Return the tests included in this test suite.
    144      */
    145     public static Test suite() {
    146
    147         return (new TestSuite(BeanUtilsBenchCase.class));
    148
    149     }
    150
    151
    152     /***
    153      * Tear down instance variables required by this test case.
    154      */
    155     public void tearDown() {
    156
    157         dynaClass = null;
    158         inBean = null;
    159         inDyna = null;
    160         inMap = null;
    161         outBean = null;
    162         outDyna = null;
    163         bu = null;
    164
    165     }
    166
    167
    168
    169     // ------------------------------------------------- Individual Test Methods
    170
    171
    172     // Time copyProperties() from a bean
    173     public void testCopyPropertiesBean() throws Exception {
    174
    175         long start;
    176         long stop;
    177
    178         // Bean->Bean
    179         for (long i = 0; i < counter; i++) {
    180             bu.copyProperties(outBean, inBean);
    181         }
    182         start = System.currentTimeMillis();
    183         for (long i = 0; i < counter; i++) {
    184             bu.copyProperties(outBean, inBean);
    185         }
    186         stop = System.currentTimeMillis();
    187         System.err.println("BU.copyProperties(bean,bean), count=" + counter +
    188                            ", time=" + (stop - start));
    189
    190         // Bean->Dyna
    191         for (long i = 0; i < counter; i++) {
    192             bu.copyProperties(outDyna, inBean);
    193         }
    194         start = System.currentTimeMillis();
    195         for (long i = 0; i < counter; i++) {
    196             bu.copyProperties(outDyna, inBean);
    197         }
    198         stop = System.currentTimeMillis();
    199         System.err.println("BU.copyProperties(dyna,bean), count=" + counter +
    200                            ", time=" + (stop - start));
    201
    202     }
    203
    204
    205     // Time copyProperties() from a DynaBean
    206     public void testCopyPropertiesDyna() throws Exception {
    207
    208         long start;
    209         long stop;
    210
    211         // Dyna->Bean
    212         for (long i = 0; i < counter; i++) {
    213             bu.copyProperties(outBean, inDyna);
    214         }
    215         start = System.currentTimeMillis();
    216         for (long i = 0; i < counter; i++) {
    217             bu.copyProperties(outBean, inDyna);
    218         }
    219         stop = System.currentTimeMillis();
    220         System.err.println("BU.copyProperties(bean,dyna), count=" + counter +
    221                            ", time=" + (stop - start));
    222
    223         // Dyna->Dyna
    224         for (long i = 0; i < counter; i++) {
    225             bu.copyProperties(outDyna, inDyna);
    226         }
    227         start = System.currentTimeMillis();
    228         for (long i = 0; i < counter; i++) {
    229             bu.copyProperties(outDyna, inDyna);
    230         }
    231         stop = System.currentTimeMillis();
    232         System.err.println("BU.copyProperties(dyna,dyna), count=" + counter +
    233                            ", time=" + (stop - start));
    234
    235     }
    236
    237
    238     // Time copyProperties() from a Map of Objects
    239     public void testCopyPropertiesMap() throws Exception {
    240
    241         long start;
    242         long stop;
    243
    244         // Map->Bean
    245         for (long i = 0; i < counter; i++) {
    246             bu.copyProperties(outBean, inMap);
    247         }
    248         start = System.currentTimeMillis();
    249         for (long i = 0; i < counter; i++) {
    250             bu.copyProperties(outBean, inMap);
    251         }
    252         stop = System.currentTimeMillis();
    253         System.err.println("BU.copyProperties(bean, map), count=" + counter +
    254                            ", time=" + (stop - start));
    255
    256         // Map->Dyna
    257         for (long i = 0; i < counter; i++) {
    258             bu.copyProperties(outDyna, inMap);
    259         }
    260         start = System.currentTimeMillis();
    261         for (long i = 0; i < counter; i++) {
    262             bu.copyProperties(outDyna, inMap);
    263         }
    264         stop = System.currentTimeMillis();
    265         System.err.println("BU.copyProperties(dyna, map), count=" + counter +
    266                            ", time=" + (stop - start));
    267
    268     }
    269
    270
    271     // Time copyProperties() from a Map of Strings
    272     public void testCopyPropertiesStrs() throws Exception {
    273
    274         long start;
    275         long stop;
    276
    277         // Strs->Bean
    278         for (long i = 0; i < counter; i++) {
    279             bu.copyProperties(outBean, inStrs);
    280         }
    281         start = System.currentTimeMillis();
    282         for (long i = 0; i < counter; i++) {
    283             bu.copyProperties(outBean, inStrs);
    284         }
    285         stop = System.currentTimeMillis();
    286         System.err.println("BU.copyProperties(bean,strs), count=" + counter +
    287                            ", time=" + (stop - start));
    288
    289         // Strs->Dyna
    290         for (long i = 0; i < counter; i++) {
    291             bu.copyProperties(outDyna, inStrs);
    292         }
    293         start = System.currentTimeMillis();
    294         for (long i = 0; i < counter; i++) {
    295             bu.copyProperties(outDyna, inStrs);
    296         }
    297         stop = System.currentTimeMillis();
    298         System.err.println("BU.copyProperties(dyna,strs), count=" + counter +
    299                            ", time=" + (stop - start));
    300
    301     }
    302
    303
    304     // Time populate() from a Map of Objects
    305     public void testPopulateMap() throws Exception {
    306
    307         long start;
    308         long stop;
    309
    310         // Map->Bean
    311         for (long i = 0; i < counter; i++) {
    312             bu.populate(outBean, inMap);
    313         }
    314         start = System.currentTimeMillis();
    315         for (long i = 0; i < counter; i++) {
    316             bu.populate(outBean, inMap);
    317         }
    318         stop = System.currentTimeMillis();
    319         System.err.println("BU.populate(bean, map), count=" + counter +
    320                            ", time=" + (stop - start));
    321
    322         // Map->Dyna
    323         for (long i = 0; i < counter; i++) {
    324             bu.populate(outDyna, inMap);
    325         }
    326         start = System.currentTimeMillis();
    327         for (long i = 0; i < counter; i++) {
    328             bu.populate(outDyna, inMap);
    329         }
    330         stop = System.currentTimeMillis();
    331         System.err.println("BU.populate(dyna, map), count=" + counter +
    332                            ", time=" + (stop - start));
    333
    334     }
    335
    336
    337     // Time populate() from a Map of Strings
    338     // NOTE - This simulates what Struts does when processing form beans
    339     public void testPopulateStrs() throws Exception {
    340
    341         long start;
    342         long stop;
    343
    344         // Strs->Bean
    345         for (long i = 0; i < counter; i++) {
    346             bu.populate(outBean, inStrs);
    347         }
    348         start = System.currentTimeMillis();
    349         for (long i = 0; i < counter; i++) {
    350             bu.populate(outBean, inStrs);
    351         }
    352         stop = System.currentTimeMillis();
    353         System.err.println("BU.populate(bean,strs), count=" + counter +
    354                            ", time=" + (stop - start));
    355
    356         // Strs->Dyna
    357         for (long i = 0; i < counter; i++) {
    358             bu.populate(outDyna, inStrs);
    359         }
    360         start = System.currentTimeMillis();
    361         for (long i = 0; i < counter; i++) {
    362             bu.populate(outDyna, inStrs);
    363         }
    364         stop = System.currentTimeMillis();
    365         System.err.println("BU.populate(dyna,strs), count=" + counter +
    366                            ", time=" + (stop - start));
    367
    368     }
    369
    370
    371     // --------------------------------------------------------- Support Methods
    372
    373
    374 }