1
0
Fork 0
loadr/main.go

206 lines
5.8 KiB
Go
Raw Permalink Normal View History

2024-01-14 16:13:45 -07:00
package main
import (
2024-01-14 19:47:56 -07:00
"bytes"
2024-01-14 16:13:45 -07:00
"flag"
"fmt"
2024-01-14 19:47:56 -07:00
"io"
"math"
2024-01-14 16:13:45 -07:00
"net/http"
2024-01-14 19:47:56 -07:00
"os"
2024-01-15 00:47:48 -07:00
"path/filepath"
2024-01-14 19:47:56 -07:00
"strings"
"sync"
2024-01-14 16:13:45 -07:00
"sync/atomic"
"time"
)
2024-01-14 19:47:56 -07:00
// Global HTTP client used for making requests.
2024-01-14 16:13:45 -07:00
var client = &http.Client{}
// PerformanceMetrics holds the metrics for performance evaluation.
type PerformanceMetrics struct {
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) {
2024-01-14 19:47:56 -07:00
startTime := time.Now()
// Create a new request with the provided verb, URL, and JSON data if provided.
var req *http.Request
var err error
if jsonData != nil {
req, err = http.NewRequest(verb, url, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
} else {
req, err = http.NewRequest(verb, url, nil)
}
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// Add the bearer token to the request's Authorization header if provided.
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
// Send the request.
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error making request:", err)
return
}
defer resp.Body.Close()
// Calculate the duration of the request.
duration := time.Since(startTime)
// Update the performance metrics.
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)
2024-01-14 16:13:45 -07:00
if err != nil {
2024-01-14 19:47:56 -07:00
fmt.Println("Error reading response body:", err)
2024-01-14 16:13:45 -07:00
return
}
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
// Define command-line flags for configuring the load test.
2024-01-14 16:13:45 -07:00
requestsPerSecond := flag.Float64("rate", 10, "Number of requests per second")
maxRequests := flag.Int("max", 50, "Maximum number of requests to send (0 for unlimited)")
2024-01-14 16:13:45 -07:00
url := flag.String("url", "https://example.com", "The URL to make requests to")
2024-01-14 19:47:56 -07:00
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-14 16:13:45 -07:00
2024-01-14 19:47:56 -07:00
// Parse the command-line flags.
2024-01-14 16:13:45 -07:00
flag.Parse()
// Ensure maxRequests is greater than 0.
if *maxRequests <= 0 {
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.
jsonData, err := readJSONFile(*jsonFilePath)
if err != nil {
fmt.Println("Error reading JSON file:", err)
return
}
// Calculate the rate limit based on the requests per second.
2024-01-14 16:13:45 -07:00
rateLimit := time.Second / time.Duration(*requestsPerSecond)
ticker := time.NewTicker(rateLimit)
defer ticker.Stop()
2024-01-14 19:47:56 -07:00
// Initialize the request count.
2024-01-14 16:13:45 -07:00
var requestCount int32 = 0
// Wait for all goroutines to finish.
var wg sync.WaitGroup
// Log beginning of requests
fmt.Println("Starting Loadr Requests...")
2024-01-14 19:47:56 -07:00
// Start sending requests at the specified rate.
startTime := time.Now()
2024-01-14 16:13:45 -07:00
for range ticker.C {
second := int(time.Since(startTime).Seconds())
if int(requestCount) >= *maxRequests {
2024-01-14 19:47:56 -07:00
break
}
wg.Add(1)
go func(u, t, verb string, data []byte, sec int) {
defer wg.Done()
makeRequest(verb, u, t, data, sec)
2024-01-14 19:47:56 -07:00
atomic.AddInt32(&requestCount, 1)
}(*url, *bearerToken, strings.ToUpper(*requestType), jsonData, second)
}
wg.Wait() // Wait for all requests to finish.
// 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
2024-01-14 16:13:45 -07:00
}
2024-01-14 19:47:56 -07:00
2024-01-15 00:47:48 -07:00
// Format the results
results := fmt.Sprintln("Performance Metrics:")
results += fmt.Sprintf("Total Requests Sent: %d\n", metrics.totalRequests)
results += fmt.Sprintf("Total Responses Received: %d\n", totalResponses)
results += fmt.Sprintf("Average Latency: %s\n", averageLatency)
results += fmt.Sprintf("Max Latency: %s\n", metrics.maxLatency)
results += fmt.Sprintf("Min Latency: %s\n", metrics.minLatency)
results += fmt.Sprintf("Requests Per Second (Sent): %.2f\n", float64(*requestsPerSecond))
results += fmt.Sprintf("Responses Per Second (Received): %.2f\n", float64(totalResponses)/totalDuration)
// Print the results to the console
fmt.Print(results)
// Ensure the .reports directory exists
reportsDir := ".reports"
if _, err := os.Stat(reportsDir); os.IsNotExist(err) {
err := os.Mkdir(reportsDir, 0755)
if err != nil {
fmt.Println("Error creating reports directory:", err)
return
}
}
// Save the results to a file in the .reports directory
timestamp := time.Now().Format("20060102-150405") // YYYYMMdd-HHmmss format
fileName := fmt.Sprintf("%s.txt", timestamp)
filePath := filepath.Join(reportsDir, fileName)
if err := os.WriteFile(filePath, []byte(results), 0644); err != nil {
fmt.Println("Error writing results to file:", err)
return
}
2024-01-14 16:13:45 -07:00
}