BenchmarkClientCommand is the entrypoint for the benchmark client command.
(cfg *config.Config)
| 39 | |
| 40 | // BenchmarkClientCommand is the entrypoint for the benchmark client command. |
| 41 | func BenchmarkClientCommand(cfg *config.Config) *cobra.Command { |
| 42 | benchClientCmd := &cobra.Command{ |
| 43 | Use: "client", |
| 44 | Short: "Start a client that continuously makes web requests and prints stats. The options mimic curl, but we default to PROPFIND requests.", |
| 45 | RunE: func(cmd *cobra.Command, args []string) error { |
| 46 | jobs, err := cmd.Flags().GetInt("jobs") |
| 47 | if err != nil { |
| 48 | return err |
| 49 | } |
| 50 | insecure, _ := cmd.Flags().GetBool("insecure") |
| 51 | opt := clientOptions{ |
| 52 | url: args[0], |
| 53 | insecure: insecure, |
| 54 | jobs: jobs, |
| 55 | headers: make(map[string]string), |
| 56 | } |
| 57 | |
| 58 | if d, _ := cmd.Flags().GetString("data-raw"); d != "" { |
| 59 | opt.request = "POST" |
| 60 | opt.headers["Content-Type"] = "application/x-www-form-urlencoded" |
| 61 | opt.data = []byte(d) |
| 62 | } |
| 63 | |
| 64 | if d, _ := cmd.Flags().GetString("data"); d != "" { |
| 65 | opt.request = "POST" |
| 66 | opt.headers["Content-Type"] = "application/x-www-form-urlencoded" |
| 67 | if strings.HasPrefix(d, "@") { |
| 68 | filePath := strings.TrimPrefix(d, "@") |
| 69 | var data []byte |
| 70 | var err error |
| 71 | |
| 72 | // read from file or stdin and trim trailing newlines |
| 73 | if filePath == "-" { |
| 74 | data, err = os.ReadFile("/dev/stdin") |
| 75 | } else { |
| 76 | data, err = os.ReadFile(filePath) |
| 77 | } |
| 78 | if err != nil { |
| 79 | log.Fatal(errors.New("could not read data from file '" + filePath + "': " + err.Error())) |
| 80 | } |
| 81 | |
| 82 | // clean byte array similar to curl's --data parameter |
| 83 | // It removes leading/trailing whitespace and converts line breaks to spaces |
| 84 | |
| 85 | // Trim leading and trailing whitespace |
| 86 | data = bytes.TrimSpace(data) |
| 87 | |
| 88 | // Replace newlines and carriage returns with spaces |
| 89 | data = bytes.ReplaceAll(data, []byte("\r\n"), []byte(" ")) |
| 90 | data = bytes.ReplaceAll(data, []byte("\n"), []byte(" ")) |
| 91 | data = bytes.ReplaceAll(data, []byte("\r"), []byte(" ")) |
| 92 | |
| 93 | // Replace multiple spaces with single space |
| 94 | for bytes.Contains(data, []byte(" ")) { |
| 95 | data = bytes.ReplaceAll(data, []byte(" "), []byte(" ")) |
| 96 | } |
| 97 | |
| 98 | opt.data = data |
no test coverage detected