mirror of
https://github.com/nprasad2077/NBA_Go.git
synced 2026-09-22 14:05:13 +00:00
testing code update
This commit is contained in:
@@ -23,3 +23,9 @@ curl http://localhost:8080/api/playeradvancedstats \
|
|||||||
```bash
|
```bash
|
||||||
swag init -g main.go -o docs
|
swag init -g main.go -o docs
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Test
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go run loadtest.go -n 100 -c 10 -url "http://127.0.0.1:8080/api/playeradvancedstats?page=1&pageSize=20" -log results.log -key "xxx"
|
||||||
|
```
|
||||||
|
|||||||
+48
-29
@@ -1,14 +1,14 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
"log"
|
|
||||||
"flag"
|
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Response struct to track which server handled the request
|
// Response struct to track which server handled the request
|
||||||
@@ -25,6 +25,7 @@ func main() {
|
|||||||
concurrency := flag.Int("c", 10, "Number of concurrent requests")
|
concurrency := flag.Int("c", 10, "Number of concurrent requests")
|
||||||
endpoint := flag.String("url", "http://localhost:8080/api/player/advanced", "API endpoint to test")
|
endpoint := flag.String("url", "http://localhost:8080/api/player/advanced", "API endpoint to test")
|
||||||
logFile := flag.String("log", "loadtest.log", "Log file path")
|
logFile := flag.String("log", "loadtest.log", "Log file path")
|
||||||
|
apiKey := flag.String("key", "", "API key for x-api-key header")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Setup logging
|
// Setup logging
|
||||||
@@ -34,48 +35,64 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
log.SetOutput(f)
|
log.SetOutput(f)
|
||||||
|
|
||||||
fmt.Printf("Starting load test with %d requests, %d concurrent\n", *numRequests, *concurrency)
|
fmt.Printf("Starting load test with %d requests, %d concurrent\n", *numRequests, *concurrency)
|
||||||
fmt.Printf("Testing endpoint: %s\n", *endpoint)
|
fmt.Printf("Testing endpoint: %s\n", *endpoint)
|
||||||
|
|
||||||
// Channel to collect results
|
// Channel to collect results
|
||||||
results := make(chan Response, *numRequests)
|
results := make(chan Response, *numRequests)
|
||||||
|
|
||||||
// Use a WaitGroup to manage concurrency
|
// Use a WaitGroup to manage concurrency
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
// Semaphore to limit concurrency
|
// Semaphore to limit concurrency
|
||||||
sem := make(chan bool, *concurrency)
|
sem := make(chan bool, *concurrency)
|
||||||
|
|
||||||
// Start the timer
|
// Start the timer
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
|
|
||||||
// Launch goroutines for requests
|
// Launch goroutines for requests
|
||||||
for i := 0; i < *numRequests; i++ {
|
for i := 0; i < *numRequests; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(id int) {
|
go func(id int) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
|
||||||
// Acquire semaphore
|
// Acquire semaphore
|
||||||
sem <- true
|
sem <- true
|
||||||
defer func() { <-sem }()
|
defer func() { <-sem }()
|
||||||
|
|
||||||
// Make the request
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
resp, err := http.Get(*endpoint)
|
|
||||||
duration := time.Since(start)
|
|
||||||
|
|
||||||
result := Response{
|
result := Response{
|
||||||
Duration: duration,
|
Duration: 0,
|
||||||
Error: err,
|
Error: nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", *endpoint, nil)
|
||||||
|
if err != nil {
|
||||||
|
result.Duration = time.Since(start)
|
||||||
|
result.Error = err
|
||||||
|
log.Printf("Request %d failed to create request: %v", id, err)
|
||||||
|
results <- result
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if *apiKey != "" {
|
||||||
|
req.Header.Add("x-api-key", *apiKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
duration := time.Since(start)
|
||||||
|
|
||||||
|
result.Duration = duration
|
||||||
|
result.Error = err
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Request %d failed: %v", id, err)
|
log.Printf("Request %d failed: %v", id, err)
|
||||||
results <- result
|
results <- result
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -84,27 +101,26 @@ func main() {
|
|||||||
results <- result
|
results <- result
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result.StatusCode = resp.StatusCode
|
result.StatusCode = resp.StatusCode
|
||||||
result.Body = string(body)
|
result.Body = string(body)
|
||||||
results <- result
|
results <- result
|
||||||
|
|
||||||
// Log request details
|
// Log request details
|
||||||
log.Printf("Request %d: Status=%d, Time=%v",
|
log.Printf("Request %d: Status=%d, Time=%v", id, resp.StatusCode, duration)
|
||||||
id, resp.StatusCode, duration)
|
|
||||||
}(i)
|
}(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close the results channel when all requests are done
|
// Close the results channel when all requests are done
|
||||||
go func() {
|
go func() {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
close(results)
|
close(results)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Process results
|
// Process results
|
||||||
var successCount, errorCount int
|
var successCount, errorCount int
|
||||||
var totalDuration time.Duration
|
var totalDuration time.Duration
|
||||||
|
|
||||||
for result := range results {
|
for result := range results {
|
||||||
if result.Error != nil {
|
if result.Error != nil {
|
||||||
errorCount++
|
errorCount++
|
||||||
@@ -115,12 +131,15 @@ func main() {
|
|||||||
errorCount++
|
errorCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate statistics
|
// Calculate statistics
|
||||||
totalTime := time.Since(startTime)
|
totalTime := time.Since(startTime)
|
||||||
avgDuration := totalDuration / time.Duration(successCount)
|
var avgDuration time.Duration
|
||||||
|
if successCount > 0 {
|
||||||
|
avgDuration = totalDuration / time.Duration(successCount)
|
||||||
|
}
|
||||||
requestsPerSecond := float64(*numRequests) / totalTime.Seconds()
|
requestsPerSecond := float64(*numRequests) / totalTime.Seconds()
|
||||||
|
|
||||||
// Print summary
|
// Print summary
|
||||||
fmt.Printf("\nLoad Test Summary:\n")
|
fmt.Printf("\nLoad Test Summary:\n")
|
||||||
fmt.Printf("Total Requests: %d\n", *numRequests)
|
fmt.Printf("Total Requests: %d\n", *numRequests)
|
||||||
|
|||||||
+1000
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user