move stat bucket up to core api

This commit is contained in:
Jonathan Shook 2024-07-23 11:15:39 -05:00
parent 1468f21d14
commit 3990b42022
5 changed files with 118 additions and 57 deletions

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package io.nosqlbench.scenarios.simframe.stabilization;
package io.nosqlbench.nb.api.stats;
public class DoubleRing {
private final double[] dbuf;
@ -47,4 +47,21 @@ public class DoubleRing {
public int count() {
return count;
}
public double min() {
double min = Double.MAX_VALUE;
for (int i = 0; i < count; i++) {
min = Math.min(min,dbuf[i]);
}
return min;
}
public double max() {
double max = Double.MIN_VALUE;
for (int i = 0; i < count; i++) {
max = Math.max(max,dbuf[i]);
}
return max;
}
}

View File

@ -14,10 +14,16 @@
* limitations under the License.
*/
package io.nosqlbench.scenarios.simframe.stabilization;
package io.nosqlbench.nb.api.stats;
import java.util.Objects;
/**
* This is a relatively efficient statistics bucket which can maintain moving
* aggregates over a window of samples for count, mean, variance, stddev, sum.
* This is particularly useful when you know that each update to the data
* will likely be used in a query.
*/
public final class StatBucket {
DoubleRing ringbuf;
private double mean;
@ -98,10 +104,31 @@ public final class StatBucket {
return "StatBucket[" +
"count=" + ringbuf.count() + ", " +
"mean=" + mean + ", " +
"stddev=" + stddev() + ']';
"stddev=" + stddev() + ", " +
"variance=" + variance() + ']';
}
public boolean primed() {
return this.count()== ringbuf.size();
}
public double getMin() {
return ringbuf.min();
}
public double getMax() {
return ringbuf.max();
}
public double getAverage() {
return this.mean();
}
public double getCount() {
return count();
}
public double getSum() {
return this.mean() * this.count();
}
}

View File

@ -0,0 +1,70 @@
/*
* Copyright (c) 2023 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.nb.api.stats;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class StatBucketTest {
@Test
public void testStreamingMean() {
var bucket = new StatBucket();
bucket.apply(5.0d);
assertThat(bucket.mean()).isCloseTo(5.0d, Offset.offset(0.001d));
bucket.apply(10.0d);
assertThat(bucket.mean()).isCloseTo(7.5d, Offset.offset(0.001d));
bucket.apply(15.0d);
assertThat(bucket.mean()).isCloseTo(10.0d, Offset.offset(0.001d));
bucket.apply(20.0d);
assertThat(bucket.mean()).isCloseTo(12.5d, Offset.offset(0.001d));
}
@Test
public void testStreamingComputations() {
double[] samples = new double[]{2, 4, 4, 4, 5, 5, 7, 9};
var bucket = new StatBucket(8);
for (int i = 0; i < samples.length * 10; i++) {
bucket.apply(samples[i % samples.length]);
if (i > 0 && (i % samples.length) == 0) {
assertThat(bucket.mean()).isCloseTo(5, Offset.offset(0.001d));
assertThat(bucket.stddev()).isCloseTo(2.0, Offset.offset(0.001d));
}
}
}
@Test
public void testErrorAccumulation1() {
var bucket = new StatBucket(11);
for (long base = 1; base <10000000000000000L ; base*=10) {
for (int i = 0; i< 10; i++) {
long value = base+i;
bucket.apply(value);
}
for (int i = 10; i < 20; i++) {
long value = base+i;
bucket.apply(value);
double streamingMean = bucket.mean();
assertThat(streamingMean).isCloseTo((double)(value-5), Offset.offset(0.03d));
}
}
}
}

View File

@ -16,12 +16,12 @@
package io.nosqlbench.scenarios.simframe.stabilization;
import io.nosqlbench.nb.api.stats.StatBucket;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;
import java.util.function.DoubleSupplier;
import java.util.function.Supplier;

View File

@ -1,53 +0,0 @@
/*
* Copyright (c) 2023 nosqlbench
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.nosqlbench.scenarios.simframe.stats;
import io.nosqlbench.scenarios.simframe.stabilization.StatBucket;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class StatBucketTest {
@Test
public void testStreamingMean() {
var bucket = new StatBucket();
bucket.apply(5.0d);
assertThat(bucket.mean()).isCloseTo(5.0d,Offset.offset(0.001d));
bucket.apply(10.0d);
assertThat(bucket.mean()).isCloseTo(7.5d,Offset.offset(0.001d));
bucket.apply(15.0d);
assertThat(bucket.mean()).isCloseTo(10.0d,Offset.offset(0.001d));
bucket.apply(20.0d);
assertThat(bucket.mean()).isCloseTo(12.5d,Offset.offset(0.001d));
}
@Test
public void testStreamingComputations() {
double[] samples = new double[]{2,4,4,4,5,5,7,9};
var bucket = new StatBucket(8);
for (int i = 0; i < samples.length * 10; i++) {
bucket.apply(samples[i%samples.length]);
if (i>0&&(i%samples.length)==0) {
assertThat(bucket.mean()).isCloseTo(5,Offset.offset(0.001d));
assertThat(bucket.stddev()).isCloseTo(2.0,Offset.offset(0.001d));
}
}
}
}