1
0
Fork 0

Added performance monitoring and modified

This commit is contained in:
Atridad Lahiji 2024-01-15 00:33:58 -07:00
parent 6ff8c79a58
commit 83e35f17a7
No known key found for this signature in database
2 changed files with 84 additions and 18 deletions

View file

@ -14,7 +14,7 @@ Example using binary:
Flags: Flags:
- `-rate`: Number of requests per second. Default is 10. - `-rate`: Number of requests per second. Default is 10.
- `-max`: Maximum number of requests to send. If 0, there is no limit. Default is 0. - `-max`: Maximum number of requests to send. Must be a non-zero integer. Default is 50.
- `-url`: The URL to make requests to. Default is "https://example.com". - `-url`: The URL to make requests to. Default is "https://example.com".
- `-type`: Type of HTTP request. Can be GET, POST, PUT, DELETE, etc. Default is "GET". - `-type`: Type of HTTP request. Can be GET, POST, PUT, DELETE, etc. Default is "GET".
- `-json`: Path to the JSON file with request data. If not provided, no data is sent with the requests. - `-json`: Path to the JSON file with request data. If not provided, no data is sent with the requests.

100
main.go
View file

@ -5,9 +5,11 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"math"
"net/http" "net/http"
"os" "os"
"strings" "strings"
"sync"
"sync/atomic" "sync/atomic"
"time" "time"
) )
@ -15,9 +17,26 @@ import (
// Global HTTP client used for making requests. // Global HTTP client used for making requests.
var client = &http.Client{} var client = &http.Client{}
// makeRequest sends an HTTP request with the specified verb, URL, bearer token, and JSON data. // PerformanceMetrics holds the metrics for performance evaluation.
// It prints out the status code, response time, and response size. type PerformanceMetrics struct {
func makeRequest(verb, url, token string, jsonData []byte) { mu sync.Mutex // Protects the metrics
totalRequests int32
totalResponses int32
totalLatency time.Duration
maxLatency time.Duration
minLatency time.Duration
responseCounters map[int]int32
}
// Initialize the metrics with default values.
var metrics = PerformanceMetrics{
minLatency: time.Duration(math.MaxInt64),
responseCounters: make(map[int]int32),
}
// makeRequest sends an HTTP request and updates performance metrics.
func makeRequest(verb, url, token string, jsonData []byte, second int) {
startTime := time.Now() startTime := time.Now()
// Create a new request with the provided verb, URL, and JSON data if provided. // Create a new request with the provided verb, URL, and JSON data if provided.
@ -50,15 +69,28 @@ func makeRequest(verb, url, token string, jsonData []byte) {
// Calculate the duration of the request. // Calculate the duration of the request.
duration := time.Since(startTime) duration := time.Since(startTime)
// Read the response body to determine its size. // Update the performance metrics.
body, err := io.ReadAll(resp.Body) metrics.mu.Lock()
metrics.totalRequests++
metrics.totalLatency += duration
if duration > metrics.maxLatency {
metrics.maxLatency = duration
}
if duration < metrics.minLatency {
metrics.minLatency = duration
}
if resp.StatusCode == http.StatusOK {
metrics.totalResponses++
metrics.responseCounters[second]++
}
metrics.mu.Unlock()
// Read the response body to determine its size (not shown in the output).
_, err = io.ReadAll(resp.Body)
if err != nil { if err != nil {
fmt.Println("Error reading response body:", err) fmt.Println("Error reading response body:", err)
return return
} }
// Print out the request details.
fmt.Printf("Request Sent. Status Code: %d, Duration: %s, Response Size: %d bytes\n", resp.StatusCode, duration, len(body))
} }
// readJSONFile reads the contents of the JSON file at the given path and returns the bytes. // readJSONFile reads the contents of the JSON file at the given path and returns the bytes.
@ -72,7 +104,7 @@ func readJSONFile(filePath string) ([]byte, error) {
func main() { func main() {
// Define command-line flags for configuring the load test. // Define command-line flags for configuring the load test.
requestsPerSecond := flag.Float64("rate", 10, "Number of requests per second") requestsPerSecond := flag.Float64("rate", 10, "Number of requests per second")
maxRequests := flag.Int("max", 0, "Maximum number of requests to send (0 for unlimited)") 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") 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.)") 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") jsonFilePath := flag.String("json", "", "Path to the JSON file with request data")
@ -81,6 +113,12 @@ func main() {
// Parse the command-line flags. // Parse the command-line flags.
flag.Parse() flag.Parse()
// Ensure maxRequests is greater than 0.
if *maxRequests <= 0 {
fmt.Println("Error: max must be an integer greater than 0")
return
}
// Read the JSON file if the path is provided. // Read the JSON file if the path is provided.
jsonData, err := readJSONFile(*jsonFilePath) jsonData, err := readJSONFile(*jsonFilePath)
if err != nil { if err != nil {
@ -96,19 +134,47 @@ func main() {
// Initialize the request count. // Initialize the request count.
var requestCount int32 = 0 var requestCount int32 = 0
// Wait for all goroutines to finish.
var wg sync.WaitGroup
// Log beginning of requests
fmt.Println("Starting Loadr Requests...")
// Start sending requests at the specified rate. // Start sending requests at the specified rate.
startTime := time.Now()
for range ticker.C { for range ticker.C {
// Stop if the maximum number of requests is reached. second := int(time.Since(startTime).Seconds())
if *maxRequests > 0 && int(requestCount) >= *maxRequests { if int(requestCount) >= *maxRequests {
break break
} }
// Send the request in a new goroutine. wg.Add(1)
go func(u, t, verb string, data []byte) { go func(u, t, verb string, data []byte, sec int) {
makeRequest(verb, u, t, data) defer wg.Done()
makeRequest(verb, u, t, data, sec)
atomic.AddInt32(&requestCount, 1) atomic.AddInt32(&requestCount, 1)
}(*url, *bearerToken, strings.ToUpper(*requestType), jsonData) }(*url, *bearerToken, strings.ToUpper(*requestType), jsonData, second)
} }
// Print out the total number of requests sent after the load test is finished. wg.Wait() // Wait for all requests to finish.
fmt.Printf("Finished sending requests. Total requests: %d\n", requestCount)
// Calculate and print performance metrics.
averageLatency := time.Duration(0)
if metrics.totalRequests > 0 {
averageLatency = metrics.totalLatency / time.Duration(metrics.totalRequests)
}
totalDuration := time.Since(startTime).Seconds()
totalResponses := int32(0)
for _, count := range metrics.responseCounters {
totalResponses += count
}
fmt.Printf("Performance Metrics:\n")
fmt.Printf("Total Requests Sent: %d\n", metrics.totalRequests)
fmt.Printf("Total Responses Received: %d\n", totalResponses)
fmt.Printf("Average Latency: %s\n", averageLatency)
fmt.Printf("Max Latency: %s\n", metrics.maxLatency)
fmt.Printf("Min Latency: %s\n", metrics.minLatency)
fmt.Printf("Requests Per Second (Sent): %.2f\n", float64(*requestsPerSecond))
fmt.Printf("Responses Per Second (Received): %.2f\n", float64(totalResponses)/totalDuration)
} }