test: consider global option for printing human-readable sizes and avoid unsigned integer overflow

This commit is contained in:
albertony 2021-04-06 14:10:11 +02:00
parent 8c4b06da79
commit 8500d95579

View File

@ -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
})
},