Merge pull request #77 from bgrozev/statistics

Exposes RateStatistics#getAccumulatedCount
This commit is contained in:
George Politis 2016-08-17 13:30:12 -05:00 committed by GitHub
commit d6c4c62689

View File

@ -74,7 +74,7 @@ public class RateStatistics
this.scale = scale / (buckets.length - 1);
}
private void eraseOld(long nowMs)
private synchronized void eraseOld(long nowMs)
{
long newOldestTime = nowMs - buckets.length + 1;
@ -102,17 +102,36 @@ public class RateStatistics
oldestTime = newOldestTime;
}
public long getRate()
{
return getRate(System.currentTimeMillis());
}
public long getRate(long nowMs)
{
eraseOld(nowMs);
return (long) (accumulatedCount * scale + 0.5F);
}
public long getAccumulatedCount()
{
return getAccumulatedCount(System.currentTimeMillis());
}
public long getAccumulatedCount(long nowMs)
{
eraseOld(nowMs);
return accumulatedCount;
}
public void update(int count, long nowMs)
{
if (nowMs < oldestTime) // Too old data is ignored.
return;
synchronized (this)
{
eraseOld(nowMs);
int nowOffset = (int) (nowMs - oldestTime);
@ -124,3 +143,4 @@ public class RateStatistics
accumulatedCount += count;
}
}
}