Package com.codahale.metrics
Class ExponentiallyDecayingReservoir
- java.lang.Object
-
- com.codahale.metrics.ExponentiallyDecayingReservoir
-
- All Implemented Interfaces:
Reservoir
public class ExponentiallyDecayingReservoir extends java.lang.Object implements Reservoir
An exponentially-decaying random reservoir oflong
s. Uses Cormode et al's forward-decaying priority reservoir sampling method to produce a statistically representative sampling reservoir, exponentially biased towards newer entries.
-
-
Field Summary
Fields Modifier and Type Field Description private double
alpha
private Clock
clock
private java.util.concurrent.atomic.AtomicLong
count
private static double
DEFAULT_ALPHA
private static int
DEFAULT_SIZE
private java.util.concurrent.locks.ReentrantReadWriteLock
lock
private java.util.concurrent.atomic.AtomicLong
nextScaleTime
private static long
RESCALE_THRESHOLD
private int
size
private long
startTime
private java.util.concurrent.ConcurrentSkipListMap<java.lang.Double,java.lang.Long>
values
-
Constructor Summary
Constructors Constructor Description ExponentiallyDecayingReservoir()
Creates a newExponentiallyDecayingReservoir
of 1028 elements, which offers a 99.9% confidence level with a 5% margin of error assuming a normal distribution, and an alpha factor of 0.015, which heavily biases the reservoir to the past 5 minutes of measurements.ExponentiallyDecayingReservoir(int size, double alpha)
Creates a newExponentiallyDecayingReservoir
.ExponentiallyDecayingReservoir(int size, double alpha, Clock clock)
Creates a newExponentiallyDecayingReservoir
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private long
currentTimeInSeconds()
Snapshot
getSnapshot()
Returns a snapshot of the reservoir's values.private void
lockForRegularUsage()
private void
lockForRescale()
private void
rescale(long now, long next)
private void
rescaleIfNeeded()
int
size()
Returns the number of values recorded.private void
unlockForRegularUsage()
private void
unlockForRescale()
void
update(long value)
Adds a new recorded value to the reservoir.void
update(long value, long timestamp)
Adds an old value with a fixed timestamp to the reservoir.private double
weight(long t)
-
-
-
Field Detail
-
DEFAULT_SIZE
private static final int DEFAULT_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_ALPHA
private static final double DEFAULT_ALPHA
- See Also:
- Constant Field Values
-
RESCALE_THRESHOLD
private static final long RESCALE_THRESHOLD
-
values
private final java.util.concurrent.ConcurrentSkipListMap<java.lang.Double,java.lang.Long> values
-
lock
private final java.util.concurrent.locks.ReentrantReadWriteLock lock
-
alpha
private final double alpha
-
size
private final int size
-
count
private final java.util.concurrent.atomic.AtomicLong count
-
startTime
private volatile long startTime
-
nextScaleTime
private final java.util.concurrent.atomic.AtomicLong nextScaleTime
-
clock
private final Clock clock
-
-
Constructor Detail
-
ExponentiallyDecayingReservoir
public ExponentiallyDecayingReservoir()
Creates a newExponentiallyDecayingReservoir
of 1028 elements, which offers a 99.9% confidence level with a 5% margin of error assuming a normal distribution, and an alpha factor of 0.015, which heavily biases the reservoir to the past 5 minutes of measurements.
-
ExponentiallyDecayingReservoir
public ExponentiallyDecayingReservoir(int size, double alpha)
Creates a newExponentiallyDecayingReservoir
.- Parameters:
size
- the number of samples to keep in the sampling reservoiralpha
- the exponential decay factor; the higher this is, the more biased the reservoir will be towards newer values
-
ExponentiallyDecayingReservoir
public ExponentiallyDecayingReservoir(int size, double alpha, Clock clock)
Creates a newExponentiallyDecayingReservoir
.- Parameters:
size
- the number of samples to keep in the sampling reservoiralpha
- the exponential decay factor; the higher this is, the more biased the reservoir will be towards newer values
-
-
Method Detail
-
size
public int size()
Description copied from interface:Reservoir
Returns the number of values recorded.
-
update
public void update(long value)
Description copied from interface:Reservoir
Adds a new recorded value to the reservoir.
-
update
public void update(long value, long timestamp)
Adds an old value with a fixed timestamp to the reservoir.- Parameters:
value
- the value to be addedtimestamp
- the epoch timestamp ofvalue
in seconds
-
rescaleIfNeeded
private void rescaleIfNeeded()
-
getSnapshot
public Snapshot getSnapshot()
Description copied from interface:Reservoir
Returns a snapshot of the reservoir's values.- Specified by:
getSnapshot
in interfaceReservoir
- Returns:
- a snapshot of the reservoir's values
-
currentTimeInSeconds
private long currentTimeInSeconds()
-
weight
private double weight(long t)
-
rescale
private void rescale(long now, long next)
-
unlockForRescale
private void unlockForRescale()
-
lockForRescale
private void lockForRescale()
-
lockForRegularUsage
private void lockForRegularUsage()
-
unlockForRegularUsage
private void unlockForRegularUsage()
-
-