From 5c49096e116e4776f2f298992e2b92deecc37531 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 7 Sep 2020 16:18:52 +0100 Subject: [PATCH] acounting: fix incorrect speed and transferTime in core/stats Before this change the code which summed up the existing transfers over all the stats groups forgot to add the old transfer time and old transfers in. This meant that the speed and elapsedTime got increasingly inaccurate over time due to the transfers being culled from the list but their time not being accounted for. This change adds the old transfers into the sum which fixes the problem. This was only a problem over the rc. Fixes #4569 --- fs/accounting/stats_groups.go | 2 ++ fs/accounting/stats_groups_test.go | 23 +++++++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/fs/accounting/stats_groups.go b/fs/accounting/stats_groups.go index 6bcd1ea20..776af484c 100644 --- a/fs/accounting/stats_groups.go +++ b/fs/accounting/stats_groups.go @@ -366,6 +366,8 @@ func (sg *statsGroups) sum() *StatsInfo { sum.lastError = stats.lastError } sum.startedTransfers = append(sum.startedTransfers, stats.startedTransfers...) + sum.oldDuration += stats.oldDuration + sum.oldTimeRanges = append(sum.oldTimeRanges, stats.oldTimeRanges...) } stats.mu.RUnlock() } diff --git a/fs/accounting/stats_groups_test.go b/fs/accounting/stats_groups_test.go index 76d93bf64..224dff9ef 100644 --- a/fs/accounting/stats_groups_test.go +++ b/fs/accounting/stats_groups_test.go @@ -4,8 +4,10 @@ import ( "fmt" "runtime" "testing" + "time" "github.com/rclone/rclone/fstest/testy" + "github.com/stretchr/testify/assert" ) func TestStatsGroupOperations(t *testing.T) { @@ -43,17 +45,26 @@ func TestStatsGroupOperations(t *testing.T) { t.Parallel() stats1 := NewStats() stats1.bytes = 5 - stats1.errors = 5 + stats1.errors = 6 + stats1.oldDuration = time.Second + stats1.oldTimeRanges = []timeRange{{time.Now(), time.Now().Add(time.Second)}} stats2 := NewStats() + stats2.bytes = 10 + stats2.errors = 12 + stats2.oldDuration = 2 * time.Second + stats2.oldTimeRanges = []timeRange{{time.Now(), time.Now().Add(2 * time.Second)}} sg := newStatsGroups() sg.set("test1", stats1) sg.set("test2", stats2) sum := sg.sum() - if sum.bytes != stats1.bytes+stats2.bytes { - t.Fatalf("sum() => bytes %d, expected %d", sum.bytes, stats1.bytes+stats2.bytes) - } - if sum.errors != stats1.errors+stats2.errors { - t.Fatalf("sum() => errors %d, expected %d", sum.errors, stats1.errors+stats2.errors) + assert.Equal(t, stats1.bytes+stats2.bytes, sum.bytes) + assert.Equal(t, stats1.errors+stats2.errors, sum.errors) + assert.Equal(t, stats1.oldDuration+stats2.oldDuration, sum.oldDuration) + // dict can iterate in either order + a := timeRanges{stats1.oldTimeRanges[0], stats2.oldTimeRanges[0]} + b := timeRanges{stats2.oldTimeRanges[0], stats1.oldTimeRanges[0]} + if !assert.ObjectsAreEqual(a, sum.oldTimeRanges) { + assert.Equal(t, b, sum.oldTimeRanges) } })