-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
174 lines (148 loc) · 5.23 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"os"
"sync"
"time"
"net/http"
)
const (
// =========================================================================
// ============================== HTTP Request =============================
// =========================================================================
// EnvTargetUrl Required
// ex: https://example.com
EnvTargetUrl = "INPUT_TARGET_URL"
// EnvHttpHeaders Required
// HashMap data structure
// ex: {"Authorization": "Bearer ", "Content-Type": "application/json"}
EnvHttpHeaders = "INPUT_HTTP_HEADERS"
// EnvThreadNum Required
// Maximum is 3.
// ex: 2
EnvThreadNum = "INPUT_THREAD_NUM"
// EnvTrialNum Required
// Maximum is 20. Takes up to 5 minutes
// ex: 20
EnvTrialNum = "INPUT_TRIAL_NUM"
// EnvReqHttpMethodRatio Optional
// HashMap data structure
// If only one http method, always 100 percent set method
// ex: {"POST": 4, "GET": 6}
EnvReqHttpMethodRatio = "INPUT_REQ_HTTP_METHOD_RATIO"
// EnvHttpRequestBody Optional
// If not empty, always use body when not GET method
// ex: {"email": "test@gmail.com", "password": "A_test12345-"}
EnvHttpRequestBody = "INPUT_HTTP_REQ_BODY"
// =========================================================================
// ============================== Report data ==============================
// =========================================================================
// EnvPermanent Optional
// Using GitHub pages
// ex: true || false
EnvPermanent = "INPUT_PERMANENT"
// =========================================================================
// ============================== Slack Alert ==============================
// =========================================================================
// EnvEnableAlert Optional
// ex: true | false
EnvEnableAlert = "INPUT_ENABLE_ALERT"
// EnvSlackWebHookUrl Optional
// ex: https://slack.com
EnvSlackWebHookUrl = "INPUT_SLACK_WEB_HOOK_URL"
// EnvSlackChannel Optional
// ex: #test
EnvSlackChannel = "INPUT_SLACK_CHANNEL"
// EnvSlackNotifyThreshHoldLatencyMillis Optional
// If set this one, notify slack when latency is not achieve threshold
// ex: 200
EnvSlackNotifyThreshHoldLatencyMillis = "INPUT_SLACK_NOTIFY_THRESHOLD_LATENCY_MILLIS"
// EnvSlackNotifyThreshHoldRps Optional
// If set this one, notify slack when The request per seconds is not achieve threshold
// ex: 20
EnvSlackNotifyThreshHoldRps = "INPUT_SLACK_NOTIFY_THRESHOLD_RPS"
)
var (
allowedHttpMethods = []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodPatch, http.MethodDelete}
)
func main() {
errs := ValidateEnv()
if errs != nil {
for _, v := range errs {
fmt.Println(v)
}
os.Exit(1)
return
}
runtime := NewRuntimeInfo()
client := NewBenchmarkClient(
runtime.TargetUrl,
runtime.HttpMethods,
runtime.HttpHeaders,
runtime.HttpRequestBody,
runtime.HttpRequestMethodRatio,
)
calculator := NewCalculator(runtime.TrialNum)
// Worm up
client.Warmup()
// Benchmarking
startTime := time.Now().UTC()
fmt.Println(fmt.Sprintf("Start benchmarking. time = %d\n", startTime.Unix()))
for i := 1; i <= runtime.TrialNum; i++ {
var wg sync.WaitGroup
var result Result
var mutex = &sync.Mutex{}
for index := 1; index <= runtime.ThreadNum; index++ {
wg.Add(1)
go func(index int) {
data := client.Attack(index)
mutex.Lock()
result.Get = append(result.Get, data.Get...)
result.Post = append(result.Post, data.Post...)
result.Put = append(result.Put, data.Put...)
result.Patch = append(result.Patch, data.Patch...)
result.Delete = append(result.Delete, data.Delete...)
result.ErrData = calculator.CalculateMethodErrors(result.ErrData, data.ErrData)
mutex.Unlock()
wg.Done()
}(index)
}
wg.Wait()
// Calculate metrics per trial
fmt.Println()
calculator.CalculatePerTrial(result.Get, http.MethodGet, i, result.ErrData[http.MethodGet])
calculator.CalculatePerTrial(result.Post, http.MethodPost, i, result.ErrData[http.MethodPost])
calculator.CalculatePerTrial(result.Put, http.MethodPut, i, result.ErrData[http.MethodPut])
calculator.CalculatePerTrial(result.Patch, http.MethodPatch, i, result.ErrData[http.MethodPatch])
calculator.CalculatePerTrial(result.Delete, http.MethodDelete, i, result.ErrData[http.MethodDelete])
time.Sleep(1 * time.Second)
}
endTime := time.Now().UTC()
fmt.Println(fmt.Sprintf("End benchmarking. time = %d", endTime.Unix()))
// Graph
metrics := calculator.GetMetricsResult()
graph := NewGraph(metrics)
charts := graph.GenerateCharts(metrics.TimeRange)
graph.Output(charts, startTime, endTime)
// Alert
if runtime.EnableAlert {
fmt.Println("\nCheck alert threshold")
if runtime.SlackNotifyThreshHoldLatencyMillis != 0 {
for _, method := range allowedHttpMethods {
// TODO: Choose percentile type
if calculator.IsOverThreshHold(PercentileAvg, runtime.SlackNotifyThreshHoldLatencyMillis, method) {
NewSlack(runtime.SlackWebHookUrl, runtime.SlackChannel).NotifyAlert(PercentileAvg)
return
}
}
}
if runtime.SlackNotifyThreshHoldRps != 0 {
for _, method := range allowedHttpMethods {
if calculator.IsOverThreshHold(Rps, runtime.SlackNotifyThreshHoldRps, method) {
NewSlack(runtime.SlackWebHookUrl, runtime.SlackChannel).NotifyAlert(PercentileAvg)
return
}
}
}
}
}