Coverage Report - org.perfidix.element.SequentialMethodArrangement
 
Classes in this File Line Coverage Branch Coverage Complexity
SequentialMethodArrangement
100%
35/35
100%
18/18
4.333
SequentialMethodArrangement$BenchmarkElementComparator
100%
6/6
100%
2/2
4.333
 
 1  
 /**
 2  
  * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
 3  
  * All rights reserved.
 4  
  * 
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  * * Redistributions of source code must retain the above copyright
 8  
  * notice, this list of conditions and the following disclaimer.
 9  
  * * Redistributions in binary form must reproduce the above copyright
 10  
  * notice, this list of conditions and the following disclaimer in the
 11  
  * documentation and/or other materials provided with the distribution.
 12  
  * * Neither the name of the University of Konstanz nor the
 13  
  * names of its contributors may be used to endorse or promote products
 14  
  * derived from this software without specific prior written permission.
 15  
  * 
 16  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 17  
  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 18  
  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 19  
  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 20  
  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 21  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 22  
  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 23  
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 24  
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 25  
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 26  
  */
 27  
 package org.perfidix.element;
 28  
 
 29  
 import java.util.ArrayList;
 30  
 import java.util.Comparator;
 31  
 import java.util.Hashtable;
 32  
 import java.util.List;
 33  
 import java.util.Map;
 34  
 import java.util.Map.Entry;
 35  
 import java.util.Set;
 36  
 import java.util.TreeSet;
 37  
 
 38  
 /**
 39  
  * This class represents an arrangement where each method is executed after
 40  
  * another. That means that <br/>
 41  
  * <code>
 42  
  * 
 43  
  * &#064;Bench(runs=3) public bench1(){ .. }<br/>
 44  
  * &#064;Bench(runs=2) public bench2(){ .. }<br/>
 45  
  * &#064;Bench(runs=1) public bench3(){ .. }<br/> </code><br/>
 46  
  * results in the following execution order:<br/>
 47  
  * <code>
 48  
  * bench1
 49  
  * bench2
 50  
  * bench3
 51  
  * bench1
 52  
  * bench2
 53  
  * bench1
 54  
  * </code>
 55  
  */
 56  
 public final class SequentialMethodArrangement extends AbstractMethodArrangement {
 57  
 
 58  
     /**
 59  
      * Simple Constructor-
 60  
      * 
 61  
      * @param elements
 62  
      */
 63  
     protected SequentialMethodArrangement(final List<BenchmarkElement> elements) {
 64  10
         super(elements);
 65  10
     }
 66  
 
 67  
     /** {@inheritDoc} */
 68  
     @Override
 69  
     protected List<BenchmarkElement> arrangeList(final List<BenchmarkElement> elements) {
 70  10
         final Map<BenchmarkMethod, ArrayList<BenchmarkElement>> table =
 71  
             new Hashtable<BenchmarkMethod, ArrayList<BenchmarkElement>>();
 72  10
         final List<BenchmarkElement> returnVal = new ArrayList<BenchmarkElement>();
 73  
 
 74  
         // Having a table
 75  10
         for (final BenchmarkElement elem : elements) {
 76  3080
             if (!table.containsKey(elem.getMeth())) {
 77  50
                 table.put(elem.getMeth(), new ArrayList<BenchmarkElement>());
 78  
             }
 79  3080
             table.get(elem.getMeth()).add(elem);
 80  3080
         }
 81  
 
 82  
         // Computing complete number of elements
 83  10
         int numberOfElements = 0;
 84  10
         for (final BenchmarkMethod meth : table.keySet()) {
 85  50
             numberOfElements = numberOfElements + table.get(meth).size();
 86  50
         }
 87  
 
 88  
         // Defining order to execute, start with the one with the most elements
 89  10
         final Set<Entry<BenchmarkMethod, ArrayList<BenchmarkElement>>> compareMethods =
 90  
             new TreeSet<Entry<BenchmarkMethod, ArrayList<BenchmarkElement>>>(new BenchmarkElementComparator());
 91  10
         for (final Entry<BenchmarkMethod, ArrayList<BenchmarkElement>> entry : table.entrySet()) {
 92  50
             compareMethods.add(entry);
 93  50
         }
 94  
 
 95  10
         final ArrayList<BenchmarkMethod> methods = new ArrayList<BenchmarkMethod>();
 96  10
         for (Entry<BenchmarkMethod, ArrayList<BenchmarkElement>> entry : compareMethods) {
 97  50
             methods.add(entry.getKey());
 98  50
         }
 99  
 
 100  3090
         for (int i = 0; i < numberOfElements; i++) {
 101  3080
             BenchmarkElement elem = null;
 102  3080
             int indexPart = 0;
 103  7525
             while (elem == null) {
 104  4445
                 final int index = (i + indexPart) % methods.size();
 105  4445
                 final BenchmarkMethod methodToInclude = methods.get(index);
 106  4445
                 if (table.containsKey(methodToInclude)) {
 107  3080
                     elem = table.get(methodToInclude).remove(0);
 108  3080
                     if (table.get(methodToInclude).size() == 0) {
 109  50
                         table.remove(methodToInclude);
 110  
                     }
 111  
                 }
 112  4445
                 indexPart++;
 113  4445
             }
 114  3080
             returnVal.add(elem);
 115  
         }
 116  10
         return returnVal;
 117  
     }
 118  
 
 119  
     /**
 120  
      * Comparator to compare the different entries according to the size of the
 121  
      * underlaying arraylist
 122  
      * 
 123  
      * @author Sebastian Graf, University of Konstanz
 124  
      */
 125  110
     class BenchmarkElementComparator implements
 126  
         Comparator<Entry<BenchmarkMethod, ArrayList<BenchmarkElement>>> {
 127  
 
 128  
         /** {@inheritDoc} */
 129  
         public int compare(final Entry<BenchmarkMethod, ArrayList<BenchmarkElement>> object1,
 130  
             final Entry<BenchmarkMethod, ArrayList<BenchmarkElement>> object2) {
 131  100
             int returnVal = 0;
 132  100
             if (object1.getValue().size() > object2.getValue().size()) {
 133  45
                 returnVal = -1;
 134  
             } else {
 135  55
                 returnVal = 1;
 136  
             }
 137  100
             return returnVal;
 138  
         }
 139  
 
 140  
     }
 141  
 
 142  
 }