initial load testing script
This commit is contained in:
98
main.go
Normal file
98
main.go
Normal file
@ -0,0 +1,98 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RequestData struct {
|
||||
RequestNumber int
|
||||
StartTime int64
|
||||
Duration int64
|
||||
IsError bool
|
||||
}
|
||||
|
||||
func makeRequest(url string, requestNumber int, wg *sync.WaitGroup, results chan<- RequestData) {
|
||||
defer wg.Done()
|
||||
|
||||
startTime := time.Now().UnixNano()
|
||||
httpClient := http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
Transport: &http.Transport{
|
||||
TLSHandshakeTimeout: 30 * time.Second,
|
||||
},
|
||||
}
|
||||
|
||||
resp, err := httpClient.Get(url)
|
||||
//resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
fmt.Printf("Error making request %d: %v\n", requestNumber, err)
|
||||
results <- RequestData{
|
||||
RequestNumber: requestNumber,
|
||||
StartTime: startTime,
|
||||
Duration: time.Now().UnixNano() - startTime,
|
||||
IsError: true,
|
||||
}
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
duration := time.Now().UnixNano() - startTime
|
||||
results <- RequestData{
|
||||
RequestNumber: requestNumber,
|
||||
StartTime: startTime,
|
||||
Duration: duration,
|
||||
IsError: false,
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Define your API URL and number of requests to make
|
||||
apiURL := "https://git.sa.vin"
|
||||
numRequests := 1000
|
||||
//numRequests := 596
|
||||
|
||||
// Create a channel to collect the request results
|
||||
results := make(chan RequestData, numRequests)
|
||||
|
||||
// Create a WaitGroup to synchronize goroutines
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(numRequests)
|
||||
|
||||
// Start load testing by spawning goroutines
|
||||
for i := 0; i < numRequests; i++ {
|
||||
go makeRequest(apiURL, i, &wg, results)
|
||||
}
|
||||
|
||||
// Wait for all goroutines to finish
|
||||
wg.Wait()
|
||||
|
||||
// Close the results channel
|
||||
close(results)
|
||||
|
||||
// Collect the results from the channel
|
||||
var requestData []RequestData
|
||||
for result := range results {
|
||||
requestData = append(requestData, result)
|
||||
}
|
||||
|
||||
// Marshal the data to JSON
|
||||
jsonData, err := json.Marshal(requestData)
|
||||
if err != nil {
|
||||
fmt.Printf("Error marshaling JSON: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Write the JSON data to disk
|
||||
err = os.WriteFile("load_test_results.json", jsonData, 0644)
|
||||
if err != nil {
|
||||
fmt.Printf("Error writing JSON file: %v\n", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Load testing completed. Results stored in load_test_results.json")
|
||||
}
|
Reference in New Issue
Block a user