Skip to content

"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files

test: add "rclone test histogram" for file name distribution stats

Nick Craig-Wood5b84adf3b983
diff --git a/cmd/all/all.go b/cmd/all/all.go
index 6fae78757..3c899fa56 100644
--- a/cmd/all/all.go
+++ b/cmd/all/all.go
@@ -53,6 +53,7 @@ import (
_ "github.com/rclone/rclone/cmd/size"
_ "github.com/rclone/rclone/cmd/sync"
_ "github.com/rclone/rclone/cmd/test"
+ _ "github.com/rclone/rclone/cmd/test/histogram"
_ "github.com/rclone/rclone/cmd/test/info"
_ "github.com/rclone/rclone/cmd/test/makefiles"
_ "github.com/rclone/rclone/cmd/test/memory"
diff --git a/cmd/test/histogram/histogram.go b/cmd/test/histogram/histogram.go
new file mode 100644
index 000000000..d4f02d5d9
--- /dev/null
+++ b/cmd/test/histogram/histogram.go
@@ -0,0 +1,59 @@
+package histogram
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "os"
+ "path"
+
+ "github.com/rclone/rclone/cmd"
+ "github.com/rclone/rclone/cmd/test"
+ "github.com/rclone/rclone/fs"
+ "github.com/rclone/rclone/fs/walk"
+ "github.com/spf13/cobra"
+)
+
+func init() {
+ test.Command.AddCommand(commandDefinition)
+}
+
+var commandDefinition = &cobra.Command{
+ Use: "histogram [remote:path]",
+ Short: `Makes a histogram of file name characters.`,
+ Long: `This command outputs JSON which shows the histogram of characters used
+in filenames in the remote:path specified.
+
+The data doesn't contain any identifying information but is useful for
+the rclone developers when developing filename compression.
+`,
+ Run: func(command *cobra.Command, args []string) {
+ cmd.CheckArgs(1, 1, command, args)
+ f := cmd.NewFsDir(args)
+ ctx := context.Background()
+ ci := fs.GetConfig(ctx)
+ cmd.Run(false, false, command, func() error {
+ var hist [256]int64
+ err := walk.ListR(ctx, f, "", false, ci.MaxDepth, walk.ListObjects, func(entries fs.DirEntries) error {
+ for _, entry := range entries {
+ base := path.Base(entry.Remote())
+ for i := range base {
+ hist[base[i]]++
+ }
+ }
+ return nil
+ })
+ if err != nil {
+ return err
+ }
+ enc := json.NewEncoder(os.Stdout)
+ // enc.SetIndent("", "\t")
+ err = enc.Encode(&hist)
+ if err != nil {
+ return err
+ }
+ fmt.Println()
+ return nil
+ })
+ },
+}