From ac0204dffc7f7fb8478776daff45c6d3535734eb Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Tue, 4 Jun 2019 23:50:36 +0100 Subject: [PATCH] hopefully more accurate bandwidth gauge --- .../core/download/DownloadSession.groovy | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 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 2b3e56a3..ec6bb3f6 100644 --- a/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy +++ b/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy @@ -31,8 +31,8 @@ class DownloadSession { private final long fileLength private final MessageDigest digest - private final ArrayDeque timestamps = new ArrayDeque<>(SAMPLES) - private final ArrayDeque reads = new ArrayDeque<>(SAMPLES) + private final LinkedList timestamps = new LinkedList<>() + private final LinkedList reads = new LinkedList<>() private ByteBuffer mapped @@ -183,9 +183,21 @@ class DownloadSession { synchronized int speed() { if (timestamps.size() < SAMPLES) return 0 - long interval = timestamps.last - timestamps.first int totalRead = 0 - reads.each { totalRead += it } + 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 = timestamps.last - timestamps[idx] + + for (int i = idx; i < SAMPLES; i++) + totalRead += reads[idx] (int)(totalRead * 1000.0 / interval) } }