diff --git a/java_console/io/src/main/java/com/rusefi/io/serial/RateCounter.java b/java_console/io/src/main/java/com/rusefi/io/serial/RateCounter.java index 51c9594811..8fad5a926c 100644 --- a/java_console/io/src/main/java/com/rusefi/io/serial/RateCounter.java +++ b/java_console/io/src/main/java/com/rusefi/io/serial/RateCounter.java @@ -5,6 +5,7 @@ import com.rusefi.Timeouts; import java.util.LinkedList; public class RateCounter { + private final static int MAGIC_DURATION = Timeouts.SECOND; private final LinkedList timeStamps = new LinkedList<>(); @@ -16,7 +17,7 @@ public class RateCounter { } public synchronized int getCurrentRate(long now) { - long threshold = now - Timeouts.SECOND; + long threshold = now - MAGIC_DURATION; while (!timeStamps.isEmpty() && timeStamps.peekFirst().timestamp < threshold) timeStamps.removeFirst(); @@ -27,12 +28,16 @@ public class RateCounter { return result; } + public synchronized int getSizeForUnitTest() { + return timeStamps.size(); + } + public void add() { add(System.currentTimeMillis()); } - public synchronized void add(long now) { - timeStamps.add(new Pair(now, 1)); + public synchronized void add(long timestamp) { + timeStamps.add(new Pair(timestamp, 1)); } private static class Pair { diff --git a/java_console/io/src/main/java/com/rusefi/io/stream/PCanIoStream.java b/java_console/io/src/main/java/com/rusefi/io/stream/PCanIoStream.java index 041f41bf1c..328fa5d7c0 100644 --- a/java_console/io/src/main/java/com/rusefi/io/stream/PCanIoStream.java +++ b/java_console/io/src/main/java/com/rusefi/io/stream/PCanIoStream.java @@ -8,6 +8,7 @@ import com.rusefi.io.IoStream; import com.rusefi.io.can.IsoTpCanDecoder; import com.rusefi.io.can.IsoTpConnector; import com.rusefi.io.serial.AbstractIoStream; +import com.rusefi.io.serial.RateCounter; import com.rusefi.io.tcp.BinaryProtocolServer; import com.rusefi.ui.StatusConsumer; import org.jetbrains.annotations.Nullable; @@ -28,6 +29,9 @@ public class PCanIoStream extends AbstractIoStream { private final IncomingDataBuffer dataBuffer = createDataBuffer("[PCAN] "); private final PCANBasic can; private final StatusConsumer statusListener; + + private final RateCounter totalCounter = new RateCounter(); + private final RateCounter isoTpCounter = new RateCounter(); private final IsoTpCanDecoder canDecoder = new IsoTpCanDecoder() { @Override protected void onTpFirstFrame() { @@ -109,13 +113,16 @@ public class PCanIoStream extends AbstractIoStream { TPCANMsg rx = new TPCANMsg(Byte.MAX_VALUE); TPCANStatus status = can.Read(CHANNEL, rx, null); if (status == TPCANStatus.PCAN_ERROR_OK) { + totalCounter.add(); if (log.debugEnabled()) log.debug("Got [" + rx + "] id=" + String.format("%X", rx.getID()) + " len=" + rx.getLength() + ": " + IoStream.printByteArray(rx.getData())); if (rx.getID() != CAN_ECU_SERIAL_TX_ID) { // if (log.debugEnabled()) log.info("Skipping non " + String.format("%X", CAN_ECU_SERIAL_TX_ID) + " packet: " + String.format("%X", rx.getID())); + log.info("Total rate " + totalCounter.getCurrentRate() + ", isotp rate " + isoTpCounter.getCurrentRate()); return; } + isoTpCounter.add(); byte[] decode = canDecoder.decodePacket(rx.getData()); listener.onDataArrived(decode); diff --git a/java_console/io/src/test/java/com/rusefi/io/serial/RateCounterTest.java b/java_console/io/src/test/java/com/rusefi/io/serial/RateCounterTest.java index 671cf49768..81bc527888 100644 --- a/java_console/io/src/test/java/com/rusefi/io/serial/RateCounterTest.java +++ b/java_console/io/src/test/java/com/rusefi/io/serial/RateCounterTest.java @@ -13,15 +13,19 @@ public class RateCounterTest { assertEquals(0, rateCounter.getCurrentRate()); - rateCounter.add(1); + rateCounter.add(/*timestamp*/1); rateCounter.add(1); rateCounter.add(1); rateCounter.add(1); + // cute size effect: FUTURE timestamps are also counted :) assertEquals(4, rateCounter.getCurrentRate(0)); + assertEquals(3, rateCounter.getSizeForUnitTest()); assertEquals(0, rateCounter.getCurrentRate(2 * Timeouts.SECOND)); + // assert purge of oldest records + assertEquals(0, rateCounter.getSizeForUnitTest()); } }