    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 PropertyUtils.
    32   */
    33
    34  public class PropertyUtilsBenchCase 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 PropertyUtilsBenchCase(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;
    65
    66      // Output objects that have identical sets of properties.
    67      private BenchBean outBean = null;
    68      private DynaBean outDyna = null;
    69
    70      // PropertyUtilsBean instance to be used
    71      private PropertyUtilsBean pu = null;
    72
    73
    74      // ---------------------------------------------------- Overall Test Methods
    75
    76
    77      /***
    78       * Set up instance variables required by this test case.
    79       */
    80      public void setUp() throws Exception {
    81
    82          // Set up loop counter (if property specified)
    83          String prop = System.getProperty("counter");
    84          if (prop != null) {
    85              counter = Long.parseLong(prop);
    86          }
    87
    88          // Set up DynaClass for our DynaBean instances
    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         // Create input instances
    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         // Create output instances
    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         // Set up PropertyUtilsBean instance we will use
    130         pu = PropertyUtilsBean.getInstance();
    131
    132     }
    133
    134
    135     /***
    136      * Return the tests included in this test suite.
    137      */
    138     public static Test suite() {
    139
    140         return (new TestSuite(PropertyUtilsBenchCase.class));
    141
    142     }
    143
    144
    145     /***
    146      * Tear down instance variables required by this test case.
    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     // ------------------------------------------------- Individual Test Methods
    163
    164
    165     // Time copyProperties() from a bean
    166     public void testCopyPropertiesBean() throws Exception {
    167
    168         long start;
    169         long stop;
    170
    171         // Bean->Bean
    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         // Bean->Dyna
    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     // Time copyProperties() from a DynaBean
    199     public void testCopyPropertiesDyna() throws Exception {
    200
    201         long start;
    202         long stop;
    203
    204         // Dyna->Bean
    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         // Dyna->Dyna
    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     // Time copyProperties() from a Map
    232     public void testCopyPropertiesMap() throws Exception {
    233
    234         long start;
    235         long stop;
    236
    237         // Dyna->Bean
    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         // Dyna->Dyna
    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     // --------------------------------------------------------- Support Methods
    265
    266
    267 }