From a89b423dfc68ff3aa2d3baa9cb1146d900f67d2e Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Tue, 2 Jul 2019 13:05:06 +0100 Subject: [PATCH] simpler speed calculation --- .../core/download/DownloadSession.groovy | 37 +++++-------------- 1 file changed, 9 insertions(+), 28 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy b/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy index cff4189a..ae2740be 100644 --- a/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy +++ b/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy @@ -25,8 +25,6 @@ import java.util.logging.Level @Log class DownloadSession { - private static int SAMPLES = 10 - private final EventBus eventBus private final String meB64 private final Pieces pieces @@ -37,9 +35,9 @@ class DownloadSession { private final long fileLength private final Set available private final MessageDigest digest - - private final LinkedList timestamps = new LinkedList<>() - private final LinkedList reads = new LinkedList<>() + + private long lastSpeedRead = System.currentTimeMillis() + private long dataSinceLastRead private ByteBuffer mapped @@ -186,13 +184,7 @@ 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) + dataSinceLastRead += read } } @@ -223,22 +215,11 @@ class DownloadSession { } synchronized int speed() { - if (timestamps.size() < SAMPLES) - return 0 - int totalRead = 0 - int idx = 0 final long now = System.currentTimeMillis() - - while(idx < SAMPLES && timestamps.get(idx) < now - 1000) - idx++ - if (idx == SAMPLES) - return 0 - if (idx == SAMPLES - 1) - return reads[idx] - - long interval = Math.max(1000,timestamps.last - timestamps[idx]) - for (int i = idx; i < SAMPLES; i++) - totalRead += reads[idx] - (int)(totalRead * 1000.0 / interval) + long interval = Math.max(1000, now - lastSpeedRead) + lastSpeedRead = now; + int rv = (int) (dataSinceLastRead * 1000.0 / interval) + dataSinceLastRead = 0 + rv } }