Re-work
This commit is contained in:
104
lib/metrics.go
Normal file
104
lib/metrics.go
Normal file
@ -0,0 +1,104 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
var metrics = PerformanceMetrics{
|
||||
MinLatency: time.Duration(math.MaxInt64),
|
||||
ResponseCounters: make(map[int]int32),
|
||||
}
|
||||
|
||||
func UpdateMetrics(duration time.Duration, resp *http.Response, second int) {
|
||||
metrics.Mu.Lock()
|
||||
defer metrics.Mu.Unlock()
|
||||
|
||||
fmt.Printf("Updating metrics - Duration: %v, Status: %d\n", duration, resp.StatusCode) // Debug log
|
||||
|
||||
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]++
|
||||
}
|
||||
|
||||
// Debug log
|
||||
fmt.Printf("Current metrics - Total Requests: %d, Total Responses: %d\n",
|
||||
metrics.TotalRequests, metrics.TotalResponses)
|
||||
}
|
||||
|
||||
func CalculateAndPrintMetrics(startTime time.Time, requestsPerSecond float64, endpoint string, patterns []RequestPattern) {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
metrics.Mu.Lock()
|
||||
defer metrics.Mu.Unlock()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if metrics.MinLatency == time.Duration(math.MaxInt64) {
|
||||
metrics.MinLatency = 0
|
||||
}
|
||||
|
||||
results := fmt.Sprintf("Load Test Report\n")
|
||||
results += fmt.Sprintf("=============\n\n")
|
||||
|
||||
results += fmt.Sprintf("Endpoint: %s\n", endpoint)
|
||||
results += fmt.Sprintf("Pattern: ")
|
||||
for i, p := range patterns {
|
||||
if i > 0 {
|
||||
results += " → "
|
||||
}
|
||||
results += fmt.Sprintf("%d%s", p.Sequence, strings.ToLower(p.Verb[:1]))
|
||||
}
|
||||
results += "\n\n"
|
||||
|
||||
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)
|
||||
|
||||
fmt.Println(results)
|
||||
saveReport(results)
|
||||
}
|
||||
|
||||
func saveReport(results string) {
|
||||
resultsDir := ".reports"
|
||||
os.MkdirAll(resultsDir, os.ModePerm)
|
||||
|
||||
resultsFile := filepath.Join(resultsDir, fmt.Sprintf("%d.txt", time.Now().Unix()))
|
||||
if err := os.WriteFile(resultsFile, []byte(results), 0644); err != nil {
|
||||
fmt.Println("Error saving report:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Report saved:", resultsFile)
|
||||
}
|
112
lib/requests.go
Normal file
112
lib/requests.go
Normal file
@ -0,0 +1,112 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"math"
|
||||
"math/rand/v2"
|
||||
"net/http"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
var client = &http.Client{}
|
||||
|
||||
func (e *RequestError) Error() string {
|
||||
return fmt.Sprintf("error making %s request to %s: %v", e.Verb, e.URL, e.Err)
|
||||
}
|
||||
|
||||
func makeRequest(verb, url, token string, jsonData []byte, second int) error {
|
||||
startTime := time.Now()
|
||||
|
||||
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 {
|
||||
return &RequestError{Verb: verb, URL: url, Err: err}
|
||||
}
|
||||
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return &RequestError{Verb: verb, URL: url, Err: err}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
duration := time.Since(startTime)
|
||||
|
||||
UpdateMetrics(duration, resp, second)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SendRequests(url string, patterns []RequestPattern, maxRequests int, requestsPerSecond float64, token string, jsonData []byte) {
|
||||
metrics = PerformanceMetrics{
|
||||
MinLatency: time.Duration(math.MaxInt64),
|
||||
ResponseCounters: make(map[int]int32),
|
||||
}
|
||||
|
||||
startTime := time.Now()
|
||||
rateLimit := time.Second / time.Duration(requestsPerSecond)
|
||||
ticker := time.NewTicker(rateLimit)
|
||||
defer ticker.Stop()
|
||||
|
||||
var requestCount int32
|
||||
var wg sync.WaitGroup
|
||||
|
||||
patternIndex := 0
|
||||
sequenceCount := 0
|
||||
|
||||
for range ticker.C {
|
||||
if int(requestCount) >= maxRequests {
|
||||
break
|
||||
}
|
||||
|
||||
var selectedVerb string
|
||||
if patterns[0].Sequence > 0 {
|
||||
currentPattern := patterns[patternIndex]
|
||||
selectedVerb = currentPattern.Verb
|
||||
|
||||
sequenceCount++
|
||||
if sequenceCount >= currentPattern.Sequence {
|
||||
sequenceCount = 0
|
||||
patternIndex = (patternIndex + 1) % len(patterns)
|
||||
}
|
||||
} else {
|
||||
rand := rand.Float64() * 100
|
||||
cumulative := 0.0
|
||||
for _, p := range patterns {
|
||||
cumulative += p.Percentage
|
||||
if rand <= cumulative {
|
||||
selectedVerb = p.Verb
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wg.Add(1)
|
||||
go func(verb string) {
|
||||
defer wg.Done()
|
||||
err := makeRequest(verb, url, token, jsonData, int(time.Since(startTime).Seconds()))
|
||||
if err != nil {
|
||||
fmt.Printf("Error making request: %v\n", err)
|
||||
}
|
||||
atomic.AddInt32(&requestCount, 1)
|
||||
}(selectedVerb)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
CalculateAndPrintMetrics(startTime, requestsPerSecond, url, patterns)
|
||||
}
|
28
lib/types.go
Normal file
28
lib/types.go
Normal file
@ -0,0 +1,28 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PerformanceMetrics struct {
|
||||
Mu sync.Mutex
|
||||
TotalRequests int32
|
||||
TotalResponses int32
|
||||
TotalLatency time.Duration
|
||||
MaxLatency time.Duration
|
||||
MinLatency time.Duration
|
||||
ResponseCounters map[int]int32
|
||||
}
|
||||
|
||||
type RequestError struct {
|
||||
Verb string
|
||||
URL string
|
||||
Err error
|
||||
}
|
||||
|
||||
type RequestPattern struct {
|
||||
Verb string
|
||||
Percentage float64
|
||||
Sequence int
|
||||
}
|
Reference in New Issue
Block a user