Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3436c64d8d | |||
| 0a2d95652f | |||
| c888c47742 | |||
| 8409ffff47 | |||
| d2c50825fd | |||
| a650db3c74 |
@@ -1,6 +1,6 @@
|
||||
# Loadr
|
||||
|
||||
A lightweight REST load testing tool with rubust support for different verbs, token auth, and performance reports.
|
||||
A lightweight REST load testing tool with robust support for different verbs, token auth, and performance reports.
|
||||
|
||||
## Example using source
|
||||
|
||||
@@ -13,6 +13,7 @@ A lightweight REST load testing tool with rubust support for different verbs, to
|
||||
|
||||
## Flags
|
||||
|
||||
- `-v` or `-version: Prints the version of Loadr.
|
||||
- `-rate`: Number of requests per second. Default is 10.
|
||||
- `-max`: Maximum number of requests to send. Must be a non-zero integer. Default is 50.
|
||||
- `-url`: The URL to make requests to. Default is "https://example.com".
|
||||
@@ -22,4 +23,4 @@ A lightweight REST load testing tool with rubust support for different verbs, to
|
||||
|
||||
## Reports
|
||||
|
||||
Reports are logged at the end of a test run. They are also saved in a directory called `.reports`. All reports are saved as text files with `YYYYMMdd-HHmmss` time format names.
|
||||
Reports are logged at the end of a test run. They are also saved in a directory called `.reports`. All reports are saved as text files with names corresponding to the unix timestamp of when they were completed.
|
||||
|
||||
86
lib/metrics.go
Normal file
86
lib/metrics.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Initialize the metrics with default values.
|
||||
var metrics = PerformanceMetrics{
|
||||
MinLatency: time.Duration(math.MaxInt64),
|
||||
ResponseCounters: make(map[int]int32),
|
||||
}
|
||||
|
||||
// updateMetrics updates the performance metrics.
|
||||
func UpdateMetrics(duration time.Duration, resp *http.Response, second int) {
|
||||
metrics.Mu.Lock()
|
||||
defer metrics.Mu.Unlock()
|
||||
|
||||
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]++
|
||||
}
|
||||
}
|
||||
|
||||
// calculateAndPrintMetrics calculates and prints the performance metrics.
|
||||
func CalculateAndPrintMetrics(startTime time.Time, requestsPerSecond float64, endpoint string, verb string) {
|
||||
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
|
||||
}
|
||||
|
||||
// Format the results
|
||||
results := fmt.Sprintf("Endpoint: %s\n", endpoint)
|
||||
results += fmt.Sprintf("HTTP Verb: %s\n", verb)
|
||||
results += fmt.Sprintln("--------------------")
|
||||
results += fmt.Sprintln("Performance Metrics:")
|
||||
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 Per Second (Sent): %.2f\n", float64(requestsPerSecond))
|
||||
results += fmt.Sprintf("Responses Per Second (Received): %.2f\n", float64(totalResponses)/totalDuration)
|
||||
|
||||
// Print the results to the console
|
||||
fmt.Println(results)
|
||||
|
||||
// Save the results to a file
|
||||
resultsDir := ".reports"
|
||||
os.MkdirAll(resultsDir, os.ModePerm) // Ensure the directory exists
|
||||
|
||||
// Use the current epoch timestamp as the filename
|
||||
resultsFile := filepath.Join(resultsDir, fmt.Sprintf("%d.txt", time.Now().Unix()))
|
||||
f, err := os.Create(resultsFile)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating file: ", err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
_, err = f.WriteString(results)
|
||||
if err != nil {
|
||||
fmt.Println("Error writing to file: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("Results saved to: ", resultsFile)
|
||||
}
|
||||
73
lib/metrics_test.go
Normal file
73
lib/metrics_test.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package lib_test
|
||||
|
||||
import (
|
||||
"loadr/lib"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func comparePerformanceMetrics(a, b *lib.PerformanceMetrics) bool {
|
||||
return a.TotalRequests == b.TotalRequests &&
|
||||
a.TotalResponses == b.TotalResponses &&
|
||||
a.TotalLatency == b.TotalLatency &&
|
||||
a.MaxLatency == b.MaxLatency &&
|
||||
a.MinLatency == b.MinLatency &&
|
||||
reflect.DeepEqual(a.ResponseCounters, b.ResponseCounters)
|
||||
}
|
||||
|
||||
func TestCalculateAndPrintMetrics(t *testing.T) {
|
||||
// Define test cases.
|
||||
tests := []struct {
|
||||
name string
|
||||
startTime time.Time
|
||||
requestsPerSecond float64
|
||||
endpoint string
|
||||
verb string
|
||||
expectedMetrics lib.PerformanceMetrics
|
||||
}{
|
||||
{
|
||||
name: "Test 1",
|
||||
startTime: time.Now().Add(-1 * time.Second), // 1 second ago
|
||||
requestsPerSecond: 1.0,
|
||||
endpoint: "http://localhost",
|
||||
verb: "GET",
|
||||
expectedMetrics: lib.PerformanceMetrics{
|
||||
TotalRequests: 1,
|
||||
TotalResponses: 1,
|
||||
TotalLatency: 1 * time.Second,
|
||||
MaxLatency: 1 * time.Second,
|
||||
MinLatency: 1 * time.Second,
|
||||
ResponseCounters: map[int]int32{1: 1},
|
||||
},
|
||||
},
|
||||
// Add more test cases as needed.
|
||||
}
|
||||
|
||||
for i := range tests {
|
||||
tt := &tests[i]
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Reset the metrics before each test
|
||||
metrics := lib.PerformanceMetrics{}
|
||||
|
||||
// Mock the system behavior
|
||||
metrics.TotalRequests++
|
||||
metrics.ResponseCounters = make(map[int]int32)
|
||||
metrics.TotalResponses++
|
||||
metrics.TotalLatency += 1 * time.Second
|
||||
metrics.MaxLatency = 1 * time.Second
|
||||
metrics.MinLatency = 1 * time.Second
|
||||
metrics.ResponseCounters[1]++
|
||||
|
||||
// Call the function
|
||||
lib.CalculateAndPrintMetrics(tt.startTime, tt.requestsPerSecond, tt.endpoint, tt.verb)
|
||||
|
||||
// Check if the metrics are correct
|
||||
if !comparePerformanceMetrics(&metrics, &tt.expectedMetrics) {
|
||||
t.Errorf("CalculateAndPrintMetrics() = TotalRequests: %v, TotalResponses: %v, TotalLatency: %v, MaxLatency: %v, MinLatency: %v, ResponseCounters: %v, want TotalRequests: %v, TotalResponses: %v, TotalLatency: %v, MaxLatency: %v, MinLatency: %v, ResponseCounters: %v",
|
||||
metrics.TotalRequests, metrics.TotalResponses, metrics.TotalLatency, metrics.MaxLatency, metrics.MinLatency, metrics.ResponseCounters,
|
||||
tt.expectedMetrics.TotalRequests, tt.expectedMetrics.TotalResponses, tt.expectedMetrics.TotalLatency, tt.expectedMetrics.MaxLatency, tt.expectedMetrics.MinLatency, tt.expectedMetrics.ResponseCounters)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
99
lib/requests.go
Normal file
99
lib/requests.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Global HTTP client used for making requests.
|
||||
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)
|
||||
}
|
||||
|
||||
// makeRequest sends an HTTP request and updates performance metrics.
|
||||
func makeRequest(verb, url, token string, jsonData []byte, second int) error {
|
||||
startTime := time.Now()
|
||||
|
||||
// Create a new request with the provided verb, URL, and JSON data if provided.
|
||||
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}
|
||||
}
|
||||
|
||||
// Add the bearer token to the request's Authorization header if provided.
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
|
||||
// Send the request.
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return &RequestError{Verb: verb, URL: url, Err: err}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Calculate the duration of the request.
|
||||
duration := time.Since(startTime)
|
||||
|
||||
// Update the performance metrics in a separate goroutine.
|
||||
go UpdateMetrics(duration, resp, second)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendRequests sends requests at the specified rate.
|
||||
func SendRequests(url, bearerToken, requestType string, jsonData []byte, maxRequests int, requestsPerSecond float64) {
|
||||
// Calculate the rate limit based on the requests per second.
|
||||
rateLimit := time.Second / time.Duration(requestsPerSecond)
|
||||
ticker := time.NewTicker(rateLimit)
|
||||
defer ticker.Stop()
|
||||
|
||||
// Initialize the request count.
|
||||
var requestCount int32 = 0
|
||||
|
||||
// Wait for all goroutines to finish.
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// Log beginning of requests
|
||||
fmt.Println("Starting Loadr Requests...")
|
||||
|
||||
// Start sending requests at the specified rate.
|
||||
startTime := time.Now()
|
||||
for range ticker.C {
|
||||
second := int(time.Since(startTime).Seconds())
|
||||
if int(requestCount) >= maxRequests {
|
||||
break
|
||||
}
|
||||
wg.Add(1)
|
||||
go func(u, t, verb string, data []byte, sec int) {
|
||||
defer wg.Done()
|
||||
err := makeRequest(verb, u, t, data, sec)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
atomic.AddInt32(&requestCount, 1)
|
||||
}(url, bearerToken, strings.ToUpper(requestType), jsonData, second)
|
||||
}
|
||||
|
||||
wg.Wait() // Wait for all requests to finish.
|
||||
|
||||
CalculateAndPrintMetrics(startTime, requestsPerSecond, url, requestType)
|
||||
}
|
||||
50
lib/requests_test.go
Normal file
50
lib/requests_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package lib_test
|
||||
|
||||
import (
|
||||
"loadr/lib"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSendRequests(t *testing.T) {
|
||||
// Create a test server that responds with a 200 status.
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
url string
|
||||
bearerToken string
|
||||
requestType string
|
||||
jsonData []byte
|
||||
maxRequests int
|
||||
requestsPerSecond float64
|
||||
}{
|
||||
{
|
||||
name: "Test 1",
|
||||
url: ts.URL,
|
||||
bearerToken: "testToken",
|
||||
requestType: "GET",
|
||||
jsonData: []byte(`{"key":"value"}`),
|
||||
maxRequests: 5,
|
||||
requestsPerSecond: 1.0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
start := time.Now()
|
||||
lib.SendRequests(tt.url, tt.bearerToken, tt.requestType, tt.jsonData, tt.maxRequests, tt.requestsPerSecond)
|
||||
elapsed := time.Since(start)
|
||||
|
||||
// Check if the requests were sent within the expected time frame.
|
||||
if elapsed > time.Duration(float64(tt.maxRequests)*1.5)*time.Second {
|
||||
t.Errorf("SendRequests() took too long, got: %v, want: less than %v", elapsed, time.Duration(float64(tt.maxRequests)*1.5)*time.Second)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
23
lib/types.go
Normal file
23
lib/types.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package lib
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PerformanceMetrics holds the metrics for performance evaluation.
|
||||
type PerformanceMetrics struct {
|
||||
Mu sync.Mutex // Protects the metrics
|
||||
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
|
||||
}
|
||||
189
main.go
189
main.go
@@ -1,97 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"loadr/lib"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Global HTTP client used for making requests.
|
||||
var client = &http.Client{}
|
||||
var version string = "1.0.2"
|
||||
|
||||
// PerformanceMetrics holds the metrics for performance evaluation.
|
||||
type PerformanceMetrics struct {
|
||||
mu sync.Mutex // Protects the metrics
|
||||
func parseCommandLine() (float64, int, string, string, string, string) {
|
||||
requestsPerSecond := flag.Float64("rate", 10, "Number of requests per second")
|
||||
maxRequests := flag.Int("max", 50, "Maximum number of requests to send (0 for unlimited)")
|
||||
url := flag.String("url", "https://example.com", "The URL to make requests to")
|
||||
requestType := flag.String("type", "GET", "Type of HTTP request (GET, POST, PUT, DELETE, etc.)")
|
||||
jsonFilePath := flag.String("json", "", "Path to the JSON file with request data")
|
||||
bearerToken := flag.String("token", "", "Bearer token for authorization")
|
||||
versionFlag := flag.Bool("version", false, "Print the version and exit")
|
||||
versionFlagShort := flag.Bool("v", false, "Print the version and exit")
|
||||
|
||||
totalRequests int32
|
||||
totalResponses int32
|
||||
totalLatency time.Duration
|
||||
maxLatency time.Duration
|
||||
minLatency time.Duration
|
||||
responseCounters map[int]int32
|
||||
}
|
||||
// Parse the command-line flags.
|
||||
flag.Parse()
|
||||
|
||||
// Initialize the metrics with default values.
|
||||
var metrics = PerformanceMetrics{
|
||||
minLatency: time.Duration(math.MaxInt64),
|
||||
responseCounters: make(map[int]int32),
|
||||
}
|
||||
|
||||
// makeRequest sends an HTTP request and updates performance metrics.
|
||||
func makeRequest(verb, url, token string, jsonData []byte, second int) {
|
||||
startTime := time.Now()
|
||||
|
||||
// Create a new request with the provided verb, URL, and JSON data if provided.
|
||||
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 {
|
||||
fmt.Println("Error creating request:", err)
|
||||
return
|
||||
// If the version flag is present, print the version number and exit.
|
||||
if *versionFlag || *versionFlagShort {
|
||||
fmt.Println("Version:", version)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Add the bearer token to the request's Authorization header if provided.
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
|
||||
// Send the request.
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println("Error making request:", err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Calculate the duration of the request.
|
||||
duration := time.Since(startTime)
|
||||
|
||||
// Update the performance metrics.
|
||||
metrics.mu.Lock()
|
||||
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]++
|
||||
}
|
||||
metrics.mu.Unlock()
|
||||
|
||||
// Read the response body to determine its size (not shown in the output).
|
||||
_, err = io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading response body:", err)
|
||||
return
|
||||
}
|
||||
return *requestsPerSecond, *maxRequests, *url, *requestType, *jsonFilePath, *bearerToken
|
||||
}
|
||||
|
||||
// readJSONFile reads the contents of the JSON file at the given path and returns the bytes.
|
||||
@@ -103,103 +40,21 @@ func readJSONFile(filePath string) ([]byte, error) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Define command-line flags for configuring the load test.
|
||||
requestsPerSecond := flag.Float64("rate", 10, "Number of requests per second")
|
||||
maxRequests := flag.Int("max", 50, "Maximum number of requests to send (0 for unlimited)")
|
||||
url := flag.String("url", "https://example.com", "The URL to make requests to")
|
||||
requestType := flag.String("type", "GET", "Type of HTTP request (GET, POST, PUT, DELETE, etc.)")
|
||||
jsonFilePath := flag.String("json", "", "Path to the JSON file with request data")
|
||||
bearerToken := flag.String("token", "", "Bearer token for authorization")
|
||||
|
||||
// Parse the command-line flags.
|
||||
flag.Parse()
|
||||
requestsPerSecond, maxRequests, url, requestType, jsonFilePath, bearerToken := parseCommandLine()
|
||||
|
||||
// Ensure maxRequests is greater than 0.
|
||||
if *maxRequests <= 0 {
|
||||
if maxRequests <= 0 {
|
||||
fmt.Println("Error: max must be an integer greater than 0")
|
||||
return
|
||||
}
|
||||
|
||||
// Read the JSON file if the path is provided.
|
||||
jsonData, err := readJSONFile(*jsonFilePath)
|
||||
jsonData, err := readJSONFile(jsonFilePath)
|
||||
if err != nil {
|
||||
fmt.Println("Error reading JSON file:", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Calculate the rate limit based on the requests per second.
|
||||
rateLimit := time.Second / time.Duration(*requestsPerSecond)
|
||||
ticker := time.NewTicker(rateLimit)
|
||||
defer ticker.Stop()
|
||||
|
||||
// Initialize the request count.
|
||||
var requestCount int32 = 0
|
||||
|
||||
// Wait for all goroutines to finish.
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// Log beginning of requests
|
||||
fmt.Println("Starting Loadr Requests...")
|
||||
|
||||
// Start sending requests at the specified rate.
|
||||
startTime := time.Now()
|
||||
for range ticker.C {
|
||||
second := int(time.Since(startTime).Seconds())
|
||||
if int(requestCount) >= *maxRequests {
|
||||
break
|
||||
}
|
||||
wg.Add(1)
|
||||
go func(u, t, verb string, data []byte, sec int) {
|
||||
defer wg.Done()
|
||||
makeRequest(verb, u, t, data, sec)
|
||||
atomic.AddInt32(&requestCount, 1)
|
||||
}(*url, *bearerToken, strings.ToUpper(*requestType), jsonData, second)
|
||||
}
|
||||
|
||||
wg.Wait() // Wait for all requests to finish.
|
||||
|
||||
// Calculate and print performance metrics.
|
||||
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
|
||||
}
|
||||
|
||||
// Format the results
|
||||
results := fmt.Sprintln("Performance Metrics:")
|
||||
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 Per Second (Sent): %.2f\n", float64(*requestsPerSecond))
|
||||
results += fmt.Sprintf("Responses Per Second (Received): %.2f\n", float64(totalResponses)/totalDuration)
|
||||
|
||||
// Print the results to the console
|
||||
fmt.Print(results)
|
||||
|
||||
// Ensure the .reports directory exists
|
||||
reportsDir := ".reports"
|
||||
if _, err := os.Stat(reportsDir); os.IsNotExist(err) {
|
||||
err := os.Mkdir(reportsDir, 0755)
|
||||
if err != nil {
|
||||
fmt.Println("Error creating reports directory:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Save the results to a file in the .reports directory
|
||||
timestamp := time.Now().Format("20060102-150405") // YYYYMMdd-HHmmss format
|
||||
fileName := fmt.Sprintf("%s.txt", timestamp)
|
||||
filePath := filepath.Join(reportsDir, fileName)
|
||||
if err := os.WriteFile(filePath, []byte(results), 0644); err != nil {
|
||||
fmt.Println("Error writing results to file:", err)
|
||||
return
|
||||
}
|
||||
|
||||
lib.SendRequests(url, bearerToken, requestType, jsonData, maxRequests, requestsPerSecond)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user