2024-01-14 16:13:45 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2024-01-16 09:27:38 -07:00
|
|
|
"loadr/lib"
|
2024-01-14 19:47:56 -07:00
|
|
|
"os"
|
2024-01-14 16:13:45 -07:00
|
|
|
)
|
|
|
|
|
2024-01-17 01:03:04 -07:00
|
|
|
var version string = "1.0.2"
|
2024-01-16 09:57:07 -07:00
|
|
|
|
2024-01-16 09:27:38 -07:00
|
|
|
func parseCommandLine() (float64, int, string, string, string, string) {
|
|
|
|
requestsPerSecond := flag.Float64("rate", 10, "Number of requests per second")
|
|
|
|
maxRequests := flag.Int("max", 50, "Maximum number of requests to send (0 for unlimited)")
|
|
|
|
url := flag.String("url", "https://example.com", "The URL to make requests to")
|
|
|
|
requestType := flag.String("type", "GET", "Type of HTTP request (GET, POST, PUT, DELETE, etc.)")
|
|
|
|
jsonFilePath := flag.String("json", "", "Path to the JSON file with request data")
|
|
|
|
bearerToken := flag.String("token", "", "Bearer token for authorization")
|
2024-01-16 09:57:07 -07:00
|
|
|
versionFlag := flag.Bool("version", false, "Print the version and exit")
|
|
|
|
versionFlagShort := flag.Bool("v", false, "Print the version and exit")
|
2024-01-14 19:47:56 -07:00
|
|
|
|
2024-01-16 09:27:38 -07:00
|
|
|
// Parse the command-line flags.
|
|
|
|
flag.Parse()
|
2024-01-15 00:33:58 -07:00
|
|
|
|
2024-01-16 09:57:07 -07:00
|
|
|
// If the version flag is present, print the version number and exit.
|
|
|
|
if *versionFlag || *versionFlagShort {
|
|
|
|
fmt.Println("Version:", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-01-16 09:27:38 -07:00
|
|
|
return *requestsPerSecond, *maxRequests, *url, *requestType, *jsonFilePath, *bearerToken
|
2024-01-14 19:47:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// readJSONFile reads the contents of the JSON file at the given path and returns the bytes.
|
|
|
|
func readJSONFile(filePath string) ([]byte, error) {
|
|
|
|
if filePath == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return os.ReadFile(filePath)
|
2024-01-14 16:13:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2024-01-14 19:47:56 -07:00
|
|
|
// Parse the command-line flags.
|
2024-01-16 09:27:38 -07:00
|
|
|
requestsPerSecond, maxRequests, url, requestType, jsonFilePath, bearerToken := parseCommandLine()
|
2024-01-14 16:13:45 -07:00
|
|
|
|
2024-01-15 00:33:58 -07:00
|
|
|
// Ensure maxRequests is greater than 0.
|
2024-01-16 09:27:38 -07:00
|
|
|
if maxRequests <= 0 {
|
2024-01-15 00:33:58 -07:00
|
|
|
fmt.Println("Error: max must be an integer greater than 0")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-14 19:47:56 -07:00
|
|
|
// Read the JSON file if the path is provided.
|
2024-01-16 09:27:38 -07:00
|
|
|
jsonData, err := readJSONFile(jsonFilePath)
|
2024-01-14 19:47:56 -07:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error reading JSON file:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 09:27:38 -07:00
|
|
|
lib.SendRequests(url, bearerToken, requestType, jsonData, maxRequests, requestsPerSecond)
|
2024-01-14 16:13:45 -07:00
|
|
|
}
|