Skip to content

Commit

Permalink
Add HTTP Methods support, Request Body Support
Browse files Browse the repository at this point in the history
- Set default header as Content-Type:application/json
  • Loading branch information
me-heer committed Jan 13, 2024
1 parent 2171ad3 commit 9937310
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
14 changes: 10 additions & 4 deletions load_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/http/httptrace"
"os"
"strings"
"sync/atomic"
"time"
)
Expand All @@ -15,6 +16,8 @@ var (
TotalReq int
Endpoint string
Concurrent int
HttpMethod string
Body string

// Accessed by other files to show results
ReqProgress int
Expand All @@ -33,9 +36,10 @@ var (
)

const (
failed = "failed"
succeeded = "succeeded"
reqPerSecond = "reqPerSecond"
failed = "failed"
succeeded = "succeeded"
reqPerSecond = "reqPerSecond"
totalDuration = "totalDuration"
)

type Response struct {
Expand All @@ -62,7 +66,8 @@ func LoadTest() {
func createRequestJobs(reqPool chan<- *http.Request, url string, numberOfRequests int) {
defer close(reqPool)
for i := 0; i < numberOfRequests; i++ {
r, err := http.NewRequest(http.MethodGet, url, nil)
r, err := http.NewRequest(HttpMethod, url, strings.NewReader(Body))
r.Header.Set("Content-Type", "application/json")
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -96,6 +101,7 @@ func evaluateResponses(responseChannel <-chan *Response) {
results[failed] = fmt.Sprintf("%d", failedCount)
requestsPerSecond := float64(succeededCount) / Elapsed.Seconds()
results[reqPerSecond] = fmt.Sprintf("%f", requestsPerSecond)
results[totalDuration] = Elapsed.String()
}

func startRequestWorkers(requestChannel <-chan *http.Request, responseChannel chan<- *Response, maxConcurrentRequests int) {
Expand Down
17 changes: 17 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"github.com/fatih/color"
"net/http"
url2 "net/url"
"os"
"runtime"
Expand All @@ -18,15 +19,30 @@ func main() {
flag.StringVar(&Endpoint, "url", "", "Endpoint URL for load testing")
flag.IntVar(&TotalReq, "n", defaultNumberOfTotalRequests, "Total number of requests to make")
flag.IntVar(&Concurrent, "c", defaultConcurrentRequests, "Number of Concurrent requests")
flag.StringVar(&HttpMethod, "method", "", "HTTP Method to use while making the request")
flag.StringVar(&Body, "body", "", "JSON Request Body for each Request")
flag.Parse()
if _, err := url2.ParseRequestURI(Endpoint); err != nil {
fmt.Printf("Invalid Endpoint URL: %s\n", err)
flag.PrintDefaults()
os.Exit(-1)
}

if HttpMethod == "" {
HttpMethod = http.MethodGet
}

switch HttpMethod {
case http.MethodGet, http.MethodHead, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodConnect, http.MethodOptions, http.MethodTrace:
default:
fmt.Printf("Invalid Http Method: %s\n", HttpMethod)
os.Exit(-1)
}

println("USING:", runtime.NumCPU(), "CPUs")
println("URL:", Endpoint)
println("HTTP Method:", HttpMethod)
println("Request Body:", Body)
println("Total number of requests:", TotalReq)
println("Parallel requests:", Concurrent)

Expand All @@ -40,4 +56,5 @@ func printResults() {
color.Green("Succeeded Requests: %s", results[succeeded])
color.Red("Failed Requests: %s", results[failed])
color.Cyan("Requests/Second: %s", results[reqPerSecond])
color.Cyan("Completed Load Testing In: %s", results[totalDuration])
}

0 comments on commit 9937310

Please sign in to comment.