-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.go
More file actions
150 lines (135 loc) · 3.93 KB
/
client.go
File metadata and controls
150 lines (135 loc) · 3.93 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"fmt"
"io"
"io/ioutil"
"strings"
"time"
"math/rand"
"net/http"
)
var durationSeconds = 5
var warmupSeconds = 5
type BenchmarkClient interface {
Attack(attackNum int) Result
Warmup()
GetRandomHttpRequests() []*http.Request
}
type HttpClient struct {
HttpClient *http.Client
RandomHttpRequests []*http.Request
RequestDuration time.Duration
}
type Result struct {
Get []int
Post []int
Put []int
Patch []int
Delete []int
ErrData map[string]map[int]int
}
// NewBenchmarkClient New BenchmarkClient
func NewBenchmarkClient(url string, methods []string, headers map[string]string, body io.Reader, percentages map[string]int) BenchmarkClient {
var requests []*http.Request
for _, method := range methods {
var request *http.Request
for targetMethod, percentage := range percentages {
if strings.EqualFold(method, targetMethod) {
// GenerateCharts request per percentage method
for i := 0; i < percentage; i++ {
if !strings.EqualFold(method, http.MethodGet) {
request, _ = http.NewRequest(method, url, body)
} else {
request, _ = http.NewRequest(method, url, nil)
}
// Set headers
for headerKey, headerValue := range headers {
request.Header.Set(headerKey, headerValue)
}
requests = append(requests, request)
}
}
}
}
shuffleRequest(requests)
fmt.Print("HTTP request pattern according to the ratio = ")
for _, r := range requests {
fmt.Printf("%s ", r.Method)
}
fmt.Println()
client := new(http.Client)
return HttpClient{
HttpClient: client,
RandomHttpRequests: requests,
RequestDuration: time.Duration(durationSeconds) * time.Second,
}
}
func (h HttpClient) Attack(attackNum int) Result {
var getLatency, postLatency, putLatency, patchLatency, deleteLatency []int
errData := make(map[string]map[int]int)
fmt.Printf("(Thread-%d): Start attack for duration %d seconds\n", attackNum, durationSeconds)
for begin := time.Now(); time.Since(begin) < h.RequestDuration; {
// Random Http Method request
for _, request := range h.RandomHttpRequests {
start := makeTimestamp()
res, err := h.HttpClient.Do(request)
if err != nil {
} else if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated {
end := makeTimestamp()
latency := end - start
switch request.Method {
case http.MethodGet:
getLatency = append(getLatency, int(latency))
case http.MethodPost:
postLatency = append(postLatency, int(latency))
case http.MethodPut:
putLatency = append(putLatency, int(latency))
case http.MethodPatch:
patchLatency = append(patchLatency, int(latency))
case http.MethodDelete:
deleteLatency = append(deleteLatency, int(latency))
}
} else {
if _, ok := errData[request.Method]; ok {
errData[request.Method][res.StatusCode] = errData[request.Method][res.StatusCode] + 1
} else {
errData[request.Method] = map[int]int{res.StatusCode: 1}
}
}
if res != nil {
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
}
}
}
fmt.Printf("(Thread-%d): End attack \n", attackNum)
return Result{
Get: getLatency,
Post: postLatency,
Put: putLatency,
Patch: patchLatency,
Delete: deleteLatency,
ErrData: errData,
}
}
func (h HttpClient) Warmup() {
fmt.Printf("Start warnmup for duration %d seconds\n", warmupSeconds)
for begin := time.Now(); time.Since(begin) < h.RequestDuration; {
// Random Http Method request
for _, request := range h.RandomHttpRequests {
h.HttpClient.Do(request)
}
}
fmt.Println("End warmup")
fmt.Println()
}
func (h HttpClient) GetRandomHttpRequests() []*http.Request {
return h.RandomHttpRequests
}
func shuffleRequest(requests []*http.Request) {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(requests), func(i, j int) { requests[i], requests[j] = requests[j], requests[i] })
}
func makeTimestamp() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}