2024-12-04 16:40:52 -06:00
|
|
|
package lib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Global metrics instance to track performance during load testing
|
2024-12-04 16:40:52 -06:00
|
|
|
var metrics = PerformanceMetrics{
|
2024-12-04 17:26:34 -06:00
|
|
|
// Initialize MinLatency to the maximum possible duration
|
|
|
|
MinLatency: time.Duration(math.MaxInt64),
|
|
|
|
// Initialize response counters map
|
2024-12-04 16:40:52 -06:00
|
|
|
ResponseCounters: make(map[int]int32),
|
|
|
|
}
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// UpdateMetrics synchronously updates performance metrics for each request
|
|
|
|
// - duration: time taken to complete the request
|
|
|
|
// - resp: HTTP response from the request
|
|
|
|
// - second: elapsed seconds since the start of the test
|
2024-12-04 16:40:52 -06:00
|
|
|
func UpdateMetrics(duration time.Duration, resp *http.Response, second int) {
|
|
|
|
metrics.Mu.Lock()
|
|
|
|
defer metrics.Mu.Unlock()
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Increment total requests
|
2024-12-04 16:40:52 -06:00
|
|
|
metrics.TotalRequests++
|
2024-12-04 17:26:34 -06:00
|
|
|
// Add current request's latency to total
|
2024-12-04 16:40:52 -06:00
|
|
|
metrics.TotalLatency += duration
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Update maximum latency if current duration is higher
|
2024-12-04 16:40:52 -06:00
|
|
|
if duration > metrics.MaxLatency {
|
|
|
|
metrics.MaxLatency = duration
|
|
|
|
}
|
2024-12-04 17:26:34 -06:00
|
|
|
// Update minimum latency if current duration is lower
|
2024-12-04 16:40:52 -06:00
|
|
|
if duration < metrics.MinLatency {
|
|
|
|
metrics.MinLatency = duration
|
|
|
|
}
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Track successful responses
|
2024-12-04 16:40:52 -06:00
|
|
|
if resp.StatusCode == http.StatusOK {
|
|
|
|
metrics.TotalResponses++
|
|
|
|
metrics.ResponseCounters[second]++
|
|
|
|
}
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Debug log of current metrics
|
2024-12-04 16:40:52 -06:00
|
|
|
fmt.Printf("Current metrics - Total Requests: %d, Total Responses: %d\n",
|
|
|
|
metrics.TotalRequests, metrics.TotalResponses)
|
|
|
|
}
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// CalculateAndPrintMetrics generates a comprehensive report of load test performance
|
|
|
|
// Parameters:
|
|
|
|
// - startTime: when the load test began
|
|
|
|
// - requestsPerSecond: target request rate
|
|
|
|
// - endpoint: URL being tested
|
|
|
|
// - patterns: request patterns used in the test
|
2024-12-04 16:40:52 -06:00
|
|
|
func CalculateAndPrintMetrics(startTime time.Time, requestsPerSecond float64, endpoint string, patterns []RequestPattern) {
|
2024-12-04 17:26:34 -06:00
|
|
|
// Small delay to ensure all metrics are captured
|
2024-12-04 16:40:52 -06:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
metrics.Mu.Lock()
|
|
|
|
defer metrics.Mu.Unlock()
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Calculate average latency
|
2024-12-04 16:40:52 -06:00
|
|
|
averageLatency := time.Duration(0)
|
|
|
|
if metrics.TotalRequests > 0 {
|
|
|
|
averageLatency = metrics.TotalLatency / time.Duration(metrics.TotalRequests)
|
|
|
|
}
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Calculate total test duration and total responses
|
2024-12-04 16:40:52 -06:00
|
|
|
totalDuration := time.Since(startTime).Seconds()
|
|
|
|
totalResponses := int32(0)
|
|
|
|
for _, count := range metrics.ResponseCounters {
|
|
|
|
totalResponses += count
|
|
|
|
}
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Ensure MinLatency is not left at its initial max value
|
2024-12-04 16:40:52 -06:00
|
|
|
if metrics.MinLatency == time.Duration(math.MaxInt64) {
|
|
|
|
metrics.MinLatency = 0
|
|
|
|
}
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Build detailed results string
|
2024-12-04 16:40:52 -06:00
|
|
|
results := fmt.Sprintf("Load Test Report\n")
|
|
|
|
results += fmt.Sprintf("=============\n\n")
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Report endpoint and request pattern
|
2024-12-04 16:40:52 -06:00
|
|
|
results += fmt.Sprintf("Endpoint: %s\n", endpoint)
|
|
|
|
results += fmt.Sprintf("Pattern: ")
|
2024-12-04 17:26:34 -06:00
|
|
|
|
2024-12-04 16:40:52 -06:00
|
|
|
for i, p := range patterns {
|
|
|
|
if i > 0 {
|
|
|
|
results += " → "
|
|
|
|
}
|
2024-12-04 17:26:34 -06:00
|
|
|
|
|
|
|
var patternDesc string
|
|
|
|
if p.Percentage > 0 && p.Percentage < 100 {
|
|
|
|
// Probabilistic pattern (e.g., "20%p80%g")
|
|
|
|
patternDesc = fmt.Sprintf("%.0f%%%s", p.Percentage, strings.ToLower(p.Verb[:1]))
|
|
|
|
} else {
|
|
|
|
// Simple or sequential pattern (e.g., "5p", "3g", "1p5g")
|
|
|
|
patternDesc = fmt.Sprintf("%d%s", p.Sequence, strings.ToLower(p.Verb[:1]))
|
|
|
|
}
|
|
|
|
|
|
|
|
results += patternDesc
|
2024-12-04 16:40:52 -06:00
|
|
|
}
|
|
|
|
results += "\n\n"
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Detailed performance metrics
|
2024-12-04 16:40:52 -06:00
|
|
|
results += fmt.Sprintf("Performance Metrics\n")
|
|
|
|
results += fmt.Sprintf("-----------------\n")
|
|
|
|
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/sec (Target): %.2f\n", requestsPerSecond)
|
|
|
|
results += fmt.Sprintf("Requests/sec (Actual): %.2f\n", float64(metrics.TotalRequests)/totalDuration)
|
|
|
|
results += fmt.Sprintf("Responses/sec: %.2f\n", float64(totalResponses)/totalDuration)
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Print and save the report
|
2024-12-04 16:40:52 -06:00
|
|
|
fmt.Println(results)
|
|
|
|
saveReport(results)
|
|
|
|
}
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// saveReport writes the load test results to a timestamped file in the .reports directory
|
|
|
|
// Parameters:
|
|
|
|
// - results: formatted results string to be saved
|
2024-12-04 16:40:52 -06:00
|
|
|
func saveReport(results string) {
|
2024-12-04 17:26:34 -06:00
|
|
|
// Ensure .reports directory exists
|
2024-12-04 16:40:52 -06:00
|
|
|
resultsDir := ".reports"
|
|
|
|
os.MkdirAll(resultsDir, os.ModePerm)
|
|
|
|
|
2024-12-04 17:26:34 -06:00
|
|
|
// Create a unique filename based on current timestamp
|
2024-12-04 16:40:52 -06:00
|
|
|
resultsFile := filepath.Join(resultsDir, fmt.Sprintf("%d.txt", time.Now().Unix()))
|
2024-12-04 17:26:34 -06:00
|
|
|
|
|
|
|
// Write results to file
|
2024-12-04 16:40:52 -06:00
|
|
|
if err := os.WriteFile(resultsFile, []byte(results), 0644); err != nil {
|
|
|
|
fmt.Println("Error saving report:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Report saved:", resultsFile)
|
|
|
|
}
|