mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
move stat bucket up to core api
This commit is contained in:
@@ -14,7 +14,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.nosqlbench.scenarios.simframe.stabilization;
|
package io.nosqlbench.nb.api.stats;
|
||||||
|
|
||||||
public class DoubleRing {
|
public class DoubleRing {
|
||||||
private final double[] dbuf;
|
private final double[] dbuf;
|
||||||
@@ -47,4 +47,21 @@ public class DoubleRing {
|
|||||||
public int count() {
|
public int count() {
|
||||||
return 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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -14,10 +14,16 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.nosqlbench.scenarios.simframe.stabilization;
|
package io.nosqlbench.nb.api.stats;
|
||||||
|
|
||||||
import java.util.Objects;
|
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 {
|
public final class StatBucket {
|
||||||
DoubleRing ringbuf;
|
DoubleRing ringbuf;
|
||||||
private double mean;
|
private double mean;
|
||||||
@@ -98,10 +104,31 @@ public final class StatBucket {
|
|||||||
return "StatBucket[" +
|
return "StatBucket[" +
|
||||||
"count=" + ringbuf.count() + ", " +
|
"count=" + ringbuf.count() + ", " +
|
||||||
"mean=" + mean + ", " +
|
"mean=" + mean + ", " +
|
||||||
"stddev=" + stddev() + ']';
|
"stddev=" + stddev() + ", " +
|
||||||
|
"variance=" + variance() + ']';
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean primed() {
|
public boolean primed() {
|
||||||
return this.count()== ringbuf.size();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -16,12 +16,12 @@
|
|||||||
|
|
||||||
package io.nosqlbench.scenarios.simframe.stabilization;
|
package io.nosqlbench.scenarios.simframe.stabilization;
|
||||||
|
|
||||||
|
import io.nosqlbench.nb.api.stats.StatBucket;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.OptionalDouble;
|
|
||||||
import java.util.function.DoubleSupplier;
|
import java.util.function.DoubleSupplier;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
|||||||
@@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user