1
0
mirror of https://github.com/rclone/rclone.git synced 2025-07-10 17:27:18 +02:00

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 
This commit is contained in:
Nick Craig-Wood
2020-09-07 16:18:52 +01:00
parent 0e121eeddb
commit b81dc16484
2 changed files with 19 additions and 6 deletions

@ -366,6 +366,8 @@ func (sg *statsGroups) sum() *StatsInfo {
sum.lastError = stats.lastError sum.lastError = stats.lastError
} }
sum.startedTransfers = append(sum.startedTransfers, stats.startedTransfers...) sum.startedTransfers = append(sum.startedTransfers, stats.startedTransfers...)
sum.oldDuration += stats.oldDuration
sum.oldTimeRanges = append(sum.oldTimeRanges, stats.oldTimeRanges...)
} }
stats.mu.RUnlock() stats.mu.RUnlock()
} }

@ -4,8 +4,10 @@ import (
"fmt" "fmt"
"runtime" "runtime"
"testing" "testing"
"time"
"github.com/rclone/rclone/fstest/testy" "github.com/rclone/rclone/fstest/testy"
"github.com/stretchr/testify/assert"
) )
func TestStatsGroupOperations(t *testing.T) { func TestStatsGroupOperations(t *testing.T) {
@ -43,17 +45,26 @@ func TestStatsGroupOperations(t *testing.T) {
t.Parallel() t.Parallel()
stats1 := NewStats() stats1 := NewStats()
stats1.bytes = 5 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 := 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 := newStatsGroups()
sg.set("test1", stats1) sg.set("test1", stats1)
sg.set("test2", stats2) sg.set("test2", stats2)
sum := sg.sum() sum := sg.sum()
if sum.bytes != stats1.bytes+stats2.bytes { assert.Equal(t, stats1.bytes+stats2.bytes, sum.bytes)
t.Fatalf("sum() => bytes %d, expected %d", sum.bytes, stats1.bytes+stats2.bytes) assert.Equal(t, stats1.errors+stats2.errors, sum.errors)
} assert.Equal(t, stats1.oldDuration+stats2.oldDuration, sum.oldDuration)
if sum.errors != stats1.errors+stats2.errors { // dict can iterate in either order
t.Fatalf("sum() => errors %d, expected %d", sum.errors, stats1.errors+stats2.errors) 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)
} }
}) })