-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (96 loc) · 2.38 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"strconv"
"time"
)
func main() {
requestsCount, _ := strconv.Atoi(os.Args[1])
maxConcurrency, _ := strconv.Atoi(os.Args[2])
url := os.Args[3]
client := CreateClient()
request, _ := CreateRequest(url)
concurrencyMonitor := make(chan string, maxConcurrency)
concurrent := 0
requestsCountOk := 0
requestsCountFailed := 0
requestsLastError := ""
lastMonitoringMessageTime := time.Now().Add(-3 * time.Second)
for {
if time.Now().Sub(lastMonitoringMessageTime) > 2*time.Second {
fmt.Printf("remaining requests: %v, concurrent requests: %v, ok: %v, failed: %v, lastError: %v\n", requestsCount, concurrent, requestsCountOk, requestsCountFailed, requestsLastError)
lastMonitoringMessageTime = time.Now()
requestsCountOk = 0
requestsCountFailed = 0
requestsLastError = ""
}
if concurrent < maxConcurrency && requestsCount > 0 {
concurrent++
requestsCount--
go DoRequest(client, request, concurrencyMonitor)
} else {
time.Sleep(10 * time.Microsecond)
}
select {
case status := <-concurrencyMonitor:
concurrent--
if status == "OK" {
requestsCountOk++
} else {
requestsCountFailed++
requestsLastError = status
}
default:
}
if requestsCount <= 0 && concurrent <= 0 {
break
}
}
}
func CreateClient() *http.Client {
transport := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
DialContext: (&net.Dialer{
Timeout: 60 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 120 * time.Second,
ResponseHeaderTimeout: 180 * time.Second,
MaxIdleConns: 0,
MaxIdleConnsPerHost: 0,
DisableKeepAlives: true,
}
client := &http.Client{
Transport: transport,
Timeout: 200 * time.Second,
}
return client
}
func CreateRequest(url string) (*http.Request, error) {
request, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("while creating a new request: %w", err)
}
request.Header.Add("Accept", "application/json")
return request, nil
}
func DoRequest(client *http.Client, request *http.Request, monitor chan string) {
response, err := client.Do(request)
if err != nil {
monitor <- err.Error()
return
}
_, err = ioutil.ReadAll(response.Body)
if err != nil {
monitor <- err.Error()
return
}
_ = response.Body.Close()
monitor <- "OK"
}