-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalculator.go
283 lines (257 loc) · 7.29 KB
/
calculator.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package main
import (
"fmt"
"math"
"sort"
"strconv"
"net/http"
)
type MetricsType int
const (
Percentile99 MetricsType = iota
Percentile95
PercentileAvg
PercentileMax
PercentileMin
Rps
)
func (mt MetricsType) String() string {
if mt != Rps {
return "Latency"
} else {
return "Rps"
}
}
type Calculator interface {
// GetMetricsResult
// Get benchmark result
GetMetricsResult() Metrics
// CalculatePerTrial
// Calculate metrics of attack per trial
CalculatePerTrial(requests []int, method string, trialNum int, errData map[int]int)
// CalculateMethodErrors
// Calculate error count per HTTP Method
CalculateMethodErrors(srcData map[string]map[int]int, dstData map[string]map[int]int) map[string]map[int]int
// PercentileN
// Calculate percentile
PercentileN(size int, percentile int) int
IsOverThreshHold(metricsType MetricsType, threshold int, targetHttpMethod string) bool
}
type Metrics struct {
GetMetrics []MetricsDetail
PostMetrics []MetricsDetail
PutMetrics []MetricsDetail
PatchMetrics []MetricsDetail
DeleteMetrics []MetricsDetail
TimeRange []float64
}
// MetricsDetail Metrics per trial benchmark
// 1 trial has that below data
type MetricsDetail struct {
Percentile99 float64
Percentile95 float64
PercentileAvg float64
PercentileMax float64
PercentileMin float64
Rps float64
ErrorData map[int]int
}
// NewCalculator Constructor
func NewCalculator(trialNum int) Calculator {
return Metrics{
GetMetrics: make([]MetricsDetail, trialNum),
PostMetrics: make([]MetricsDetail, trialNum),
PutMetrics: make([]MetricsDetail, trialNum),
PatchMetrics: make([]MetricsDetail, trialNum),
DeleteMetrics: make([]MetricsDetail, trialNum),
TimeRange: make([]float64, trialNum),
}
}
func (m Metrics) CalculatePerTrial(requests []int, method string, trialNum int, errData map[int]int) {
index := trialNum - 1
samplingSize := len(requests)
if samplingSize == 0 && len(errData) > 0 {
detail := MetricsDetail{ErrorData: errData}
detail.outputOnlyErrorStats(method)
return
}
if samplingSize == 0 {
return
}
sort.Ints(requests)
ignore95Index := m.PercentileN(samplingSize, 95) - 1
percentile95 := requests[ignore95Index]
ignore99Index := m.PercentileN(samplingSize, 99) - 1
percentile99 := requests[ignore99Index]
var avgLatency, maxLatency, minLatency, currentRps, beforeRps int
for i, v := range requests {
avgLatency += v
beforeRps = currentRps
currentRps = v
if i == 0 {
minLatency = currentRps
maxLatency = currentRps
}
if currentRps > beforeRps {
maxLatency = currentRps
}
if currentRps < beforeRps {
minLatency = currentRps
}
}
detail := MetricsDetail{
Percentile99: float64(percentile99),
Percentile95: float64(percentile95),
PercentileAvg: float64(avgLatency / len(requests)),
PercentileMax: float64(maxLatency),
PercentileMin: float64(minLatency),
Rps: float64(len(requests)) / float64(durationSeconds),
ErrorData: errData,
}
detail.outputStats(method)
switch method {
case http.MethodGet:
m.GetMetrics[index] = detail
case http.MethodPost:
m.PostMetrics[index] = detail
case http.MethodPut:
m.PutMetrics[index] = detail
case http.MethodPatch:
m.PatchMetrics[index] = detail
case http.MethodDelete:
m.DeleteMetrics[index] = detail
}
m.TimeRange[index] = float64(trialNum * durationSeconds)
}
func (m Metrics) CalculateMethodErrors(srcData map[string]map[int]int, dstData map[string]map[int]int) map[string]map[int]int {
if srcData != nil {
for method1, errorData1 := range dstData {
for k1, v1 := range errorData1 {
for method2, errorData2 := range srcData {
if method1 == method2 {
for k2, v2 := range errorData2 {
if k1 == k2 {
dstData[method1][k1] = v1 + v2
}
}
}
}
}
}
}
return dstData
}
func (m Metrics) GetMetricsResult() Metrics {
return m
}
func (m Metrics) PercentileN(size int, percentile int) int {
n := (float64(percentile) / float64(100)) * float64(size)
return int(math.Round(n*1) / 1)
}
func (m Metrics) IsOverThreshHold(metricsType MetricsType, threshold int, targetHttpMethod string) bool {
switch targetHttpMethod {
case http.MethodGet:
metrics := getMetrics(metricsType, m.GetMetrics)
for _, metric := range metrics {
if metricsType != Rps && metric > float64(threshold) {
return true
}
if metricsType == Rps && metric < float64(threshold) {
return true
}
}
case http.MethodPost:
metrics := getMetrics(metricsType, m.PostMetrics)
for _, metric := range metrics {
if metricsType != Rps && metric > float64(threshold) {
return true
}
if metricsType == Rps && metric < float64(threshold) {
return true
}
}
case http.MethodPut:
metrics := getMetrics(metricsType, m.PutMetrics)
for _, metric := range metrics {
if metricsType != Rps && metric > float64(threshold) {
return true
}
if metricsType == Rps && metric < float64(threshold) {
return true
}
}
case http.MethodPatch:
metrics := getMetrics(metricsType, m.PatchMetrics)
for _, metric := range metrics {
if metricsType != Rps && metric > float64(threshold) {
return true
}
if metricsType == Rps && metric < float64(threshold) {
return true
}
}
case http.MethodDelete:
metrics := getMetrics(metricsType, m.DeleteMetrics)
for _, metric := range metrics {
if metricsType != Rps && metric > float64(threshold) {
return true
}
if metricsType == Rps && metric < float64(threshold) {
return true
}
}
}
return false
}
func getMetrics(metricsType MetricsType, details []MetricsDetail) []float64 {
var metrics []float64
switch metricsType {
case Percentile95:
for _, detail := range details {
metrics = append(metrics, detail.Percentile95)
}
case Percentile99:
for _, detail := range details {
metrics = append(metrics, detail.Percentile99)
}
case PercentileAvg:
for _, detail := range details {
metrics = append(metrics, detail.PercentileAvg)
}
case PercentileMax:
for _, detail := range details {
metrics = append(metrics, detail.PercentileMax)
}
case PercentileMin:
for _, detail := range details {
metrics = append(metrics, detail.PercentileMin)
}
case Rps:
for _, detail := range details {
metrics = append(metrics, detail.Rps)
}
}
return metrics
}
func (md MetricsDetail) outputStats(method string) {
fmt.Println(fmt.Sprintf("%s request stats information", method))
fmt.Println(fmt.Sprintf("Latency 99 percentile: %d milliseconds", int(md.Percentile99)))
fmt.Println(fmt.Sprintf("Latency 95 percentile: %d milliseconds", int(md.Percentile95)))
fmt.Println(fmt.Sprintf("Latency avg percentile: %d milliseconds", int(md.PercentileAvg)))
fmt.Println(fmt.Sprintf("Latency max percentile: %d milliseconds", int(md.PercentileMax)))
fmt.Println(fmt.Sprintf("Latency min percentile: %d milliseconds", int(md.PercentileMin)))
fmt.Println(fmt.Sprintf("Request per seconds: %d", int(md.Rps)))
if md.ErrorData != nil {
for k, v := range md.ErrorData {
fmt.Println(fmt.Sprintf("Error status code %d count: ", k) + strconv.Itoa(v))
}
}
fmt.Println()
}
func (md MetricsDetail) outputOnlyErrorStats(method string) {
fmt.Println(fmt.Sprintf("%s request stats information", method))
for k, v := range md.ErrorData {
fmt.Println(fmt.Sprintf("Error status code %d count: ", k) + strconv.Itoa(v))
}
fmt.Println()
}