Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.codahale.metrics;
  
A meter metric which measures mean throughput and one-, five-, and fifteen-minute exponentially-weighted moving average throughputs.

See also:
EWMA
 
 public class Meter implements Metered {
     private static final long TICK_INTERVAL = ..toNanos(5);
 
     private final EWMA m1Rate = EWMA.oneMinuteEWMA();
     private final EWMA m5Rate = EWMA.fiveMinuteEWMA();
     private final EWMA m15Rate = EWMA.fifteenMinuteEWMA();
 
     private final LongAdder count = new LongAdder();
     private final long startTime;
     private final AtomicLong lastTick;
     private final Clock clock;

    
Creates a new Meter.
 
     public Meter() {
         this(Clock.defaultClock());
     }

    
Creates a new Meter.

Parameters:
clock the clock to use for the meter ticks
 
     public Meter(Clock clock) {
         this. = clock;
         this. = this..getTick();
         this. = new AtomicLong();
     }

    
Mark the occurrence of an event.
 
     public void mark() {
         mark(1);
     }

    
Mark the occurrence of a given number of events.

Parameters:
n the number of events
 
     public void mark(long n) {
         tickIfNecessary();
         .add(n);
         .update(n);
         .update(n);
         .update(n);
     }
 
     private void tickIfNecessary() {
         final long oldTick = .get();
         final long newTick = .getTick();
         final long age = newTick - oldTick;
         if (age > ) {
             final long newIntervalStartTick = newTick - age % ;
             if (.compareAndSet(oldTicknewIntervalStartTick)) {
                 final long requiredTicks = age / ;
                 for (long i = 0; i < requiredTicksi++) {
                     .tick();
                     .tick();
                     .tick();
                 }
             }
         }
     }
 
     @Override
     public long getCount() {
         return .sum();
     }
 
     @Override
     public double getFifteenMinuteRate() {
         tickIfNecessary();
         return .getRate(.);
     }
 
     @Override
     public double getFiveMinuteRate() {
         tickIfNecessary();
         return .getRate(.);
     }
 
     @Override
     public double getMeanRate() {
         if (getCount() == 0) {
             return 0.0;
        } else {
            final double elapsed = (.getTick() - );
            return getCount() / elapsed * ..toNanos(1);
        }
    }
    @Override
    public double getOneMinuteRate() {
        tickIfNecessary();
        return .getRate(.);
    }
New to GrepCode? Check out our FAQ X