Repository: rclone/rclone
File: cmd/test/makefiles/speed.go (Go)

Code before:
	"github.com/rclone/rclone/lib/atexit"
	"github.com/rclone/rclone/lib/random"
	"github.com/spf13/cobra"
)

var (
	// Flags
	testTime = fs.Duration(15 * time.Second)
	fcap     = 100
	small    = fs.SizeSuffix(1024)
	medium   = fs.SizeSuffix(10 * 1024 * 1024)
	large    = fs.SizeSuffix(1024 * 1024 * 1024)
	useJSON  = false
)

func init() {
	test.Command.AddCommand(speedCmd)

	speedFlags := speedCmd.Flags()
	flags.FVarP(speedFlags, &testTime, "test-time", "", "Length for each test to run", "")
	flags.IntVarP(speedFlags, &fcap, "file-cap", "", fcap, "Maximum number of files to use in each test", "")
	flags.FVarP(speedFlags, &small, "small", "", "Size of small files", "")
	flags.FVarP(speedFlags, &medium, "medium", "", "Size of medium files", "")
	flags.FVarP(speedFlags, &large, "large", "", "Size of large files", "")
	flags.BoolVarP(speedFlags, &useJSON, "json", "", useJSON, "Output only results in JSON format", "")

}

var speedCmd = &cobra.Command{
	Use:   "speed <remote> [flags]",
	Short: `Run a speed test to the remote`,
	Long: `Run a speed test to the remote.

	This command runs a series of uploads and downloads to the remote, measuring
	and printing the speed of each test using varying file sizes and numbers of
	files.

	Test time can be innaccurate with small file caps and large files. As it
	uses the results of an initial test to determine how many files to use in
	each subsequent test.

	It is recommended to use -q flag for a simpler output.

	**NB** This command will create and delete files on the remote in a randomly
	named directory which should be tidied up after.

	You can use the --json flag to only print the results in JSON format.`,
	Annotations: map[string]string{
		"versionIntroduced": "v1.72",
	},
	RunE: func(command *cobra.Command, args []string) error {

Review comment (suggestion):
Can you factor this bit of code into a function - say `addCommonFlags`

https://github.com/rclone/rclone/blob/0019e18ac3a673714e851b2a97d63a8ad0a77654/cmd/test/makefiles/makefiles.go#L64-L72

Then call it here `addCommonFlags(speedFlags)` so this command has the flags for controlling the contents of the test files too.

Code after:
	"github.com/rclone/rclone/lib/atexit"
	"github.com/rclone/rclone/lib/random"
	"github.com/spf13/cobra"
)

var (
	// Flags
	testTime = fs.Duration(15 * time.Second)
	fcap     = 100
	small    = fs.SizeSuffix(1024)
	medium   = fs.SizeSuffix(10 * 1024 * 1024)
	large    = fs.SizeSuffix(1024 * 1024 * 1024)
	useJSON  = false
)

func init() {
	test.Command.AddCommand(speedCmd)

	speedFlags := speedCmd.Flags()
	flags.FVarP(speedFlags, &testTime, "test-time", "", "Length for each test to run", "")
	flags.IntVarP(speedFlags, &fcap, "file-cap", "", fcap, "Maximum number of files to use in each test", "")
	flags.FVarP(speedFlags, &small, "small", "", "Size of small files", "")
	flags.FVarP(speedFlags, &medium, "medium", "", "Size of medium files", "")
	flags.FVarP(speedFlags, &large, "large", "", "Size of large files", "")
	flags.BoolVarP(speedFlags, &useJSON, "json", "", useJSON, "Output only results in JSON format", "")

	addCommonFlags(speedFlags)
}

func logf(text string, args ...any) {
	if !useJSON {
		fmt.Printf(text, args...)
	}
}

var speedCmd = &cobra.Command{
	Use:   "speed <remote> [flags]",
	Short: `Run a speed test to the remote`,
	Long: `Run a speed test to the remote.

	This command runs a series of uploads and downloads to the remote, measuring
	and printing the speed of each test using varying file sizes and numbers of
	files.

	Test time can be innaccurate with small file caps and large files. As it
	uses the results of an initial test to determine how many files to use in
	each subsequent test.

	It is recommended to use -q flag for a simpler output. e.g.:
	
	    rlone test speed remote: -q