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
|
||||
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"
|
||||
```
|
||||
|
||||
+31
-12
@@ -1,14 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
"log"
|
||||
"flag"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Response struct to track which server handled the request
|
||||
@@ -25,6 +25,7 @@ func main() {
|
||||
concurrency := flag.Int("c", 10, "Number of concurrent requests")
|
||||
endpoint := flag.String("url", "http://localhost:8080/api/player/advanced", "API endpoint to test")
|
||||
logFile := flag.String("log", "loadtest.log", "Log file path")
|
||||
apiKey := flag.String("key", "", "API key for x-api-key header")
|
||||
flag.Parse()
|
||||
|
||||
// Setup logging
|
||||
@@ -60,15 +61,31 @@ func main() {
|
||||
sem <- true
|
||||
defer func() { <-sem }()
|
||||
|
||||
// Make the request
|
||||
start := time.Now()
|
||||
resp, err := http.Get(*endpoint)
|
||||
result := Response{
|
||||
Duration: 0,
|
||||
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 := Response{
|
||||
Duration: duration,
|
||||
Error: err,
|
||||
}
|
||||
result.Duration = duration
|
||||
result.Error = err
|
||||
|
||||
if err != nil {
|
||||
log.Printf("Request %d failed: %v", id, err)
|
||||
@@ -90,8 +107,7 @@ func main() {
|
||||
results <- result
|
||||
|
||||
// Log request details
|
||||
log.Printf("Request %d: Status=%d, Time=%v",
|
||||
id, resp.StatusCode, duration)
|
||||
log.Printf("Request %d: Status=%d, Time=%v", id, resp.StatusCode, duration)
|
||||
}(i)
|
||||
}
|
||||
|
||||
@@ -118,7 +134,10 @@ func main() {
|
||||
|
||||
// Calculate statistics
|
||||
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()
|
||||
|
||||
// Print summary
|
||||
|
||||
+1000
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user