-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_client.go
125 lines (119 loc) · 3.99 KB
/
api_client.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
117
118
119
120
121
122
123
124
125
package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"sync/atomic"
"time"
)
func (conf *APIConfig) request() {
status := &APIStatus{
TotalDuration: time.Millisecond,
MinRequestTime: time.Hour,
MaxRequestTime: time.Millisecond,
StatusCodes: &StatusCodes{},
}
client, err := newHTTPClient(conf.timeOut)
if err != nil {
log.Fatal(err)
}
start := time.Now()
finalConf, err := prepareRequest(conf)
if err != nil {
log.Fatal(err)
}
for time.Since(start).Seconds() <= float64(finalConf.duration) && atomic.LoadInt32(&finalConf.interrupt) == 0 {
for _, val := range finalConf.params {
reqDuration, respSize := run(client, val.request, status)
if respSize > 0 {
status.NumberOfRequests++
status.TotalResponseSize += int64(respSize)
status.TotalDuration += reqDuration
status.MaxRequestTime = findMaxRequestTime(reqDuration, status.MaxRequestTime)
status.MinRequestTime = findMinRequestTime(reqDuration, status.MinRequestTime)
} else {
status.ErrorCount++
}
}
}
finalConf.finalStatus <- status
}
func run(httpClient *http.Client, req *http.Request, s *APIStatus) (requestDuration time.Duration, responseSize int) {
requestDuration = -1
responseSize = -1
start := time.Now()
resp, err := httpClient.Do(req)
if err != nil {
fmt.Println("[Info] An error occurred while creating the request", err)
}
if resp == nil {
fmt.Println("[Info] empty response")
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("[Info] An error occurred while reading response body", err)
}
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusCreated {
requestDuration = time.Since(start)
responseSize = len(body) + int(headerSize(resp.Header))
s.StatusCodes.TwoXX++
} else if resp.StatusCode == http.StatusContinue || resp.StatusCode == http.StatusSwitchingProtocols ||
resp.StatusCode == http.StatusProcessing {
s.StatusCodes.OneXX++
} else if resp.StatusCode == http.StatusMultipleChoices || resp.StatusCode == http.StatusMovedPermanently ||
resp.StatusCode == http.StatusFound || resp.StatusCode == http.StatusSeeOther ||
resp.StatusCode == http.StatusNotModified {
s.StatusCodes.ThreeXX++
} else if resp.StatusCode == http.StatusBadRequest || resp.StatusCode == http.StatusUnauthorized ||
resp.StatusCode == http.StatusPaymentRequired || resp.StatusCode == http.StatusForbidden ||
resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusMethodNotAllowed ||
resp.StatusCode == http.StatusNotAcceptable || resp.StatusCode == http.StatusProxyAuthRequired ||
resp.StatusCode == http.StatusRequestTimeout || resp.StatusCode == http.StatusContinue {
s.StatusCodes.FourXX++
} else if resp.StatusCode == http.StatusInternalServerError || resp.StatusCode == http.StatusNotImplemented ||
resp.StatusCode == http.StatusBadGateway || resp.StatusCode == http.StatusServiceUnavailable ||
resp.StatusCode == http.StatusGatewayTimeout || resp.StatusCode == http.StatusHTTPVersionNotSupported {
s.StatusCodes.FiveXX++
} else {
s.StatusCodes.Others++
}
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
return
}
func newHTTPClient(timeOut int) (*http.Client, error) {
client := &http.Client{}
client.Transport = &http.Transport{
ResponseHeaderTimeout: time.Millisecond * time.Duration(timeOut),
}
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return errors.New("url redirection not allowed")
}
return client, nil
}
func prepareRequest(conf *APIConfig) (*APIConfig, error) {
for _, val := range conf.params {
var buffer io.Reader
if len(val.jsonBody) > 0 {
buffer = bytes.NewBufferString(val.jsonBody)
}
req, err := http.NewRequest(val.Method, val.URL, buffer)
if err != nil {
fmt.Println("[Info] An error occurred while creating a new http request", err)
return nil, err
}
for headerKey, headerValue := range val.Header {
req.Header.Add(headerKey, headerValue)
}
val.request = req
}
return conf, nil
}