more use of the new methods

This commit is contained in:
zab
2012-11-17 19:22:23 +00:00
parent 3cbca7c0ac
commit efc202d2ee
7 changed files with 68 additions and 42 deletions

View File

@ -414,12 +414,21 @@ public class Rate {
return 0.0D;
}
/**
* @return a thread-local temp object containing computed averages.
*/
public RateAverages computeAverages() {
return computeAverages(RateAverages.getTemp(),false);
}
/**
* @param out where to store the computed averages.
* @param useLifetime whether the lifetime average should be used if
* there are no events.
* @return the same RateAverages object for chaining
*/
public synchronized void computeAverages(RateAverages out, boolean useLifetime) {
public synchronized RateAverages computeAverages(RateAverages out, boolean useLifetime) {
out.reset();
final long total = _currentEventCount + _lastEventCount;
out.setTotalEventCount(total);
@ -427,15 +436,17 @@ public class Rate {
if (total <= 0) {
final double avg = useLifetime ? getAvgOrLifetimeAvg() : getAverageValue();
out.setAverage(avg);
return;
} else {
if (_currentEventCount > 0)
out.setCurrent( getCurrentTotalValue() / _currentEventCount );
if (_lastEventCount > 0)
out.setLast( getLastTotalValue() / _lastEventCount );
out.setTotalValues(getCurrentTotalValue() + getLastTotalValue());
out.setAverage( out.getTotalValues() / total );
}
if (_currentEventCount > 0)
out.setCurrent( getCurrentTotalValue() / _currentEventCount );
if (_lastEventCount > 0)
out.setLast( getLastTotalValue() / _lastEventCount );
out.setAverage( ( getCurrentTotalValue() + getLastTotalValue() ) / total );
return out;
}
public synchronized void store(String prefix, StringBuilder buf) throws IOException {

View File

@ -7,14 +7,30 @@ package net.i2p.stat;
*/
public class RateAverages {
private double average, current, last;
/** thread-local temp instance */
private static final ThreadLocal<RateAverages> TEMP =
new ThreadLocal<RateAverages>() {
public RateAverages initialValue() {
return new RateAverages();
}
};
/**
* @return thread-local temp instance.
*/
public static RateAverages getTemp() {
return TEMP.get();
}
private double average, current, last, totalValues;
private long totalEventCount;
public void reset() {
void reset() {
average = 0;
current = 0;
last = 0;
totalEventCount = 0;
totalValues = 0;
}
public double getAverage() {
@ -49,4 +65,12 @@ public class RateAverages {
this.totalEventCount = totalEventCount;
}
public double getTotalValues() {
return totalValues;
}
public void setTotalValues(double totalValues) {
this.totalValues = totalValues;
}
}