Coverage Report - org.mule.util.counters.impl.RatePerUnit
 
Classes in this File Line Coverage Branch Coverage Complexity
RatePerUnit
88%
37/42
71%
17/24
2.714
RatePerUnit$Sample
71%
5/7
N/A
2.714
 
 1  
 /*
 2  
  * $Id: RatePerUnit.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.util.counters.impl;
 12  
 
 13  
 import org.mule.util.counters.CounterFactory.Type;
 14  
 
 15  
 import java.security.InvalidParameterException;
 16  
 import java.util.Iterator;
 17  
 import java.util.LinkedList;
 18  
 
 19  
 public class RatePerUnit extends AggregateCounter
 20  
 {
 21  
 
 22  52
     private static class Sample
 23  
     {
 24  
         private double value;
 25  
         private long time;
 26  
 
 27  
         public Sample(double value, long time)
 28  12
         {
 29  12
             this.value = value;
 30  12
             this.time = time;
 31  12
         }
 32  
 
 33  
         /**
 34  
          * @return the time of the sample
 35  
          */
 36  
         public long getTime()
 37  
         {
 38  0
             return time;
 39  
         }
 40  
 
 41  
         /**
 42  
          * @return the value of the sample
 43  
          */
 44  
         public double getValue()
 45  
         {
 46  0
             return value;
 47  
         }
 48  
     }
 49  
 
 50  
     private final LinkedList samples;
 51  
     private final long unit;
 52  
     private final long length;
 53  
     private final long baseTime;
 54  
 
 55  
     public RatePerUnit(String name, String p, Type type, AbstractCounter base)
 56  
     {
 57  4
         super(name, type, base);
 58  
 
 59  4
         if (type == Type.RATE_PER_SECOND)
 60  
         {
 61  2
             unit = 1000;
 62  
         }
 63  2
         else if (type == Type.RATE_PER_MINUTE)
 64  
         {
 65  2
             unit = 60 * 1000;
 66  
         }
 67  0
         else if (type == Type.RATE_PER_HOUR)
 68  
         {
 69  0
             unit = 60 * 60 * 1000;
 70  
         }
 71  
         else
 72  
         {
 73  0
             throw new InvalidParameterException();
 74  
         }
 75  
 
 76  4
         long newLength = 0;
 77  
 
 78  
         try
 79  
         {
 80  4
             newLength = Long.parseLong(p);
 81  
         }
 82  4
         catch (Exception e)
 83  
         {
 84  4
             newLength = 0;
 85  
         }
 86  
         finally
 87  
         {
 88  4
             if (newLength <= 0)
 89  
             {
 90  4
                 newLength = 128;
 91  
             }
 92  
 
 93  4
             length = newLength;
 94  4
         }
 95  
 
 96  4
         samples = new LinkedList();
 97  4
         baseTime = System.currentTimeMillis();
 98  4
     }
 99  
 
 100  
     public double nextValue()
 101  
     {
 102  6
         if (samples.isEmpty())
 103  
         {
 104  2
             return 0.0;
 105  
         }
 106  
         else
 107  
         {
 108  4
             double total = 0.0;
 109  4
             long current = getTime();
 110  4
             Iterator it = samples.iterator();
 111  4
             Sample sample = null;
 112  16
             while (it.hasNext())
 113  
             {
 114  12
                 sample = (Sample) it.next();
 115  12
                 if (current - sample.time > length)
 116  
                 {
 117  0
                     break;
 118  
                 }
 119  12
                 total += sample.value;
 120  
             }
 121  4
             return total / (1 + current - (sample != null ? sample.time : 0));
 122  
         }
 123  
     }
 124  
 
 125  
     public void doCompute()
 126  
     {
 127  20
         Sample l = samples.isEmpty() ? null : (Sample) samples.getFirst();
 128  20
         long t = getTime();
 129  20
         if (l == null || t > l.time)
 130  
         {
 131  12
             Sample s = new Sample(this.getBase().nextValue(), t);
 132  12
             samples.addFirst(s);
 133  12
         }
 134  
         else
 135  
         {
 136  8
             l.value += getBase().nextValue();
 137  
         }
 138  20
         while (samples.size() > length)
 139  
         {
 140  0
             samples.removeLast();
 141  
         }
 142  20
     }
 143  
 
 144  
     protected long getTime()
 145  
     {
 146  24
         return (System.currentTimeMillis() - baseTime) / unit;
 147  
     }
 148  
 
 149  
 }