-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.go
168 lines (148 loc) · 4.73 KB
/
metrics.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
/*
* Copyright [2020] Sergey Kudasov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package loadgen
import (
"strconv"
"time"
"github.com/streadway/quantile"
)
// this file is a modified version from https://github.com/tsenart/vegeta/blob/master/lib/metrics.go
type (
// Metrics holds Metrics computed out of a slice of Results which are used
// in some of the Reporters
Metrics struct {
// Latencies holds computed request latency Metrics.
Latencies LatencyMetrics `json:"latencies"`
// First is the earliest timestamp in a Result set.
Earliest time.Time `json:"earliest"`
// Latest is the latest timestamp in a Result set.
Latest time.Time `json:"latest"`
// End is the latest timestamp in a Result set plus its latency.
End time.Time `json:"end"`
// Duration is the duration of the attack.
Duration time.Duration `json:"duration"`
// Wait is the extra time waiting for responses from targets.
Wait time.Duration `json:"wait"`
// Requests is the total number of requests executed.
Requests uint64 `json:"requests"`
// Rate is the rate of requests per second.
Rate float64 `json:"rate"`
// Success is the percentage of non-error responses.
Success float64 `json:"success"`
// StatusCodes is a histogram of the responses' status codes.
StatusCodes map[string]int `json:"status_codes"`
// Errors is a set of unique Errors returned by the targets during the attack.
Errors []string `json:"Errors"`
errors map[string]struct{}
errorsCount int64
successRatio float64
success int64
latencies *quantile.Estimator
}
// LatencyMetrics holds computed request latency Metrics.
LatencyMetrics struct {
// Total is the total latency sum of all requests in an attack.
Total time.Duration `json:"total"`
// Mean is the mean request latency.
Mean time.Duration `json:"mean"`
// P50 is the 50th percentile request latency.
P50 time.Duration `json:"50th"`
// P95 is the 95th percentile request latency.
P95 time.Duration `json:"95th"`
// P99 is the 99th percentile request latency.
P99 time.Duration `json:"99th"`
// Max is the maximum observed request latency.
Max time.Duration `json:"max"`
}
)
func (m Metrics) successLogEntry() int {
s := int(m.Success * 100.0)
if s < 0 {
return 0
}
return s
}
func (m Metrics) meanLogEntry() time.Duration {
lm := m.Latencies.Mean
if lm < 0 {
return time.Duration(0)
}
return lm
}
func (m *Metrics) updateSuccessRatio() {
if m.errorsCount != 0 {
m.successRatio = float64(m.errorsCount) / float64(m.Requests) * 100
}
}
func (m *Metrics) add(r result) {
m.init()
m.Requests++
// StatusCode is optional
if r.doResult.StatusCode > 0 {
m.StatusCodes[strconv.Itoa(r.doResult.StatusCode)]++
}
m.Latencies.Total += r.elapsed
m.latencies.Add(float64(r.elapsed))
if m.Earliest.IsZero() || m.Earliest.After(r.begin) {
m.Earliest = r.begin
}
if r.begin.After(m.Latest) {
m.Latest = r.begin
}
if end := r.end; end.After(m.End) {
m.End = end
}
if r.elapsed > m.Latencies.Max {
m.Latencies.Max = r.elapsed
}
if r.doResult.Error != nil {
if _, ok := m.errors[r.doResult.Error.Error()]; !ok {
m.errors[r.doResult.Error.Error()] = struct{}{}
m.Errors = append(m.Errors, r.doResult.Error.Error())
}
m.errorsCount++
} else {
if r.doResult.StatusCode == 0 || (r.doResult.StatusCode >= 200 && r.doResult.StatusCode < 400) {
m.success++
}
}
}
// updateLatencies computes derived summary Metrics which don't need to be Run on every add call.
func (m *Metrics) updateLatencies() {
m.init()
fRequests := float64(m.Requests)
m.Duration = m.Latest.Sub(m.Earliest)
if secs := m.Duration.Seconds(); secs > 0 {
m.Rate = fRequests / secs
}
m.Wait = m.End.Sub(m.Latest)
m.Success = float64(m.success) / fRequests
m.Latencies.Mean = time.Duration(float64(m.Latencies.Total) / fRequests)
m.Latencies.P50 = time.Duration(m.latencies.Get(0.50))
m.Latencies.P95 = time.Duration(m.latencies.Get(0.95))
m.Latencies.P99 = time.Duration(m.latencies.Get(0.99))
}
func (m *Metrics) init() {
if m.latencies == nil {
m.StatusCodes = map[string]int{}
m.errors = map[string]struct{}{}
m.latencies = quantile.New(
quantile.Known(0.50, 0.01),
quantile.Known(0.95, 0.001),
quantile.Known(0.99, 0.0005),
)
}
}