add speed column to uploads table

pull/24/head
Zlatin Balevsky 2019-10-27 03:00:54 +00:00
parent 817dd68faf
commit 9bd3c4f141
4 changed files with 48 additions and 4 deletions

View File

@ -62,11 +62,13 @@ class ContentUploader extends Uploader {
mapped = channel.map(FileChannel.MapMode.READ_ONLY, range.start, range.end - range.start + 1)
byte [] tmp = new byte[0x1 << 13]
while(mapped.hasRemaining()) {
int start = mapped.position()
int read
synchronized(this) {
int start = mapped.position()
mapped.get(tmp, 0, Math.min(tmp.length, mapped.remaining()))
read = mapped.position() - start
dataSinceLastRead += read
}
int read = mapped.position() - start
endpoint.getOutputStream().write(tmp, 0, read)
}
} finally {

View File

@ -26,11 +26,13 @@ class HashListUploader extends Uploader {
byte[]tmp = new byte[0x1 << 13]
while(mapped.hasRemaining()) {
int start = mapped.position()
int read
synchronized(this) {
int start = mapped.position()
mapped.get(tmp, 0, Math.min(tmp.length, mapped.remaining()))
read = mapped.position() - start
dataSinceLastRead += read
}
int read = mapped.position() - start
endpoint.getOutputStream().write(tmp, 0, read)
}
endpoint.getOutputStream().flush()

View File

@ -11,7 +11,13 @@ import com.muwire.core.connection.Endpoint
abstract class Uploader {
protected final Endpoint endpoint
protected ByteBuffer mapped
private long lastSpeedRead
protected int dataSinceLastRead
private final ArrayList<Integer> speedArr = [0,0,0,0,0]
private int speedPos, speedAvg
Uploader(Endpoint endpoint) {
this.endpoint = endpoint
}
@ -38,4 +44,34 @@ abstract class Uploader {
abstract int getTotalPieces();
abstract long getTotalSize();
synchronized int speed() {
final long now = System.currentTimeMillis()
long interval = Math.max(1000, now - lastSpeedRead)
lastSpeedRead = now;
int currSpeed = (int) (dataSinceLastRead * 1000.0 / interval)
dataSinceLastRead = 0
// normalize to speedArr.size
currSpeed /= speedArr.size()
// compute new speedAvg and update speedArr
if ( speedArr[speedPos] > speedAvg ) {
speedAvg = 0
} else {
speedAvg -= speedArr[speedPos]
}
speedAvg += currSpeed
speedArr[speedPos] = currSpeed
// this might be necessary due to rounding errors
if (speedAvg < 0)
speedAvg = 0
// rolling index over the speedArr
speedPos++
if (speedPos >= speedArr.size())
speedPos=0
speedAvg
}
}

View File

@ -293,6 +293,10 @@ class MainFrameView {
}
String.format("%02d", percent) + "% ${totalSize} ($done/$pieces pcs)".toString()
})
closureColumn(header : "Speed", type : String, read : { row ->
int speed = row.uploader.speed()
DataHelper.formatSize2Decimal(speed, false) + "B/sec"
})
}
}
}