2019-12-19 11:16:22 +01:00
|
|
|
package accounting
|
|
|
|
|
|
|
|
import (
|
2020-11-05 17:59:59 +01:00
|
|
|
"context"
|
2019-12-19 11:16:22 +01:00
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
2020-09-07 17:18:52 +02:00
|
|
|
"time"
|
2020-06-15 22:27:40 +02:00
|
|
|
|
2021-12-08 17:14:45 +01:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
2022-07-20 11:49:00 +02:00
|
|
|
"github.com/rclone/rclone/fs/rc"
|
2020-06-15 22:27:40 +02:00
|
|
|
"github.com/rclone/rclone/fstest/testy"
|
2020-09-07 17:18:52 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2022-07-20 11:49:00 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-12-19 11:16:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStatsGroupOperations(t *testing.T) {
|
2020-11-05 17:59:59 +01:00
|
|
|
ctx := context.Background()
|
2019-12-19 11:16:22 +01:00
|
|
|
|
|
|
|
t.Run("empty group returns nil", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
sg := newStatsGroups()
|
|
|
|
sg.get("invalid-group")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("set assigns stats to group", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-11-05 17:59:59 +01:00
|
|
|
stats := NewStats(ctx)
|
2019-12-19 11:16:22 +01:00
|
|
|
sg := newStatsGroups()
|
2020-11-05 17:59:59 +01:00
|
|
|
sg.set(ctx, "test", stats)
|
|
|
|
sg.set(ctx, "test1", stats)
|
2019-12-19 11:16:22 +01:00
|
|
|
if len(sg.m) != len(sg.names()) || len(sg.m) != 2 {
|
|
|
|
t.Fatalf("Expected two stats got %d, %d", len(sg.m), len(sg.order))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("get returns correct group", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-11-05 17:59:59 +01:00
|
|
|
stats := NewStats(ctx)
|
2019-12-19 11:16:22 +01:00
|
|
|
sg := newStatsGroups()
|
2020-11-05 17:59:59 +01:00
|
|
|
sg.set(ctx, "test", stats)
|
|
|
|
sg.set(ctx, "test1", stats)
|
2019-12-19 11:16:22 +01:00
|
|
|
got := sg.get("test")
|
|
|
|
if got != stats {
|
|
|
|
t.Fatal("get returns incorrect stats")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("sum returns correct values", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-11-05 17:59:59 +01:00
|
|
|
stats1 := NewStats(ctx)
|
2019-12-19 11:16:22 +01:00
|
|
|
stats1.bytes = 5
|
2021-10-24 17:58:39 +02:00
|
|
|
stats1.transferQueueSize = 10
|
2020-09-07 17:18:52 +02:00
|
|
|
stats1.errors = 6
|
|
|
|
stats1.oldDuration = time.Second
|
|
|
|
stats1.oldTimeRanges = []timeRange{{time.Now(), time.Now().Add(time.Second)}}
|
2020-11-05 17:59:59 +01:00
|
|
|
stats2 := NewStats(ctx)
|
2020-09-07 17:18:52 +02:00
|
|
|
stats2.bytes = 10
|
|
|
|
stats2.errors = 12
|
2021-10-24 17:58:39 +02:00
|
|
|
stats1.transferQueueSize = 20
|
2020-09-07 17:18:52 +02:00
|
|
|
stats2.oldDuration = 2 * time.Second
|
|
|
|
stats2.oldTimeRanges = []timeRange{{time.Now(), time.Now().Add(2 * time.Second)}}
|
2019-12-19 11:16:22 +01:00
|
|
|
sg := newStatsGroups()
|
2020-11-05 17:59:59 +01:00
|
|
|
sg.set(ctx, "test1", stats1)
|
|
|
|
sg.set(ctx, "test2", stats2)
|
|
|
|
sum := sg.sum(ctx)
|
2020-09-07 17:18:52 +02:00
|
|
|
assert.Equal(t, stats1.bytes+stats2.bytes, sum.bytes)
|
2021-10-24 17:58:39 +02:00
|
|
|
assert.Equal(t, stats1.transferQueueSize+stats2.transferQueueSize, sum.transferQueueSize)
|
2020-09-07 17:18:52 +02:00
|
|
|
assert.Equal(t, stats1.errors+stats2.errors, sum.errors)
|
|
|
|
assert.Equal(t, stats1.oldDuration+stats2.oldDuration, sum.oldDuration)
|
2021-08-14 06:56:39 +02:00
|
|
|
assert.Equal(t, stats1.average.speed+stats2.average.speed, sum.average.speed)
|
2020-09-07 17:18:52 +02:00
|
|
|
// 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)
|
2019-12-19 11:16:22 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("delete removes stats", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2020-11-05 17:59:59 +01:00
|
|
|
stats := NewStats(ctx)
|
2019-12-19 11:16:22 +01:00
|
|
|
sg := newStatsGroups()
|
2020-11-05 17:59:59 +01:00
|
|
|
sg.set(ctx, "test", stats)
|
|
|
|
sg.set(ctx, "test1", stats)
|
2019-12-19 11:16:22 +01:00
|
|
|
sg.delete("test1")
|
|
|
|
if sg.get("test1") != nil {
|
|
|
|
t.Fatal("stats not deleted")
|
|
|
|
}
|
|
|
|
if len(sg.m) != len(sg.names()) || len(sg.m) != 1 {
|
|
|
|
t.Fatalf("Expected two stats got %d, %d", len(sg.m), len(sg.order))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("memory is reclaimed", func(t *testing.T) {
|
2020-06-15 22:27:40 +02:00
|
|
|
testy.SkipUnreliable(t)
|
2019-12-19 11:16:22 +01:00
|
|
|
var (
|
|
|
|
count = 1000
|
|
|
|
start, end runtime.MemStats
|
|
|
|
sg = newStatsGroups()
|
|
|
|
)
|
|
|
|
|
|
|
|
runtime.GC()
|
|
|
|
runtime.ReadMemStats(&start)
|
|
|
|
|
build: modernize Go usage
This commit modernizes Go usage. This was done with:
go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...
Then files needed to be `go fmt`ed and a few comments needed to be
restored.
The modernizations include replacing
- if/else conditional assignment by a call to the built-in min or max functions added in go1.21
- sort.Slice(x, func(i, j int) bool) { return s[i] < s[j] } by a call to slices.Sort(s), added in go1.21
- interface{} by the 'any' type added in go1.18
- append([]T(nil), s...) by slices.Clone(s) or slices.Concat(s), added in go1.21
- loop around an m[k]=v map update by a call to one of the Collect, Copy, Clone, or Insert functions from the maps package, added in go1.21
- []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), added in go1.19
- append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), added in go1.21
- a 3-clause for i := 0; i < n; i++ {} loop by for i := range n {}, added in go1.22
2025-02-26 22:08:12 +01:00
|
|
|
for i := range count {
|
2020-11-05 17:59:59 +01:00
|
|
|
sg.set(ctx, fmt.Sprintf("test-%d", i), NewStats(ctx))
|
2019-12-19 11:16:22 +01:00
|
|
|
}
|
|
|
|
|
build: modernize Go usage
This commit modernizes Go usage. This was done with:
go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...
Then files needed to be `go fmt`ed and a few comments needed to be
restored.
The modernizations include replacing
- if/else conditional assignment by a call to the built-in min or max functions added in go1.21
- sort.Slice(x, func(i, j int) bool) { return s[i] < s[j] } by a call to slices.Sort(s), added in go1.21
- interface{} by the 'any' type added in go1.18
- append([]T(nil), s...) by slices.Clone(s) or slices.Concat(s), added in go1.21
- loop around an m[k]=v map update by a call to one of the Collect, Copy, Clone, or Insert functions from the maps package, added in go1.21
- []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), added in go1.19
- append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), added in go1.21
- a 3-clause for i := 0; i < n; i++ {} loop by for i := range n {}, added in go1.22
2025-02-26 22:08:12 +01:00
|
|
|
for i := range count {
|
2019-12-19 11:16:22 +01:00
|
|
|
sg.delete(fmt.Sprintf("test-%d", i))
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.GC()
|
|
|
|
runtime.ReadMemStats(&end)
|
|
|
|
|
2022-06-08 22:25:17 +02:00
|
|
|
t.Logf("%+v\n%+v", start, end)
|
2019-12-19 11:16:22 +01:00
|
|
|
diff := percentDiff(start.HeapObjects, end.HeapObjects)
|
2022-06-08 22:25:17 +02:00
|
|
|
if diff > 1 {
|
2019-12-19 11:16:22 +01:00
|
|
|
t.Errorf("HeapObjects = %d, expected %d", end.HeapObjects, start.HeapObjects)
|
|
|
|
}
|
|
|
|
})
|
2022-07-20 11:49:00 +02:00
|
|
|
|
|
|
|
testGroupStatsInfo := NewStatsGroup(ctx, "test-group")
|
2023-03-08 19:40:37 +01:00
|
|
|
require.NoError(t, testGroupStatsInfo.DeleteFile(ctx, 0))
|
build: modernize Go usage
This commit modernizes Go usage. This was done with:
go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...
Then files needed to be `go fmt`ed and a few comments needed to be
restored.
The modernizations include replacing
- if/else conditional assignment by a call to the built-in min or max functions added in go1.21
- sort.Slice(x, func(i, j int) bool) { return s[i] < s[j] } by a call to slices.Sort(s), added in go1.21
- interface{} by the 'any' type added in go1.18
- append([]T(nil), s...) by slices.Clone(s) or slices.Concat(s), added in go1.21
- loop around an m[k]=v map update by a call to one of the Collect, Copy, Clone, or Insert functions from the maps package, added in go1.21
- []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), added in go1.19
- append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), added in go1.21
- a 3-clause for i := 0; i < n; i++ {} loop by for i := range n {}, added in go1.22
2025-02-26 22:08:12 +01:00
|
|
|
for range 41 {
|
2023-03-08 19:40:37 +01:00
|
|
|
require.NoError(t, GlobalStats().DeleteFile(ctx, 0))
|
|
|
|
}
|
2022-07-20 11:49:00 +02:00
|
|
|
|
|
|
|
t.Run("core/group-list", func(t *testing.T) {
|
|
|
|
call := rc.Calls.Get("core/group-list")
|
|
|
|
require.NotNil(t, call)
|
|
|
|
got, err := call.Fn(ctx, rc.Params{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, rc.Params{
|
|
|
|
"groups": []string{
|
|
|
|
"test-group",
|
|
|
|
},
|
|
|
|
}, got)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("core/stats", func(t *testing.T) {
|
|
|
|
call := rc.Calls.Get("core/stats")
|
|
|
|
require.NotNil(t, call)
|
|
|
|
gotNoGroup, err := call.Fn(ctx, rc.Params{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
gotGroup, err := call.Fn(ctx, rc.Params{"group": "test-group"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(42), gotNoGroup["deletes"])
|
|
|
|
assert.Equal(t, int64(1), gotGroup["deletes"])
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("core/transferred", func(t *testing.T) {
|
|
|
|
call := rc.Calls.Get("core/transferred")
|
|
|
|
require.NotNil(t, call)
|
|
|
|
gotNoGroup, err := call.Fn(ctx, rc.Params{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
gotGroup, err := call.Fn(ctx, rc.Params{"group": "test-group"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, rc.Params{
|
|
|
|
"transferred": []TransferSnapshot{},
|
|
|
|
}, gotNoGroup)
|
|
|
|
assert.Equal(t, rc.Params{
|
|
|
|
"transferred": []TransferSnapshot{},
|
|
|
|
}, gotGroup)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("core/stats-reset", func(t *testing.T) {
|
|
|
|
call := rc.Calls.Get("core/stats-reset")
|
|
|
|
require.NotNil(t, call)
|
|
|
|
|
|
|
|
assert.Equal(t, int64(41), GlobalStats().deletes)
|
|
|
|
assert.Equal(t, int64(1), testGroupStatsInfo.deletes)
|
|
|
|
|
|
|
|
_, err := call.Fn(ctx, rc.Params{"group": "test-group"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, int64(41), GlobalStats().deletes)
|
|
|
|
assert.Equal(t, int64(0), testGroupStatsInfo.deletes)
|
|
|
|
|
|
|
|
_, err = call.Fn(ctx, rc.Params{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, int64(0), GlobalStats().deletes)
|
|
|
|
assert.Equal(t, int64(0), testGroupStatsInfo.deletes)
|
|
|
|
|
|
|
|
_, err = call.Fn(ctx, rc.Params{"group": "not-found"})
|
|
|
|
require.ErrorContains(t, err, `group "not-found" not found`)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
testGroupStatsInfo = NewStatsGroup(ctx, "test-group")
|
|
|
|
|
|
|
|
t.Run("core/stats-delete", func(t *testing.T) {
|
|
|
|
call := rc.Calls.Get("core/stats-delete")
|
|
|
|
require.NotNil(t, call)
|
|
|
|
|
|
|
|
assert.Equal(t, []string{"test-group"}, groups.names())
|
|
|
|
|
|
|
|
_, err := call.Fn(ctx, rc.Params{"group": "test-group"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, []string{}, groups.names())
|
|
|
|
|
|
|
|
_, err = call.Fn(ctx, rc.Params{"group": "not-found"})
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2019-12-19 11:16:22 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 17:14:45 +01:00
|
|
|
func TestCountError(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
Start(ctx)
|
|
|
|
defer func() {
|
|
|
|
groups = newStatsGroups()
|
|
|
|
}()
|
|
|
|
t.Run("global stats", func(t *testing.T) {
|
|
|
|
GlobalStats().ResetCounters()
|
|
|
|
err := fs.CountError(ctx, fmt.Errorf("global err"))
|
|
|
|
assert.Equal(t, int64(1), GlobalStats().errors)
|
|
|
|
|
|
|
|
assert.True(t, fserrors.IsCounted(err))
|
|
|
|
})
|
|
|
|
t.Run("group stats", func(t *testing.T) {
|
|
|
|
statGroupName := fmt.Sprintf("%s-error_group", t.Name())
|
|
|
|
GlobalStats().ResetCounters()
|
|
|
|
stCtx := WithStatsGroup(ctx, statGroupName)
|
|
|
|
st := StatsGroup(stCtx, statGroupName)
|
|
|
|
|
|
|
|
err := fs.CountError(stCtx, fmt.Errorf("group err"))
|
|
|
|
|
|
|
|
assert.Equal(t, int64(0), GlobalStats().errors)
|
|
|
|
assert.Equal(t, int64(1), st.errors)
|
|
|
|
assert.True(t, fserrors.IsCounted(err))
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-19 11:16:22 +01:00
|
|
|
func percentDiff(start, end uint64) uint64 {
|
2025-02-13 12:39:24 +01:00
|
|
|
if start == 0 {
|
|
|
|
return 0 // Handle zero start value to avoid division by zero
|
|
|
|
}
|
|
|
|
var diff uint64
|
|
|
|
if end > start {
|
|
|
|
diff = end - start // Handle case where end is larger than start
|
|
|
|
} else {
|
|
|
|
diff = start - end
|
|
|
|
}
|
|
|
|
return (diff * 100) / start
|
2019-12-19 11:16:22 +01:00
|
|
|
}
|