-
Notifications
You must be signed in to change notification settings - Fork 34
/
collect_http.go
295 lines (255 loc) · 9.85 KB
/
collect_http.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
284
285
286
287
288
289
290
291
292
293
294
295
package base
import (
"errors"
"fmt"
"io"
"os"
"time"
"dario.cat/mergo"
"fortio.org/fortio/fhttp"
"fortio.org/fortio/periodic"
log "github.com/iter8-tools/iter8/base/log"
)
// errorRange has lower and upper limits for HTTP status codes. HTTP status code within this range is considered an error
type errorRange struct {
// Lower end of the range
Lower *int `json:"lower,omitempty" yaml:"lower,omitempty"`
// Upper end of the range
Upper *int `json:"upper,omitempty" yaml:"upper,omitempty"`
}
// endpoint contains the inputs for one endpoint
type endpoint struct {
// NumRequests is the number of requests to be sent to the app. Default value is 100.
NumRequests *int64 `json:"numRequests,omitempty" yaml:"numRequests,omitempty"`
// Duration of this task. Specified in the Go duration string format (example, 5s). If both duration and numRequests are specified, then duration is ignored.
Duration *string `json:"duration,omitempty" yaml:"duration,omitempty"`
// QPS is the number of requests per second sent to the app. Default value is 8.0.
QPS *float32 `json:"qps,omitempty" yaml:"qps,omitempty"`
// Connections is the number of number of parallel connections used to send load. Default value is 4.
Connections *int `json:"connections,omitempty" yaml:"connections,omitempty"`
// PayloadStr is the string data to be sent as payload. If this field is specified, Iter8 will send HTTP POST requests to the app using this string as the payload.
PayloadStr *string `json:"payloadStr,omitempty" yaml:"payloadStr,omitempty"`
// PayloadFile is payload file. If this field is specified, Iter8 will send HTTP POST requests to the app using data in this file. If both `payloadStr` and `payloadFile` are specified, the former is ignored.
PayloadFile *string `json:"payloadFile,omitempty" yaml:"payloadFile,omitempty"`
// ContentType is the type of the payload. Indicated using the Content-Type HTTP header value. This is intended to be used in conjunction with one of the `payload*` fields above. If this field is specified, Iter8 will send HTTP POST requests to the app using this content type header value.
ContentType *string `json:"contentType,omitempty" yaml:"contentType,omitempty"`
// ErrorRanges is a list of errorRange values. Each range specifies an upper and/or lower limit on HTTP status codes. HTTP responses that fall within these error ranges are considered error. Default value is {{lower: 400},} - i.e., HTTP status codes >= 400 are considered as error.
ErrorRanges []errorRange `json:"errorRanges,omitempty" yaml:"errorRanges,omitempty"`
// Percentiles are the latency percentiles collected by this task. Percentile values have a single digit precision (i.e., rounded to one decimal place). Default value is {50.0, 75.0, 90.0, 95.0, 99.0, 99.9,}.
Percentiles []float64 `json:"percentiles,omitempty" yaml:"percentiles,omitempty"`
// HTTP headers to use in the query; optional
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"`
// URL to use for querying the app
URL string `json:"url" yaml:"url"`
// AllowInitialErrors allows and doesn't abort on initial warmup errors
AllowInitialErrors *bool `json:"allowInitialErrors,omitempty" yaml:"allowInitialErrors,omitempty"`
}
// collectHTTPInputs contain the inputs to the metrics collection task to be executed.
type collectHTTPInputs struct {
endpoint
// Warmup indicates if task execution is for warmup purposes; if so the results will be ignored
Warmup *bool `json:"warmup,omitempty" yaml:"warmup,omitempty"`
// Endpoints is used to define multiple endpoints to test
Endpoints map[string]endpoint `json:"endpoints" yaml:"endpoints"`
}
// HTTPResult is the raw data sent to the metrics server
// This data will be transformed into httpDashboard when getHTTPGrafana is called
// Key is the endpoint
type HTTPResult map[string]*fhttp.HTTPRunnerResults
const (
// CollectHTTPTaskName is the name of this task which performs load generation and metrics collection.
CollectHTTPTaskName = "http"
// defaultQPS is the default number of queries per second
defaultQPS = float32(8)
// defaultHTTPNumRequests is the default number of queries (in total)
defaultHTTPNumRequests = int64(100)
// defaultHTTPConnections is the default number of connections (parallel go routines)
defaultHTTPConnections = 4
)
var (
// defaultErrorRanges is set so that status codes 400 and above are considered errors
defaultErrorRanges = []errorRange{{Lower: IntPointer(400)}}
// defaultPercentiles are the default latency percentiles computed by this task
defaultPercentiles = [...]float64{50.0, 75.0, 90.0, 95.0, 99.0, 99.9}
)
// collectHTTPTask enables performance testing of HTTP services.
type collectHTTPTask struct {
// TaskMeta has fields common to all tasks
TaskMeta
// With contains the inputs to this task
With collectHTTPInputs `json:"with" yaml:"with"`
}
// initializeDefaults sets default values for the collect task
func (t *collectHTTPTask) initializeDefaults() {
if t.With.NumRequests == nil && t.With.Duration == nil {
t.With.NumRequests = int64Pointer(defaultHTTPNumRequests)
}
if t.With.QPS == nil {
t.With.QPS = float32Pointer(defaultQPS)
}
if t.With.Connections == nil {
t.With.Connections = IntPointer(defaultHTTPConnections)
}
if t.With.ErrorRanges == nil {
t.With.ErrorRanges = defaultErrorRanges
}
// default percentiles are always collected
// if other percentiles are specified, they are collected as well
for _, p := range defaultPercentiles {
t.With.Percentiles = append(t.With.Percentiles, p)
}
tmp := Uniq(t.With.Percentiles)
t.With.Percentiles = []float64{}
for _, val := range tmp {
t.With.Percentiles = append(t.With.Percentiles, val.(float64))
}
if t.With.AllowInitialErrors == nil {
t.With.AllowInitialErrors = BoolPointer(false)
}
}
// validateInputs for this task
func (t *collectHTTPTask) validateInputs() error {
return nil
}
// getFortioOptions constructs Fortio's HTTP runner options based on collect task inputs
func getFortioOptions(c endpoint) (*fhttp.HTTPRunnerOptions, error) {
if c.QPS == nil {
return nil, errors.New("no value for QPS")
}
if c.Connections == nil {
return nil, errors.New("no value for Connections")
}
if c.AllowInitialErrors == nil {
return nil, errors.New("no value for AllowInitialErrors")
}
// basic runner
fo := &fhttp.HTTPRunnerOptions{
RunnerOptions: periodic.RunnerOptions{
RunType: "Iter8 HTTP performance test",
QPS: float64(*c.QPS),
NumThreads: *c.Connections,
Percentiles: c.Percentiles,
Out: io.Discard,
},
HTTPOptions: fhttp.HTTPOptions{
URL: c.URL,
},
AllowInitialErrors: *c.AllowInitialErrors,
}
// num requests
if c.NumRequests != nil {
fo.RunnerOptions.Exactly = *c.NumRequests
}
// add duration
var duration time.Duration
var err error
if c.Duration != nil {
duration, err = time.ParseDuration(*c.Duration)
if err == nil {
fo.RunnerOptions.Duration = duration
} else {
log.Logger.WithStackTrace(err.Error()).Error("unable to parse duration")
return nil, err
}
}
// content type & payload
if c.ContentType != nil {
fo.ContentType = *c.ContentType
}
if c.PayloadStr != nil {
fo.Payload = []byte(*c.PayloadStr)
}
if c.PayloadFile != nil {
b, err := os.ReadFile(*c.PayloadFile)
if err != nil {
return nil, err
}
fo.Payload = b
}
// headers
for key, value := range c.Headers {
if err = fo.AddAndValidateExtraHeader(key + ":" + value); err != nil {
log.Logger.WithStackTrace("unable to add header").Error(err)
return nil, err
}
}
return fo, nil
}
// getFortioResults collects Fortio run results
// func (t *collectHTTPTask) getFortioResults() (*fhttp.HTTPRunnerResults, error) {
// key is the metric prefix
// key is the endpoint
func (t *collectHTTPTask) getFortioResults() (HTTPResult, error) {
// the main idea is to run Fortio with proper options
var err error
results := HTTPResult{}
if len(t.With.Endpoints) > 0 {
log.Logger.Trace("multiple endpoints")
for endpointID, endpoint := range t.With.Endpoints {
endpoint := endpoint // prevent implicit memory aliasing
log.Logger.Trace(fmt.Sprintf("endpoint: %s", endpointID))
// merge endpoint config with baseline config
if err := mergo.Merge(&endpoint, t.With.endpoint); err != nil {
log.Logger.Error(fmt.Sprintf("could not merge Fortio options for endpoint \"%s\"", endpointID))
return nil, err
}
efo, err := getFortioOptions(endpoint)
if err != nil {
log.Logger.Error(fmt.Sprintf("could not get Fortio options for endpoint \"%s\"", endpointID))
return nil, err
}
log.Logger.Trace("got fortio options")
log.Logger.Trace("URL: ", efo.URL)
log.Logger.Trace("run fortio HTTP test")
ifr, err := fhttp.RunHTTPTest(efo)
if err != nil {
log.Logger.WithStackTrace(err.Error()).Error("fortio failed")
continue
}
results[endpointID] = ifr
}
} else {
fo, err := getFortioOptions(t.With.endpoint)
if err != nil {
log.Logger.Error("could not get Fortio options")
return nil, err
}
log.Logger.Trace("got fortio options")
log.Logger.Trace("URL: ", fo.URL)
log.Logger.Trace("run fortio HTTP test")
ifr, err := fhttp.RunHTTPTest(fo)
if err != nil {
log.Logger.WithStackTrace(err.Error()).Error("fortio failed")
return nil, err
}
results[t.With.URL] = ifr
}
return results, err
}
// run executes this task
func (t *collectHTTPTask) run(exp *Experiment) error {
err := t.validateInputs()
if err != nil {
return err
}
t.initializeDefaults()
// run fortio
data, err := t.getFortioResults()
if err != nil {
return err
}
// ignore results if warmup
if t.With.Warmup != nil && *t.With.Warmup {
log.Logger.Debug("warmup: ignoring results")
return nil
}
// this task populates insights in the experiment
// hence, initialize insights with num versions (= 1)
err = exp.Result.initInsightsWithNumVersions(1)
if err != nil {
return err
}
// write data to Insights
exp.Result.Insights.TaskData[CollectHTTPTaskName] = data
return nil
}