summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyo Nakamura <upa@haeena.net>2023-09-13 16:14:40 +0900
committerRyo Nakamura <upa@haeena.net>2023-11-01 19:54:18 +0900
commitb298b2ec3527edcb986638af3401aff42f66d67c (patch)
treec9e4961b8aca6d0ac3b0e9f59d99f491c848257e
parent05a7e967593fc3bcb92b01910851897ac1caa23a (diff)
main: adopt rolling average of recent eight bps values to calculate ETA
-rw-r--r--src/main.c36
1 files changed, 28 insertions, 8 deletions
diff --git a/src/main.c b/src/main.c
index 6efab03..ab2c1c8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -427,23 +427,43 @@ double calculate_bps(size_t diff, struct timeval *b, struct timeval *a)
return (double)diff / calculate_timedelta(b, a);
}
+
char *calculate_eta(size_t remain, size_t diff, struct timeval *b, struct timeval *a,
bool final)
{
static char buf[16];
+
+#define bps_window_size 16
+ static double bps_window[bps_window_size];
+ static size_t sum, idx, count;
double elapsed = calculate_timedelta(b, a);
- double eta;
+ double bps = diff / elapsed;
+ double avg, eta;
- if (diff == 0)
+ /* early return when diff == 0 (stalled) or final output */
+ if (diff == 0) {
snprintf(buf, sizeof(buf), "--:-- ETA");
- else if (final) {
+ return buf;
+ }
+ if (final) {
snprintf(buf, sizeof(buf), "%02d:%02d ",
(int)(floor(elapsed / 60)), (int)round(elapsed) % 60);
- } else {
- eta = remain / (diff / elapsed);
- snprintf(buf, sizeof(buf), "%02d:%02d ETA",
- (int)floor(eta / 60), (int)round(eta) % 60);
- }
+ return buf;
+ }
+
+ /* drop the old bps value and add the recent one */
+ sum -= bps_window[idx];
+ bps_window[idx] = bps;
+ sum += bps_window[idx];
+ idx = (idx + 1) % bps_window_size;
+ count++;
+
+ /* calcuate ETA from avg of recent bps values */
+ avg = sum / min(count, bps_window_size);
+ eta = remain / avg;
+ snprintf(buf, sizeof(buf), "%02d:%02d ETA",
+ (int)floor(eta / 60), (int)round(eta) % 60);
+
return buf;
}