rudimentary speed gauge

pull/4/head
Zlatin Balevsky 2019-06-03 18:02:10 +01:00
parent abbb741d73
commit 6e0f1778b7
3 changed files with 28 additions and 0 deletions

View File

@ -20,6 +20,8 @@ import java.security.NoSuchAlgorithmException
@Log
class DownloadSession {
private static int SAMPLES = 10
private final String meB64
private final Pieces pieces
private final InfoHash infoHash
@ -29,6 +31,9 @@ class DownloadSession {
private final long fileLength
private final MessageDigest digest
private final ArrayDeque<Long> timestamps = new ArrayDeque<>(SAMPLES)
private final ArrayDeque<Integer> reads = new ArrayDeque<>(SAMPLES)
private ByteBuffer mapped
DownloadSession(String meB64, Pieces pieces, InfoHash infoHash, Endpoint endpoint, File file,
@ -122,6 +127,13 @@ class DownloadSession {
throw new IOException()
synchronized(this) {
mapped.put(tmp, 0, read)
if (timestamps.size() == SAMPLES) {
timestamps.removeFirst()
reads.removeFirst()
}
timestamps.addLast(System.currentTimeMillis())
reads.addLast(read)
}
}
@ -144,4 +156,13 @@ class DownloadSession {
return 0
mapped.position()
}
synchronized int speed() {
if (timestamps.size() < SAMPLES)
return 0
long interval = timestamps.last - timestamps.first
int totalRead = 0
reads.each { totalRead += it }
(int)(totalRead * 1000.0 / interval)
}
}

View File

@ -109,6 +109,12 @@ public class Downloader {
currentSession.positionInPiece()
}
public int speed() {
if (currentSession == null)
return 0
currentSession.speed()
}
public DownloadState getCurrentState() {
currentState
}

View File

@ -109,6 +109,7 @@ class MainFrameView {
int pieceSize = row.downloader.pieceSize // TODO: fix for last piece
"$position/$pieceSize bytes"
})
closureColumn(header: "Speed (bytes/second)", type:Integer, read :{row -> row.downloader.speed()})
}
}
}