Exposes RateStatistics#getAccumulatedCount and

synchronizes access to some of the methods.
This commit is contained in:
Boris Grozev 2016-08-11 16:37:36 -05:00
parent 6fe376a56d
commit a21088dc7a

View File

@ -74,7 +74,7 @@ public class RateStatistics
this.scale = scale / (buckets.length - 1); this.scale = scale / (buckets.length - 1);
} }
private void eraseOld(long nowMs) private synchronized void eraseOld(long nowMs)
{ {
long newOldestTime = nowMs - buckets.length + 1; long newOldestTime = nowMs - buckets.length + 1;
@ -102,25 +102,45 @@ public class RateStatistics
oldestTime = newOldestTime; oldestTime = newOldestTime;
} }
public long getRate()
{
return getRate(System.currentTimeMillis());
}
public long getRate(long nowMs) public long getRate(long nowMs)
{ {
eraseOld(nowMs); eraseOld(nowMs);
return (long) (accumulatedCount * scale + 0.5F); 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) public void update(int count, long nowMs)
{ {
if (nowMs < oldestTime) // Too old data is ignored. if (nowMs < oldestTime) // Too old data is ignored.
return; return;
eraseOld(nowMs); synchronized (this)
{
eraseOld(nowMs);
int nowOffset = (int) (nowMs - oldestTime); int nowOffset = (int) (nowMs - oldestTime);
int index = oldestIndex + nowOffset; int index = oldestIndex + nowOffset;
if (index >= buckets.length) if (index >= buckets.length)
index -= buckets.length; index -= buckets.length;
buckets[index] += count; buckets[index] += count;
accumulatedCount += count; accumulatedCount += count;
}
} }
} }