mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-25 18:55:28 -06:00
make TriangularStep ctor public
This commit is contained in:
parent
457c75bc3e
commit
862058d80f
@ -38,7 +38,7 @@ import java.util.function.LongUnaryOperator;
|
|||||||
* join the project or let us know.</p>
|
* join the project or let us know.</p>
|
||||||
*/
|
*/
|
||||||
@ThreadSafeMapper
|
@ThreadSafeMapper
|
||||||
public class TriangularStepFunction implements LongUnaryOperator {
|
public class TriangularStep implements LongUnaryOperator {
|
||||||
|
|
||||||
private final Hash hasher = new Hash();
|
private final Hash hasher = new Hash();
|
||||||
private final long average;
|
private final long average;
|
||||||
@ -47,9 +47,9 @@ public class TriangularStepFunction implements LongUnaryOperator {
|
|||||||
private final long variance;
|
private final long variance;
|
||||||
|
|
||||||
|
|
||||||
@Example({"TriangularStepFunction(100,20)","Create a sequence of values where the average is 100, but the range of values is between 80 and 120."})
|
@Example({"TriangularStep(100,20)","Create a sequence of values where the average is 100, but the range of values is between 80 and 120."})
|
||||||
@Example({"TriangularStepFunction(80,10)","Create a sequence of values where the average is 80, but the range of values is between 70 and 90."})
|
@Example({"TriangularStep(80,10)","Create a sequence of values where the average is 80, but the range of values is between 70 and 90."})
|
||||||
TriangularStepFunction(long average, long variance) {
|
public TriangularStep(long average, long variance) {
|
||||||
if (average<=0 || variance < 0 || variance > average) {
|
if (average<=0 || variance < 0 || variance > average) {
|
||||||
throw new RuntimeException(
|
throw new RuntimeException(
|
||||||
"The average must be positive, variance non-negative and the variance must be less than the average. " +
|
"The average must be positive, variance non-negative and the variance must be less than the average. " +
|
||||||
@ -61,7 +61,7 @@ public class TriangularStepFunction implements LongUnaryOperator {
|
|||||||
this.sizer = new HashRange(average-variance,average+variance);
|
this.sizer = new HashRange(average-variance,average+variance);
|
||||||
}
|
}
|
||||||
|
|
||||||
TriangularStepFunction(long average) {
|
TriangularStep(long average) {
|
||||||
this(average, average/2);
|
this(average, average/2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,9 +71,9 @@ public class TriangularStepFunction implements LongUnaryOperator {
|
|||||||
long count = operand / average;
|
long count = operand / average;
|
||||||
// offset within window
|
// offset within window
|
||||||
long offset = operand % average;
|
long offset = operand % average;
|
||||||
// base of window
|
// base of window, same as count*average
|
||||||
long base = operand - offset;
|
long base = operand - offset;
|
||||||
// variate up to window size
|
// variate + or - from average
|
||||||
long variance = sizer.applyAsLong(base);
|
long variance = sizer.applyAsLong(base);
|
||||||
// variate offset from start of window
|
// variate offset from start of window
|
||||||
long slice = base + variance;
|
long slice = base + variance;
|
@ -25,14 +25,14 @@ import java.util.stream.IntStream;
|
|||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class TriangularStepFunctionTest {
|
public class TriangularStepTest {
|
||||||
|
|
||||||
private static final int LABEL=0;
|
private static final int LABEL=0;
|
||||||
private static final int FREQUENCY=1;
|
private static final int FREQUENCY=1;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExample1() {
|
public void testStepExample1() {
|
||||||
TriangularStepFunction e1 = new TriangularStepFunction(100, 20);
|
TriangularStep e1 = new TriangularStep(100, 20);
|
||||||
int[] runLengths = this.rleStatsFor(e1, 0, 10000);
|
int[] runLengths = this.rleStatsFor(e1, 0, 10000);
|
||||||
System.out.println(Arrays.toString(runLengths));
|
System.out.println(Arrays.toString(runLengths));
|
||||||
assertThat(IntStream.of(runLengths).min().orElseThrow()).isEqualTo(80L);
|
assertThat(IntStream.of(runLengths).min().orElseThrow()).isEqualTo(80L);
|
||||||
@ -40,8 +40,8 @@ public class TriangularStepFunctionTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExample2() {
|
public void testStepExample2() {
|
||||||
TriangularStepFunction e1 = new TriangularStepFunction(80, 10);
|
TriangularStep e1 = new TriangularStep(80, 10);
|
||||||
int[] runLengths = this.rleStatsFor(e1, 0, 10000);
|
int[] runLengths = this.rleStatsFor(e1, 0, 10000);
|
||||||
System.out.println(Arrays.toString(runLengths));
|
System.out.println(Arrays.toString(runLengths));
|
||||||
assertThat(IntStream.of(runLengths).min().orElseThrow()).isEqualTo(70L);
|
assertThat(IntStream.of(runLengths).min().orElseThrow()).isEqualTo(70L);
|
||||||
@ -50,7 +50,7 @@ public class TriangularStepFunctionTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIncrementalVariance() {
|
public void testIncrementalVariance() {
|
||||||
TriangularStepFunction f = new TriangularStepFunction(100, 0);
|
TriangularStep f = new TriangularStep(100, 0);
|
||||||
assertThat(f.applyAsLong(0L)).isEqualTo(0L);
|
assertThat(f.applyAsLong(0L)).isEqualTo(0L);
|
||||||
assertThat(f.applyAsLong(1L)).isEqualTo(0L);
|
assertThat(f.applyAsLong(1L)).isEqualTo(0L);
|
||||||
assertThat(f.applyAsLong(99L)).isEqualTo(0L);
|
assertThat(f.applyAsLong(99L)).isEqualTo(0L);
|
||||||
@ -60,7 +60,7 @@ public class TriangularStepFunctionTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testVariance() {
|
public void testVariance() {
|
||||||
long first=0;
|
long first=0;
|
||||||
TriangularStepFunction f = new TriangularStepFunction(100,1);
|
TriangularStep f = new TriangularStep(100,1);
|
||||||
var rlestats = rleStatsFor(f, 0, 100000);
|
var rlestats = rleStatsFor(f, 0, 100000);
|
||||||
LongSummaryStatistics stats99to101 = statsForRle((int) f.applyAsLong(first),rlestats);
|
LongSummaryStatistics stats99to101 = statsForRle((int) f.applyAsLong(first),rlestats);
|
||||||
assertThat(stats99to101.getMin()).isEqualTo(99L);
|
assertThat(stats99to101.getMin()).isEqualTo(99L);
|
||||||
@ -74,7 +74,7 @@ public class TriangularStepFunctionTest {
|
|||||||
assertThat(histoStats.getAverage()).isEqualTo(100);
|
assertThat(histoStats.getAverage()).isEqualTo(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int[] rleStatsFor(TriangularStepFunction f, long firstTrialIncl, long lastTrialExcl) {
|
private int[] rleStatsFor(TriangularStep f, long firstTrialIncl, long lastTrialExcl) {
|
||||||
long firstBucket = f.applyAsLong(firstTrialIncl);
|
long firstBucket = f.applyAsLong(firstTrialIncl);
|
||||||
long lastBucket = f.applyAsLong(lastTrialExcl);
|
long lastBucket = f.applyAsLong(lastTrialExcl);
|
||||||
if (firstBucket>Integer.MAX_VALUE||lastBucket>Integer.MAX_VALUE) {
|
if (firstBucket>Integer.MAX_VALUE||lastBucket>Integer.MAX_VALUE) {
|
Loading…
Reference in New Issue
Block a user