Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.codahale.metrics;
  
  import java.io.Closeable;
A timer metric which aggregates timing durations and provides duration statistics, plus throughput statistics via Meter.
 
 public class Timer implements MeteredSampling {
    
A timing context.

See also:
Timer.time()
 
     public static class Context implements Closeable {
         private final Timer timer;
         private final Clock clock;
         private final long startTime;
 
         private Context(Timer timerClock clock) {
             this. = timer;
             this. = clock;
             this. = clock.getTick();
         }

        
Stops recording the elapsed time, updates the timer and returns the elapsed time in nanoseconds.
 
         public long stop() {
             final long elapsed = .getTick() - ;
             .update(elapsed.);
             return elapsed;
         }
 
         @Override
         public void close() {
             stop();
         }
     }
 
     private final Meter meter;
     private final Histogram histogram;
     private final Clock clock;

    
Creates a new Timer using an ExponentiallyDecayingReservoir and the default Clock.
 
     public Timer() {
         this(new ExponentiallyDecayingReservoir());
     }

    
Creates a new Timer that uses the given Reservoir.

Parameters:
reservoir the Reservoir implementation the timer should use
 
     public Timer(Reservoir reservoir) {
         this(reservoir, Clock.defaultClock());
     }

    
Creates a new Timer that uses the given Reservoir and Clock.

Parameters:
reservoir the Reservoir implementation the timer should use
clock the Clock implementation the timer should use
 
     public Timer(Reservoir reservoirClock clock) {
         this. = new Meter(clock);
         this. = clock;
         this. = new Histogram(reservoir);
     }

    
Adds a recorded duration.

Parameters:
duration the length of the duration
unit the scale unit of duration
 
     public void update(long durationTimeUnit unit) {
         update(unit.toNanos(duration));
     }

    
Times and records the duration of event.

Parameters:
event a java.util.concurrent.Callable whose java.util.concurrent.Callable.call() method implements a process whose duration should be timed
<T> the type of the value returned by event
Returns:
the value returned by event
Throws:
java.lang.Exception if event throws an java.lang.Exception
 
     public <T> T time(Callable<T> eventthrows Exception {
         final long startTime = .getTick();
         try {
             return event.call();
        } finally {
            update(.getTick() - startTime);
        }
    }

    
Returns a new Timer.Context.

Returns:
a new Timer.Context
See also:
Timer.Context
    public Context time() {
        return new Context(this);
    }
    @Override
    public long getCount() {
        return .getCount();
    }
    @Override
    public double getFifteenMinuteRate() {
        return .getFifteenMinuteRate();
    }
    @Override
    public double getFiveMinuteRate() {
        return .getFiveMinuteRate();
    }
    @Override
    public double getMeanRate() {
        return .getMeanRate();
    }
    @Override
    public double getOneMinuteRate() {
        return .getOneMinuteRate();
    }
    @Override
    public Snapshot getSnapshot() {
        return .getSnapshot();
    }
    private void update(long duration) {
        if (duration >= 0) {
            .update(duration);
            .mark();
        }
    }
New to GrepCode? Check out our FAQ X