Calculate bytes per second in network gauges

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2016-07-21 17:54:01 -07:00
parent 8cbeecd347
commit 4e8ca603fe
3 changed files with 36 additions and 20 deletions

View File

@ -9,7 +9,7 @@ import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
public abstract class NetworkGauge implements Gauge<Long> { public abstract class NetworkGauge implements Gauge<Double> {
protected Pair<Long, Long> getSentReceived() throws IOException { protected Pair<Long, Long> getSentReceived() throws IOException {
File proc = new File("/proc/net/dev"); File proc = new File("/proc/net/dev");

View File

@ -13,23 +13,31 @@ public class NetworkReceivedGauge extends NetworkGauge {
private long lastTimestamp; private long lastTimestamp;
private long lastReceived; private long lastReceived;
public NetworkReceivedGauge() {
try {
this.lastTimestamp = System.currentTimeMillis();
this.lastReceived = getSentReceived().second();
} catch (IOException e) {
logger.warn(NetworkReceivedGauge.class.getSimpleName(), e);
}
}
@Override @Override
public Long getValue() { public Double getValue() {
try { try {
long timestamp = System.currentTimeMillis(); long timestamp = System.currentTimeMillis();
Pair<Long, Long> sentAndReceived = getSentReceived(); Pair<Long, Long> sentAndReceived = getSentReceived();
long result = 0; double bytesReceived = sentAndReceived.second() - lastReceived;
double secondsElapsed = (timestamp - this.lastTimestamp) / 1000;
double result = bytesReceived / secondsElapsed;
if (lastTimestamp != 0) { this.lastTimestamp = timestamp;
result = sentAndReceived.second() - lastReceived; this.lastReceived = sentAndReceived.second();
lastReceived = sentAndReceived.second();
}
lastTimestamp = timestamp;
return result; return result;
} catch (IOException e) { } catch (IOException e) {
logger.warn("NetworkReceivedGauge", e); logger.warn("NetworkReceivedGauge", e);
return -1L; return -1D;
} }
} }

View File

@ -13,23 +13,31 @@ public class NetworkSentGauge extends NetworkGauge {
private long lastTimestamp; private long lastTimestamp;
private long lastSent; private long lastSent;
@Override public NetworkSentGauge() {
public Long getValue() {
try { try {
long timestamp = System.currentTimeMillis(); this.lastTimestamp = System.currentTimeMillis();
Pair<Long, Long> sentAndReceived = getSentReceived(); this.lastSent = getSentReceived().first();
long result = 0; } catch (IOException e) {
logger.warn(NetworkSentGauge.class.getSimpleName(), e);
}
}
if (lastTimestamp != 0) { @Override
result = sentAndReceived.first() - lastSent; public Double getValue() {
lastSent = sentAndReceived.first(); try {
} long timestamp = System.currentTimeMillis();
Pair<Long, Long> sentAndReceived = getSentReceived();
double bytesTransmitted = sentAndReceived.first() - lastSent;
double secondsElapsed = (timestamp - this.lastTimestamp) / 1000;
double result = bytesTransmitted / secondsElapsed;
this.lastSent = sentAndReceived.first();
this.lastTimestamp = timestamp;
lastTimestamp = timestamp;
return result; return result;
} catch (IOException e) { } catch (IOException e) {
logger.warn("NetworkSentGauge", e); logger.warn("NetworkSentGauge", e);
return -1L; return -1D;
} }
} }
} }