From 4f55687b3803ef264b0405a1598a06c97c59d683 Mon Sep 17 00:00:00 2001 From: 0xb00bface <0xboobface@gmail.com> Date: Wed, 30 Dec 2020 19:26:51 +0100 Subject: [PATCH] Fix bandwidth calulation for client/server --- common/src/main/java/ctbrec/io/BandwidthMeter.java | 11 ++++++++--- .../src/main/java/ctbrec/recorder/RemoteRecorder.java | 2 +- .../java/ctbrec/recorder/server/RecorderServlet.java | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/ctbrec/io/BandwidthMeter.java b/common/src/main/java/ctbrec/io/BandwidthMeter.java index 58472330..204609a1 100644 --- a/common/src/main/java/ctbrec/io/BandwidthMeter.java +++ b/common/src/main/java/ctbrec/io/BandwidthMeter.java @@ -7,13 +7,13 @@ import java.util.List; public class BandwidthMeter { - public static final Duration MEASURE_TIMEFRAME = Duration.ofSeconds(10); private static long[] records = new long[10000]; private static int head = 0; private static int tail = 0; private static List listeners = new ArrayList<>(); private static long lastUpdate = 0; private static long throughput = 0; + private static Duration timeframe = Duration.ofMinutes(1); private BandwidthMeter() { } @@ -25,9 +25,9 @@ public class BandwidthMeter { if (lastUpdate + 1000 < System.currentTimeMillis()) { Instant last = Instant.ofEpochMilli(lastUpdate); Instant now = Instant.now(); - Duration d = Duration.between(last, now); + timeframe = Duration.between(last, now); calculateThroughput(); - fireEvent(getThroughput(), d); + fireEvent(getThroughput(), timeframe); } } @@ -52,6 +52,7 @@ public class BandwidthMeter { public static void setThroughput(long bytes, Duration d) { records[0] = bytes; + timeframe = d; fireEvent(bytes, d); } @@ -69,6 +70,10 @@ public class BandwidthMeter { return throughput; } + public static Duration getTimeframe() { + return timeframe; + } + public static void addListener(Listener l) { listeners.add(l); } diff --git a/common/src/main/java/ctbrec/recorder/RemoteRecorder.java b/common/src/main/java/ctbrec/recorder/RemoteRecorder.java index a8771de2..7ecb4cf2 100644 --- a/common/src/main/java/ctbrec/recorder/RemoteRecorder.java +++ b/common/src/main/java/ctbrec/recorder/RemoteRecorder.java @@ -244,7 +244,7 @@ public class RemoteRecorder implements Recorder { spaceTotal = resp.getLong("spaceTotal"); spaceFree = resp.getLong("spaceFree"); long throughput = resp.getLong("throughput"); - Duration timeframe = Duration.ofSeconds(resp.getInt("throughputTimeframe")); + Duration timeframe = Duration.ofMillis(resp.getInt("throughputTimeframe")); BandwidthMeter.setThroughput(throughput, timeframe); long minimumSpaceLeftInBytes = resp.optLong("minimumSpaceLeftInBytes"); if (minimumSpaceLeftInBytes > 0 && minimumSpaceLeftInBytes >= spaceFree diff --git a/server/src/main/java/ctbrec/recorder/server/RecorderServlet.java b/server/src/main/java/ctbrec/recorder/server/RecorderServlet.java index 7dcb1c00..8e49a4d4 100644 --- a/server/src/main/java/ctbrec/recorder/server/RecorderServlet.java +++ b/server/src/main/java/ctbrec/recorder/server/RecorderServlet.java @@ -214,7 +214,7 @@ public class RecorderServlet extends AbstractCtbrecServlet { jsonResponse.put("spaceTotal", recorder.getTotalSpaceBytes()); jsonResponse.put("spaceFree", recorder.getFreeSpaceBytes()); jsonResponse.put("throughput", BandwidthMeter.getThroughput()); - jsonResponse.put("throughputTimeframe", BandwidthMeter.MEASURE_TIMEFRAME.getSeconds()); + jsonResponse.put("throughputTimeframe", BandwidthMeter.getTimeframe().toMillis()); jsonResponse.put("minimumSpaceLeftInBytes", Config.getInstance().getSettings().minimumSpaceLeftInBytes); resp.getWriter().write(jsonResponse.toString()); break;