Skip to content

Commit

Permalink
Replace linked lists in LSC averaging
Browse files Browse the repository at this point in the history
I really don't know why they were there.
  • Loading branch information
ah-OOG-ah committed Dec 9, 2024
1 parent 8974edf commit 0488648
Showing 1 changed file with 31 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -106,14 +107,16 @@ private enum TopState {
private int counter = 1;
private boolean balanced = false;

private final Queue<Long> energyInputValues = new LinkedList<>();
private final Queue<Long> energyOutputValues = new LinkedList<>();

private final Queue<Long> energyInputValues5m = new LinkedList<>();
private final Queue<Long> energyOutputValues5m = new LinkedList<>();

private final Queue<Long> energyInputValues1h = new LinkedList<>();
private final Queue<Long> energyOutputValues1h = new LinkedList<>();
// Holds one hour of ticks
private final long[] energyInput = new long[60 * 60 * 20];
private final long[] energyOutput = new long[60 * 60 * 20];
private int bufferPos = 0;
private long averageInput1m = 0;
private long averageOutput1m = 0;
private long averageInput5m = 0;
private long averageOutput5m = 0;
private long averageInput1h = 0;
private long averageOutput1h = 0;

private final long max_passive_drain_eu_per_tick_per_uhv_cap = 1_000_000;
private final long max_passive_drain_eu_per_tick_per_uev_cap = 100_000_000;
Expand Down Expand Up @@ -788,41 +791,22 @@ public boolean onRunningTick(ItemStack stack) {
tBMTE.injectEnergyUnits(ForgeDirection.UNKNOWN, inputLastTick, 1L);
tBMTE.drainEnergyUnits(ForgeDirection.UNKNOWN, outputLastTick, 1L);

// Add I/O values to Queues
if (energyInputValues.size() > DURATION_AVERAGE_TICKS) {
energyInputValues.remove();
}
energyInputValues.offer(inputLastTick);

if (energyOutputValues.size() > DURATION_AVERAGE_TICKS) {
energyOutputValues.remove();
}

energyOutputValues.offer(outputLastTick);

// Add I/O values to Queues 5 min
if (energyInputValues5m.size() > 6000) {
energyInputValues5m.remove();
}
energyInputValues5m.offer(inputLastTick);

if (energyOutputValues5m.size() > 6000) {
energyOutputValues5m.remove();
}

energyOutputValues5m.offer(outputLastTick);
// Pull off oldest I/O values
final long droppedInput = energyInput[bufferPos];
final long droppedOutput = energyOutput[bufferPos];

This comment has been minimized.

Copy link
@combusterf

combusterf Dec 10, 2024

Contributor

To get accurate results, you want to sample buffer[pos - 1h], buffer[pos - 5m] and buffer[pos - 1m] separately. If you don't, any spikes that happened an hour ago will make your 1m/5m averages go negative.


// Add I/O values to Queues 1 hour
if (energyInputValues1h.size() > 72000) {
energyInputValues1h.remove();
}
energyInputValues1h.offer(inputLastTick);
// Update running counters
averageInput1m -= droppedInput / 20 * 60;
averageInput5m -= droppedInput / 20 * 60 * 5;
averageInput1h -= droppedInput / 20 * 60 * 60;
averageOutput1m -= droppedOutput / 20 * 60;
averageOutput5m -= droppedOutput / 20 * 60 * 5;
averageOutput1h -= droppedOutput / 20 * 60 * 60;

if (energyOutputValues1h.size() > 72000) {
energyOutputValues1h.remove();
}

energyOutputValues1h.offer(outputLastTick);
// Insert values and bump the head
energyInput[bufferPos] = inputLastTick;
energyOutput[bufferPos] = outputLastTick;
bufferPos++;

return true;
}
Expand Down Expand Up @@ -924,51 +908,27 @@ private long getPowerToPush(long hatchWatts) {
}

private long getAvgIn() {
long sum = 0L;
for (long l : energyInputValues) {
sum += l;
}
return sum / Math.max(energyInputValues.size(), 1);
return averageInput1m;
}

private long getAvgOut() {
long sum = 0L;
for (long l : energyOutputValues) {
sum += l;
}
return sum / Math.max(energyOutputValues.size(), 1);
return averageOutput1m;
}

private long getAvgIn5m() {
double sum = 0;
for (long l : energyInputValues5m) {
sum += l;
}
return (long) sum / Math.max(energyInputValues5m.size(), 1);
return averageInput5m;
}

private long getAvgOut5m() {
double sum = 0;
for (long l : energyOutputValues5m) {
sum += l;
}
return (long) sum / Math.max(energyOutputValues5m.size(), 1);
return averageOutput5m;
}

private long getAvgIn1h() {
double sum = 0;
for (long l : energyInputValues1h) {
sum += l;
}
return (long) sum / Math.max(energyInputValues1h.size(), 1);
return averageInput1h;
}

private long getAvgOut1h() {
double sum = 0;
for (long l : energyOutputValues1h) {
sum += l;
}
return (long) sum / Math.max(energyOutputValues1h.size(), 1);
return averageOutput1h;
}

private String getTimeTo() {
Expand Down

0 comments on commit 0488648

Please sign in to comment.