Fix bandwidth calulation for client/server

This commit is contained in:
0xb00bface 2020-12-30 19:26:51 +01:00
parent dbb44e1374
commit 4f55687b38
3 changed files with 10 additions and 5 deletions

View File

@ -7,13 +7,13 @@ import java.util.List;
public class BandwidthMeter { public class BandwidthMeter {
public static final Duration MEASURE_TIMEFRAME = Duration.ofSeconds(10);
private static long[] records = new long[10000]; private static long[] records = new long[10000];
private static int head = 0; private static int head = 0;
private static int tail = 0; private static int tail = 0;
private static List<Listener> listeners = new ArrayList<>(); private static List<Listener> listeners = new ArrayList<>();
private static long lastUpdate = 0; private static long lastUpdate = 0;
private static long throughput = 0; private static long throughput = 0;
private static Duration timeframe = Duration.ofMinutes(1);
private BandwidthMeter() { private BandwidthMeter() {
} }
@ -25,9 +25,9 @@ public class BandwidthMeter {
if (lastUpdate + 1000 < System.currentTimeMillis()) { if (lastUpdate + 1000 < System.currentTimeMillis()) {
Instant last = Instant.ofEpochMilli(lastUpdate); Instant last = Instant.ofEpochMilli(lastUpdate);
Instant now = Instant.now(); Instant now = Instant.now();
Duration d = Duration.between(last, now); timeframe = Duration.between(last, now);
calculateThroughput(); calculateThroughput();
fireEvent(getThroughput(), d); fireEvent(getThroughput(), timeframe);
} }
} }
@ -52,6 +52,7 @@ public class BandwidthMeter {
public static void setThroughput(long bytes, Duration d) { public static void setThroughput(long bytes, Duration d) {
records[0] = bytes; records[0] = bytes;
timeframe = d;
fireEvent(bytes, d); fireEvent(bytes, d);
} }
@ -69,6 +70,10 @@ public class BandwidthMeter {
return throughput; return throughput;
} }
public static Duration getTimeframe() {
return timeframe;
}
public static void addListener(Listener l) { public static void addListener(Listener l) {
listeners.add(l); listeners.add(l);
} }

View File

@ -244,7 +244,7 @@ public class RemoteRecorder implements Recorder {
spaceTotal = resp.getLong("spaceTotal"); spaceTotal = resp.getLong("spaceTotal");
spaceFree = resp.getLong("spaceFree"); spaceFree = resp.getLong("spaceFree");
long throughput = resp.getLong("throughput"); long throughput = resp.getLong("throughput");
Duration timeframe = Duration.ofSeconds(resp.getInt("throughputTimeframe")); Duration timeframe = Duration.ofMillis(resp.getInt("throughputTimeframe"));
BandwidthMeter.setThroughput(throughput, timeframe); BandwidthMeter.setThroughput(throughput, timeframe);
long minimumSpaceLeftInBytes = resp.optLong("minimumSpaceLeftInBytes"); long minimumSpaceLeftInBytes = resp.optLong("minimumSpaceLeftInBytes");
if (minimumSpaceLeftInBytes > 0 && minimumSpaceLeftInBytes >= spaceFree if (minimumSpaceLeftInBytes > 0 && minimumSpaceLeftInBytes >= spaceFree

View File

@ -214,7 +214,7 @@ public class RecorderServlet extends AbstractCtbrecServlet {
jsonResponse.put("spaceTotal", recorder.getTotalSpaceBytes()); jsonResponse.put("spaceTotal", recorder.getTotalSpaceBytes());
jsonResponse.put("spaceFree", recorder.getFreeSpaceBytes()); jsonResponse.put("spaceFree", recorder.getFreeSpaceBytes());
jsonResponse.put("throughput", BandwidthMeter.getThroughput()); 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); jsonResponse.put("minimumSpaceLeftInBytes", Config.getInstance().getSettings().minimumSpaceLeftInBytes);
resp.getWriter().write(jsonResponse.toString()); resp.getWriter().write(jsonResponse.toString());
break; break;