2016-08-05 18:12:27 +02:00
|
|
|
package memtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/cmd"
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/ncw/rclone/fs/operations"
|
2016-08-05 18:12:27 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-10-22 13:05:45 +02:00
|
|
|
cmd.Root.AddCommand(commandDefintion)
|
2016-08-05 18:12:27 +02:00
|
|
|
}
|
|
|
|
|
2016-10-22 13:05:45 +02:00
|
|
|
var commandDefintion = &cobra.Command{
|
2016-08-05 18:12:27 +02:00
|
|
|
Use: "memtest remote:path",
|
|
|
|
Short: `Load all the objects at remote:path and report memory stats.`,
|
|
|
|
Hidden: true,
|
|
|
|
Run: func(command *cobra.Command, args []string) {
|
|
|
|
cmd.CheckArgs(1, 1, command, args)
|
|
|
|
fsrc := cmd.NewFsSrc(args)
|
2016-12-04 17:52:24 +01:00
|
|
|
cmd.Run(false, false, command, func() error {
|
2018-01-12 17:30:54 +01:00
|
|
|
objects, _, err := operations.Count(fsrc)
|
2016-08-05 18:12:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
objs := make([]fs.Object, 0, objects)
|
|
|
|
var before, after runtime.MemStats
|
|
|
|
runtime.GC()
|
|
|
|
runtime.ReadMemStats(&before)
|
|
|
|
var mu sync.Mutex
|
2018-01-12 17:30:54 +01:00
|
|
|
err = operations.ListFn(fsrc, func(o fs.Object) {
|
2016-08-05 18:12:27 +02:00
|
|
|
mu.Lock()
|
|
|
|
objs = append(objs, o)
|
|
|
|
mu.Unlock()
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
runtime.GC()
|
|
|
|
runtime.ReadMemStats(&after)
|
|
|
|
usedMemory := after.Alloc - before.Alloc
|
2017-02-09 12:01:20 +01:00
|
|
|
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)
|
2016-08-05 18:12:27 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
},
|
|
|
|
}
|