Coverage Report - org.perfidix.meter.MemMeter
 
Classes in this File Line Coverage Branch Coverage Complexity
MemMeter
82%
23/28
41%
5/12
1.857
 
 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.meter;
 28  
 
 29  
 import java.math.BigDecimal;
 30  
 import java.math.MathContext;
 31  
 
 32  
 /**
 33  
  * Meter to bench the amount of memory used by the current Benchmark. Please
 34  
  * note that the results of this meter can only be seen as an approximation.
 35  
  * Because Perfidix-processes are influencing the used memory as well, a small
 36  
  * increasment of used memory is normal. However, because being based only on
 37  
  * the Runtime-class of Java, no extraction of the perfidix processes themselves
 38  
  * is possible. The MemMeter is only usable with an instance of the {@link Memory} enumeration for formatting
 39  
  * purposes. This choose must be made
 40  
  * by instantiation of the meter.
 41  
  * 
 42  
  * @see Memory
 43  
  * @author Sebastian Graf, University of Konstanz
 44  
  */
 45  
 public final class MemMeter extends AbstractMeter {
 46  
 
 47  
     /** Name of the Meter. */
 48  
     private static final String NAME = "MemMeter";
 49  
 
 50  
     /** Amount of already used memory. */
 51  
     private transient double memAlreadyUsed;
 52  
 
 53  
     /** Scale of memory. */
 54  
     private transient final Memory scale;
 55  
 
 56  
     /**
 57  
      * Constructor.
 58  
      * 
 59  
      * @param paramScale
 60  
      *            scale for this meter, can be any instance of Memory-enum
 61  
      */
 62  
     public MemMeter(final Memory paramScale) {
 63  55
         super();
 64  55
         memAlreadyUsed = 0;
 65  55
         this.scale = paramScale;
 66  55
     }
 67  
 
 68  
     /**
 69  
      * {@inheritDoc}
 70  
      */
 71  
     @Override
 72  
     public double getValue() {
 73  6115
         final Runtime runtime = Runtime.getRuntime();
 74  6115
         memAlreadyUsed = memAlreadyUsed + runtime.totalMemory() - runtime.freeMemory();
 75  6115
         return new BigDecimal(memAlreadyUsed, MathContext.DECIMAL128).divide(
 76  
             new BigDecimal(scale.getNumberOfBytes()), MathContext.DECIMAL128).doubleValue();
 77  
     }
 78  
 
 79  
     /** {@inheritDoc} */
 80  
     @Override
 81  
     public String getName() {
 82  5
         return NAME;
 83  
     }
 84  
 
 85  
     /** {@inheritDoc} */
 86  
     @Override
 87  
     public String getUnit() {
 88  65
         return scale.getUnit();
 89  
     }
 90  
 
 91  
     /** {@inheritDoc} */
 92  
     @Override
 93  
     public String getUnitDescription() {
 94  15
         return scale.getUnitDescription();
 95  
     }
 96  
 
 97  
     /** {@inheritDoc} */
 98  
     @Override
 99  
     public int hashCode() {
 100  19044
         final int prime = 31;
 101  19044
         int result = prime;
 102  19044
         if (scale == null) {
 103  0
             result = prime * result;
 104  
         } else {
 105  19044
             result = prime * result + scale.hashCode();
 106  
         }
 107  
 
 108  19044
         return result;
 109  
     }
 110  
 
 111  
     /** {@inheritDoc} */
 112  
     @Override
 113  
     public boolean equals(final Object obj) {
 114  18929
         boolean returnVal = true;
 115  18929
         if (this == obj) {
 116  18929
             returnVal = true;
 117  
         }
 118  18929
         if (getClass() != obj.getClass()) {
 119  0
             returnVal = false;
 120  
         }
 121  18929
         final MemMeter other = (MemMeter)obj;
 122  18929
         if (scale == null) {
 123  0
             if (other.scale != null) {
 124  0
                 returnVal = false;
 125  
             }
 126  
         } else {
 127  18929
             if (!scale.equals(other.scale)) {
 128  0
                 returnVal = false;
 129  
             }
 130  
         }
 131  18929
         return returnVal;
 132  
     }
 133  
 
 134  
 }