From 8500d95579b3b9639303fc1224b004a403b33f17 Mon Sep 17 00:00:00 2001 From: albertony <12441419+albertony@users.noreply.github.com> Date: Tue, 6 Apr 2021 14:10:11 +0200 Subject: [PATCH] test: consider global option for printing human-readable sizes and avoid unsigned integer overflow --- cmd/test/memory/memory.go | 50 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/cmd/test/memory/memory.go b/cmd/test/memory/memory.go index 0fbf216fc..823cade14 100644 --- a/cmd/test/memory/memory.go +++ b/cmd/test/memory/memory.go @@ -24,6 +24,7 @@ var commandDefinition = &cobra.Command{ fsrc := cmd.NewFsSrc(args) cmd.Run(false, false, command, func() error { ctx := context.Background() + ci := fs.GetConfig(context.Background()) objects, _, err := operations.Count(ctx, fsrc) if err != nil { return err @@ -43,9 +44,52 @@ var commandDefinition = &cobra.Command{ } runtime.GC() runtime.ReadMemStats(&after) - usedMemory := after.Alloc - before.Alloc - fs.Logf(nil, "%d objects took %d bytes, %.1f bytes/object", len(objs), usedMemory, float64(usedMemory)/float64(len(objs))) - fs.Logf(nil, "System memory changed from %d to %d bytes a change of %d bytes", before.Sys, after.Sys, after.Sys-before.Sys) + var allocChange int64 + if after.Alloc >= before.Alloc { + allocChange = int64(after.Alloc - before.Alloc) + } else { + allocChange = -int64(before.Alloc - after.Alloc) + } + var sysChange int64 + if after.Sys >= before.Sys { + sysChange = int64(after.Sys - before.Sys) + } else { + sysChange = -int64(before.Sys - after.Sys) + } + if ci.HumanReadable { + objString := fs.CountSuffix(int64(len(objs))) + var usedString string + if after.Alloc >= before.Alloc { + usedString = fs.SizeSuffix(int64(after.Alloc - before.Alloc)).ByteUnit() + } else { + usedString = "-" + fs.SizeSuffix(int64(before.Alloc-after.Alloc)).ByteUnit() + } + avgString := fs.SizeSuffix(allocChange / int64(len(objs))).ByteUnit() + fs.Logf(nil, "%s objects took %s, %s/object", objString, usedString, avgString) + + var sysBeforeString string + if before.Sys <= fs.SizeSuffixMaxValue { + sysBeforeString = fs.SizeSuffix(int64(before.Sys)).String() + } else { + sysBeforeString = ">" + fs.SizeSuffixMax.String() + } + var sysAfterString string + if after.Sys <= fs.SizeSuffixMaxValue { + sysAfterString = fs.SizeSuffix(int64(after.Sys)).ByteUnit() + } else { + sysAfterString = ">" + fs.SizeSuffixMax.ByteUnit() + } + var sysUsedString string + if after.Sys >= before.Sys { + sysUsedString = fs.SizeSuffix(int64(after.Sys - before.Sys)).ByteUnit() + } else { + sysUsedString = "-" + fs.SizeSuffix(int64(before.Sys-after.Sys)).ByteUnit() + } + fs.Logf(nil, "System memory changed from %s to %s a change of %s", sysBeforeString, sysAfterString, sysUsedString) + } else { + fs.Logf(nil, "%d objects took %d bytes, %.1f bytes/object", len(objs), allocChange, float64(allocChange)/float64(len(objs))) + fs.Logf(nil, "System memory changed from %d to %d bytes a change of %d bytes", before.Sys, after.Sys, sysChange) + } return nil }) },