findmax improvements

This commit is contained in:
Jonathan Shook
2023-12-15 23:10:41 -06:00
parent 84bc7bd4fb
commit 06ef405b63
6 changed files with 45 additions and 17 deletions

View File

@@ -20,6 +20,7 @@ import io.nosqlbench.scenarios.simframe.stabilization.StabilityDetector;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.function.*;
@@ -39,7 +40,7 @@ public class SimFrameCapture implements SimFrameResults {
private final StabilityDetector stabilizer;
public SimFrameCapture() {
stabilizer = new StabilityDetector(0.1,0.98,this::getPartialValue, 10,5);
stabilizer = new StabilityDetector(0.1,0.98,this::getPartialValue, this::toString, 10,5);
}
private double getPartialValue() {
@@ -138,10 +139,11 @@ public class SimFrameCapture implements SimFrameResults {
@Override
public double getValue() {
if (allFrames.isEmpty()) {
return Double.NaN;
FrameSampleSet lastFrame = allFrames.peekLast();
if (lastFrame==null) {
System.out.println("no last frame");
}
return allFrames.getLast().value();
return lastFrame != null ? lastFrame.value() : Double.NaN;
}
@Override
@@ -152,7 +154,8 @@ public class SimFrameCapture implements SimFrameResults {
@Override
public String toString() {
StringBuilder sb = new StringBuilder("PERF VALUE=").append(getValue()).append("\n");
sb.append("windows:\n").append(allFrames.getLast().toString());
FrameSampleSet lastFrame = allFrames.peekLast();
sb.append("windows:\n").append(lastFrame==null ? "NONE" : lastFrame.toString());
return sb.toString();
}
@@ -175,6 +178,7 @@ public class SimFrameCapture implements SimFrameResults {
throw new RuntimeException("cant start window twice in a row. Must close window first");
}
restartWindow(now);
// System.out.println("after (re)start):\n"+ frameCaptureSummary(activeFrame));
}
private String frameCaptureSummary(FrameSampleSet currentFrame) {
@@ -195,7 +199,7 @@ public class SimFrameCapture implements SimFrameResults {
activeFrame.set(i, activeFrame.get(i).stop(now));
}
allFrames.add(activeFrame);
// System.out.println("after stop:\n"+ frameCaptureSummary(currentFrame));
// System.out.println("after stop:\n"+ frameCaptureSummary(activeFrame));
activeFrame = null;
}
@@ -212,7 +216,7 @@ public class SimFrameCapture implements SimFrameResults {
}
public static class FrameSamples extends ArrayList<FrameSampleSet> {
public static class FrameSamples extends LinkedList<FrameSampleSet> {
}
}

View File

@@ -69,7 +69,7 @@ public class FindmaxRatchet extends SimFramePlanner<RatchetConfig, RatchetFrameP
double newStepSize = best.params().step_size() * config.rate_scaledown();
return new RatchetFrameParams(
best.params().rate() + newStepSize, best.params().attempt(), newStepSize,
"SMALLER-STEP: " + newStepSize + " from fram " + best.index()
"SMALLER-STEP: " + newStepSize + " from frame " + best.index()
);
} else if (last.params().attempt() < config.max_attempts()) {
return new RatchetFrameParams(

View File

@@ -38,8 +38,8 @@ public record RatchetConfig(
params.maybeGet("sample_max").map(Integer::parseInt).orElse(10000),
params.maybeGet("sample_incr").map(Double::parseDouble).orElse(1.2d),
params.maybeGet("rate_base").map(Double::parseDouble).orElse(5d),
params.maybeGet("rate_step").map(Double::parseDouble).orElse(1000d),
params.maybeGet("rate_minstep").map(Double::parseDouble).orElse(1000d),
params.maybeGet("rate_step").map(Double::parseDouble).orElse(10000d),
params.maybeGet("rate_minstep").map(Double::parseDouble).orElse(10000d),
params.maybeGet("rate_scaledown").map(Double::parseDouble).orElse(0.25),
params.maybeGet("max_attempts").map(Integer::parseInt).orElse(3)
);

View File

@@ -70,10 +70,10 @@ public abstract class SimFramePlanner<C,P extends Record> {
applyParams(frameParams,flywheel);
capture.startWindow();
capture.awaitSteadyState();
applyParams(frameParams,flywheel);
capture.restartWindow();
// controller.waitMillis(500);
capture.awaitSteadyState();
// applyParams(frameParams,flywheel);
// capture.restartWindow();
//// controller.waitMillis(500);
// capture.awaitSteadyState();
capture.stopWindow();
journal.record(frameParams, capture.last());
stdout.println(capture.last());

View File

@@ -23,12 +23,14 @@ import java.util.Arrays;
import java.util.List;
import java.util.OptionalDouble;
import java.util.function.DoubleSupplier;
import java.util.function.Supplier;
public class StabilityDetector implements Runnable {
private final static Logger logger = LogManager.getLogger(StabilityDetector.class);
private final double timeSliceSeconds;
private final double threshold;
private final DoubleSupplier source;
private final Supplier<String> summary;
private StatBucket[] buckets;
private int[] windows;
private volatile boolean running = true;
@@ -53,13 +55,20 @@ public class StabilityDetector implements Runnable {
* The size of each window in the set of diminishing sizes. These contain the last N samples by size,
* respectively.
*/
public StabilityDetector(double timeSliceSeconds, double minThreshold, DoubleSupplier source, int... windows) {
public StabilityDetector(
double timeSliceSeconds,
double minThreshold,
DoubleSupplier source,
Supplier<String> summary,
int... windows
) {
if (windows.length < 2) {
throw new RuntimeException("you must provide at least to summarization windows, ordered in decreasing size.");
}
this.timeSliceSeconds = timeSliceSeconds;
this.threshold = minThreshold;
this.source = source;
this.summary = summary;
this.windows = windows;
for (int i = 0; i < windows.length - 1; i++) {
if (windows[i] < windows[i + 1]) {
@@ -122,6 +131,8 @@ public class StabilityDetector implements Runnable {
for (int i = 0; i < stddev.length; i++) {
System.out.printf("[%d]: %g ", windows[i], stddev[i]);
}
System.out.println("stddevs: "+ Arrays.toString(stddev));
System.out.printf(this.summary.get());
System.out.println();
}
return basis;
@@ -133,6 +144,18 @@ public class StabilityDetector implements Runnable {
*/
@Override
public void run() {
try {
// System.out.println("Detector> OPEN");
updateAndAwait();
} catch (Exception e) {
// System.out.println("Detector> ERROR ERROR:" + e.toString());
throw new RuntimeException(e);
} finally {
// System.out.println("Detector> CLOSE");
}
}
private void updateAndAwait() {
int interval = (int) (this.timeSliceSeconds * 1000);
startedAt = System.currentTimeMillis();
reset();
@@ -145,10 +168,10 @@ public class StabilityDetector implements Runnable {
try {
Thread.sleep(delay);
} catch (InterruptedException ignored) {
System.out.println("Interrupted>");
}
delay = nextCheckAt - System.currentTimeMillis();
}
double value = source.getAsDouble();
apply(value);
double stabilityFactor = computeStability();
@@ -164,7 +187,7 @@ public class StabilityDetector implements Runnable {
}
}
private static String levels8 = " ▁▂▃▄▅▆▇";
private static final String levels8 = " ▁▂▃▄▅▆▇";
public String stabilitySummary(double[] stddev) {
StringBuilder sb = new StringBuilder("[");
double bias=(1.0d/16.0);

View File

@@ -35,6 +35,7 @@ public final class StatBucket {
}
public StatBucket apply(double value) {
// System.out.println("stat->" + value + " bucket:" + toString());
double popped = ringbuf.push(value);
if (ringbuf.count() == 1) {
mean = value;