Skip to content

Commit

Permalink
Average now uses long[] as samples window
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Dec 19, 2024
1 parent 44dcec9 commit 1345b9c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 34 deletions.
30 changes: 13 additions & 17 deletions src/org/jgroups/util/Average.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @since 3.4, 5.4
*/
public class Average implements Streamable {
protected double[] samples;
protected long[] samples;
protected TimeUnit unit;
protected volatile boolean all_filled;
protected static final int DEFAULT_CAPACITY=512;
Expand All @@ -27,27 +27,23 @@ public Average() {
}

public Average(final int capacity) {
this.samples=new double[capacity];
Arrays.fill(samples, -1.0);
this.samples=new long[capacity];
Arrays.fill(samples, -1);
}

public int capacity() {return samples.length;}
public TimeUnit unit() {return unit;}
public <T extends Average> T unit(TimeUnit u) {this.unit=u; return (T)this;}
public double getAverage() {return average();}

public <T extends Average> T add(double num) {
public <T extends Average> T add(long num) {
if(num < 0)
return (T)this;
int idx=Util.random(samples.length)-1;
samples[idx]=num;
return (T)this;
}

public <T extends Average> T add(long num) {
return add((double)num);
}

/** Merges this average with another one */
public <T extends Average> T merge(T other) {
if(other == null)
Expand All @@ -58,10 +54,10 @@ public <T extends Average> T merge(T other) {
}

/** Returns the total of all valid (>= 0) values */
public double total() {
double ret=0.0;
public long total() {
long ret=0;
for(int i=0; i < samples.length; i++) {
double sample=samples[i];
long sample=samples[i];
if(sample >= 0)
ret+=sample;
}
Expand Down Expand Up @@ -96,17 +92,17 @@ public boolean isEmpty() {
/** Returns the average of all valid samples divided by the number of valid samples */
public double average() {
int count=0;
double total=0.0;
long total=0;
for(int i=0; i < samples.length; i++) {
double sample=samples[i];
long sample=samples[i];
if(sample >= 0) {
count++;
total+=sample;
}
}
if(count == 0)
return 0.0;
return total / count;
return total / (double)count;
}

public void clear() {
Expand All @@ -128,15 +124,15 @@ public String toString(TimeUnit u) {
public void writeTo(DataOutput out) throws IOException {
out.writeInt(samples.length);
for(int i=0; i < samples.length; i++)
Bits.writeDouble(samples[i], out);
Bits.writeLongCompressed(samples[i], out);
}

@Override
public void readFrom(DataInput in) throws IOException {
int len=in.readInt();
samples=new double[len];
samples=new long[len];
for(int i=0; i < samples.length; i++)
samples[i]=Bits.readDouble(in);
samples[i]=Bits.readLongCompressed(in);
}

}
28 changes: 12 additions & 16 deletions src/org/jgroups/util/AverageMinMax.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* @since 4.0, 3.6.10
*/
public class AverageMinMax extends Average {
protected double min=Double.MAX_VALUE, max=0;
protected List<Double> values;
protected long min=Long.MAX_VALUE, max=0;
protected List<Long> values;
protected volatile boolean sorted;

public AverageMinMax() {
Expand All @@ -27,13 +27,13 @@ public AverageMinMax(int capacity) {
super(capacity);
}

public double min() {return min;}
public double max() {return max;}
public long min() {return min;}
public long max() {return max;}
public boolean usePercentiles() {return values != null;}
public AverageMinMax usePercentiles(int cap) {values=cap > 0? new ArrayList<>(cap) : null; return this;}
public List<Double> values() {return values;}
public List<Long> values() {return values;}

public <T extends Average> T add(double num) {
public <T extends Average> T add(long num) {
if(num < 0)
return (T)this;
super.add(num);
Expand All @@ -46,10 +46,6 @@ public <T extends Average> T add(double num) {
return (T)this;
}

public <T extends Average> T add(long num) {
return add((double)num);
}

public <T extends Average> T merge(T other) {
if(other.count() == 0)
return (T)this;
Expand All @@ -70,7 +66,7 @@ public void clear() {
super.clear();
if(values != null)
values.clear();
min=Double.MAX_VALUE; max=0;
min=Long.MAX_VALUE; max=0;
}

public String percentiles() {
Expand All @@ -92,7 +88,7 @@ public String toString() {
return count() == 0? "n/a" :
unit != null? toString(unit) :
String.format("min/avg/max=%.2f/%.2f/%.2f%s",
min, average(), max, unit == null? "" : " " + Util.suffix(unit));
(double)min, average(), (double)max, unit == null? "" : " " + Util.suffix(unit));
}

public String toString(TimeUnit u) {
Expand All @@ -103,14 +99,14 @@ public String toString(TimeUnit u) {

public void writeTo(DataOutput out) throws IOException {
super.writeTo(out);
Bits.writeDouble(min, out);
Bits.writeDouble(max, out);
Bits.writeLongCompressed(min, out);
Bits.writeLongCompressed(max, out);
}

public void readFrom(DataInput in) throws IOException {
super.readFrom(in);
min=Bits.readDouble(in);
max=Bits.readDouble(in);
min=Bits.readLongCompressed(in);
max=Bits.readLongCompressed(in);
}

public double percentile(double percentile) {
Expand Down
3 changes: 2 additions & 1 deletion tests/junit-functional/org/jgroups/tests/AverageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ public void testAverage() {
assert Util.withinRange(avg, expected_avg, DEVIATION);
}

// @Test(invocationCount=100,successPercentage=50)
public void testAverage2() {
Average avg=new Average(16);
double expected_avg=IntStream.rangeClosed(1, 16).sum() / 16.0;
IntStream.rangeClosed(1,16).forEach(avg::add);
double actual_avg=avg.average();
assert Util.withinRange(actual_avg, expected_avg, DEVIATION) : String.format("actual: %.2f expected: %.2f\n", actual_avg, expected_avg);
assert Util.withinRange(actual_avg, expected_avg, 0.5) : String.format("actual: %.2f expected: %.2f\n", actual_avg, expected_avg);
}


Expand Down

0 comments on commit 1345b9c

Please sign in to comment.