public class TimeWindowCounter extends java.lang.Object implements Counter
Counter which keeps track of the increments only over a particular
TimeWindow
, expiring old increments after the window has elapsed. For
example, a period of Uses the resolution of the supplied TimeWindow to group
events together for efficiency. For example, when supplied with a TimeWindow
with a period of 60 seconds and a resolution of 5 seconds, 12 'buckets' will
be created to keep count of events. This means that as each bucket is
overwritten after 60 seconds, the total count may be understated by as much
as (5/60) ≅ 8%. This, however, means that the memory footprint is
approximately that of just the 12 counters, rather than having to track the
time of each individual event.
Currently uses very coarse-grained locking (each get()
or
inc(long)
takes and holds a shared lock for the duration); this may
prove too contentious and require change later.
Modifier and Type | Field and Description |
---|---|
private int |
headIndex |
private long |
headTime |
private long[] |
interimValues |
private java.lang.Object |
lock |
private long |
overallValue |
private com.google.common.base.Supplier<java.lang.Long> |
timeSource |
private TimeWindow |
window |
NULL_COUNTER
Constructor and Description |
---|
TimeWindowCounter(TimeWindow window) |
TimeWindowCounter(TimeWindow window,
com.google.common.base.Supplier<java.lang.Long> timeSource) |
Modifier and Type | Method and Description |
---|---|
private void |
cleanState()
Clean out old data from the buckets, getting us ready to enter a new
bucket.
|
(package private) java.lang.String |
counterState() |
java.lang.Long |
get() |
void |
inc() |
void |
inc(long increment) |
java.lang.String |
toString() |
private long overallValue
private final long[] interimValues
private int headIndex
private long headTime
private final java.lang.Object lock
private final com.google.common.base.Supplier<java.lang.Long> timeSource
private final TimeWindow window
public TimeWindowCounter(TimeWindow window)
TimeWindowCounter(TimeWindow window, com.google.common.base.Supplier<java.lang.Long> timeSource)
private void cleanState()
public java.lang.Long get()
public java.lang.String toString()
toString
in class java.lang.Object
java.lang.String counterState()