Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.metrics.core;
  
  
A timer metric which aggregates timing durations and provides duration statistics, plus throughput statistics via Meter.
 
 public class Timer implements MeteredStoppableSamplingSummarizable {
 
     private final TimeUnit durationUnitrateUnit;
     private final Meter meter;
     private final Histogram histogram = new Histogram(.);
     private final Clock clock;

    
Creates a new Timer.

Parameters:
tickThread background thread for updating the rates
durationUnit the scale unit for this timer's duration metrics
rateUnit the scale unit for this timer's rate metrics
 
     Timer(ScheduledExecutorService tickThreadTimeUnit durationUnitTimeUnit rateUnit) {
         this(tickThreaddurationUnitrateUnit, Clock.defaultClock());
     }

    
Creates a new Timer.

Parameters:
tickThread background thread for updating the rates
durationUnit the scale unit for this timer's duration metrics
rateUnit the scale unit for this timer's rate metrics
clock the clock used to calculate duration
 
     Timer(ScheduledExecutorService tickThreadTimeUnit durationUnitTimeUnit rateUnitClock clock) {
         this. = durationUnit;
         this. = rateUnit;
         this. = new Meter(tickThread"calls"rateUnitclock);
         this. = clock;
         clear();
     }

    
Returns the timer's duration scale unit.

Returns:
the timer's duration scale unit
 
     public TimeUnit durationUnit() {
         return ;
     }
 
     @Override
     public TimeUnit rateUnit() {
         return ;
     }

    
Clears all recorded durations.
 
     public void clear() {
         .clear();
     }

    
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 = .tick();
         try {
             return event.call();
         } finally {
             update(.tick() - startTime);
         }
     }

    
Returns a timing TimerContext, which measures an elapsed time in nanoseconds.

Returns:
a new TimerContext
    public TimerContext time() {
        return new TimerContext(this);
    }
    @Override
    public long count() {
        return .count();
    }
    @Override
    public double fifteenMinuteRate() {
        return .fifteenMinuteRate();
    }
    @Override
    public double fiveMinuteRate() {
        return .fiveMinuteRate();
    }
    @Override
    public double meanRate() {
        return .meanRate();
    }
    @Override
    public double oneMinuteRate() {
        return .oneMinuteRate();
    }

    
Returns the longest recorded duration.

Returns:
the longest recorded duration
    @Override
    public double max() {
        return convertFromNS(.max());
    }

    
Returns the shortest recorded duration.

Returns:
the shortest recorded duration
    @Override
    public double min() {
        return convertFromNS(.min());
    }

    
Returns the arithmetic mean of all recorded durations.

Returns:
the arithmetic mean of all recorded durations
    @Override
    public double mean() {
        return convertFromNS(.mean());
    }

    
Returns the standard deviation of all recorded durations.

Returns:
the standard deviation of all recorded durations
    @Override
    public double stdDev() {
        return convertFromNS(.stdDev());
    }

    
Returns the sum of all recorded durations.

Returns:
the sum of all recorded durations
    @Override
    public double sum() {
        return convertFromNS(.sum());
    }
    @Override
    public Snapshot getSnapshot() {
        final double[] values = .getSnapshot().getValues();
        final double[] converted = new double[values.length];
        for (int i = 0; i < values.lengthi++) {
            converted[i] = convertFromNS(values[i]);
        }
        return new Snapshot(converted);
    }
    @Override
    public String eventType() {
        return .eventType();
    }
    private void update(long duration) {
        if (duration >= 0) {
            .update(duration);
            .mark();
        }
    }
    private double convertFromNS(double ns) {
        return ns / ..convert(1, );
    }
    @Override
    public void stop() {
        .stop();
    }
    @Override
    public <T> void processWith(MetricProcessor<T> processorMetricName name, T contextthrows Exception {
        processor.processTimer(namethiscontext);
    }
New to GrepCode? Check out our FAQ X