-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
97 lines (81 loc) · 2.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"flag"
"fmt"
"net/http"
"sync"
"time"
)
func main() {
url := flag.String("url", "", "URL do serviço a ser testado.")
requests := flag.Int("requests", 0, "Número total de requests.")
concurrency := flag.Int("concurrency", 1, "Número de chamadas simultâneas.")
maxRetries := flag.Int("max-retries", 3, "Número máximo de tentativas para solicitações falhadas.")
flag.Parse()
if *url == "" || *requests <= 0 || *concurrency <= 0 || *maxRetries <= 0 {
fmt.Println("Por favor, forneça valores válidos para URL, requests, concurrency e max-retries.")
return
}
var mu sync.Mutex
var wg sync.WaitGroup
requestCounter := 0
successfulRequests := 0
totalRequests := 0
statusCodes := make(map[int]int)
startTime := time.Now()
doRequest := func() {
defer wg.Done()
var err error
var response *http.Response
// Realiza tentativas até atingir o limite
for retries := 0; retries < *maxRetries; retries++ {
response, err = http.Get(*url)
if err == nil && response.StatusCode == http.StatusOK {
break
}
time.Sleep(100 * time.Millisecond)
}
mu.Lock()
defer mu.Unlock()
if err != nil {
fmt.Println("Erro na request:", err)
return
}
defer response.Body.Close()
statusCodes[response.StatusCode]++
if response.StatusCode == http.StatusOK {
successfulRequests++
}
requestCounter++
totalRequests++
}
// Inicializa as goroutines
wg.Add(*requests)
for i := 0; i < *requests; i++ {
go doRequest()
}
// Aguarda a conclusão de todas as goroutines
wg.Wait()
// Verifica se é necessário realizar mais solicitações recursivamente
for successfulRequests < *requests {
wg.Add(*requests - successfulRequests)
for i := 0; i < (*requests - successfulRequests); i++ {
go doRequest()
}
wg.Wait()
}
// Exibindo o tempo total gasto nos testes
elapsedTime := time.Since(startTime)
// Exibindo o relatório principal
fmt.Printf("Relatório de Teste:\n")
fmt.Printf("Tempo total gasto: %v\n", elapsedTime)
fmt.Printf("Quantidade total de requests: %d\n", totalRequests)
fmt.Printf("Quantidade de requests com status HTTP 200: %d\n", successfulRequests)
fmt.Printf("Distribuição de códigos de status HTTP:\n")
for code, count := range statusCodes {
if code != http.StatusOK {
fmt.Printf("%d: %d\n", code, count)
continue
}
}
}