/
http_load_generator.go
233 lines (193 loc) · 6.89 KB
/
http_load_generator.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
/*
Copyright 2019 The Knative Authors
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 sender
import (
"context"
"fmt"
"math/rand"
"net"
"net/http"
"time"
cloudevents "github.com/cloudevents/sdk-go/v1"
"github.com/cloudevents/sdk-go/v1/cloudevents/client"
"github.com/golang/protobuf/ptypes"
"github.com/google/uuid"
"github.com/rogpeppe/fastuuid"
vegeta "github.com/tsenart/vegeta/lib"
"knative.dev/eventing/test/performance/infra/common"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type CloudEventsTargeter struct {
sinkUrl string
msgSize uint
eventType string
eventSource string
body []byte
fixedBody bool
}
var letterBytes = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
const markLetter = byte('"')
// generateRandString returns a random string with the given length.
func generateRandStringPayload(length uint) []byte {
b := make([]byte, length)
b[0] = markLetter
for i := uint(1); i < length-1; i++ {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
b[length-1] = markLetter
return b
}
func NewCloudEventsTargeter(sinkUrl string, msgSize uint, eventType string, eventSource string, fixedBody bool) CloudEventsTargeter {
var body []byte
if fixedBody {
body = generateRandStringPayload(msgSize)
}
return CloudEventsTargeter{
sinkUrl: sinkUrl,
msgSize: msgSize,
eventType: eventType,
eventSource: eventSource,
fixedBody: fixedBody,
body: body,
}
}
func (cet CloudEventsTargeter) VegetaTargeter() vegeta.Targeter {
uuidGen := fastuuid.MustNewGenerator()
ceType := []string{cet.eventType}
ceSource := []string{cet.eventSource}
ceSpecVersion := []string{cloudevents.VersionV1}
ceContentType := []string{cloudevents.ApplicationJSON}
return func(t *vegeta.Target) error {
t.Method = http.MethodPost
t.URL = cet.sinkUrl
t.Header = make(http.Header, 5)
t.Header["Ce-Id"] = []string{uuidGen.Hex128()}
t.Header["Ce-Type"] = ceType
t.Header["Ce-Source"] = ceSource
t.Header["Ce-Specversion"] = ceSpecVersion
t.Header["Content-Type"] = ceContentType
if cet.fixedBody {
t.Body = cet.body
} else {
t.Body = generateRandStringPayload(cet.msgSize)
}
return nil
}
}
type HttpLoadGenerator struct {
eventSource string
sinkUrl string
sentCh chan common.EventTimestamp
acceptedCh chan common.EventTimestamp
warmupAttacker *vegeta.Attacker
paceAttacker *vegeta.Attacker
ceClient client.Client
}
func NewHttpLoadGeneratorFactory(sinkUrl string, minWorkers uint64) LoadGeneratorFactory {
return func(eventSource string, sentCh chan common.EventTimestamp, acceptedCh chan common.EventTimestamp) (generator LoadGenerator, e error) {
if sinkUrl == "" {
panic("Missing --sink flag")
}
loadGen := &HttpLoadGenerator{
eventSource: eventSource,
sinkUrl: sinkUrl,
sentCh: sentCh,
acceptedCh: acceptedCh,
}
loadGen.warmupAttacker = vegeta.NewAttacker(vegeta.Workers(minWorkers))
loadGen.paceAttacker = vegeta.NewAttacker(
vegeta.Client(&http.Client{
Timeout: vegeta.DefaultTimeout,
Transport: requestInterceptor{
before: func(request *http.Request) {
id := request.Header.Get("Ce-Id")
loadGen.sentCh <- common.EventTimestamp{EventId: id, At: ptypes.TimestampNow()}
},
transport: vegetaAttackerTransport(),
after: func(request *http.Request, response *http.Response, e error) {
id := request.Header.Get("Ce-Id")
t := ptypes.TimestampNow()
if e == nil && response.StatusCode >= http.StatusOK && response.StatusCode < http.StatusMultipleChoices {
loadGen.acceptedCh <- common.EventTimestamp{EventId: id, At: t}
}
},
},
}),
vegeta.Workers(minWorkers),
vegeta.MaxBody(0),
)
var err error
loadGen.ceClient, err = newCloudEventsClient(sinkUrl)
if err != nil {
return nil, err
}
return loadGen, nil
}
}
// Since we need to add an interceptor to keep track of timestamps before and after sending events,
// we need to have our own Transport implementation.
// At the same time we still need to use the one implemented in Vegeta, which is optimized to being able to generate
// high loads. But since the function is not exported, we need to add it here in order to use it.
// The below function is mostly copied from https://github.com/tsenart/vegeta/blob/44a49c878dd6f28f04b9b5ce5751490b0dce1e18/lib/attack.go#L80
func vegetaAttackerTransport() *http.Transport {
dialer := &net.Dialer{
LocalAddr: &net.TCPAddr{IP: vegeta.DefaultLocalAddr.IP, Zone: vegeta.DefaultLocalAddr.Zone},
KeepAlive: 30 * time.Second,
}
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: dialer.Dial,
TLSClientConfig: vegeta.DefaultTLSConfig,
MaxIdleConnsPerHost: vegeta.DefaultConnections,
}
}
func newCloudEventsClient(sinkUrl string) (client.Client, error) {
t, err := cloudevents.NewHTTPTransport(
cloudevents.WithBinaryEncoding(),
cloudevents.WithTarget(sinkUrl),
)
if err != nil {
return nil, fmt.Errorf("failed to create transport: %v", err)
}
return cloudevents.NewClient(t)
}
func (h HttpLoadGenerator) Warmup(pace common.PaceSpec, msgSize uint, fixedBody bool) {
targeter := NewCloudEventsTargeter(h.sinkUrl, msgSize, common.WarmupEventType, defaultEventSource, fixedBody).VegetaTargeter()
vegetaResults := h.warmupAttacker.Attack(targeter, vegeta.ConstantPacer{Freq: pace.Rps, Per: time.Second}, pace.Duration, common.WarmupEventType+"-attack")
for range vegetaResults {
}
}
func (h HttpLoadGenerator) RunPace(i int, pace common.PaceSpec, msgSize uint, fixedBody bool) {
targeter := NewCloudEventsTargeter(h.sinkUrl, msgSize, common.MeasureEventType, eventsSource(), fixedBody).VegetaTargeter()
res := h.paceAttacker.Attack(targeter, vegeta.ConstantPacer{Freq: pace.Rps, Per: time.Second}, pace.Duration, fmt.Sprintf("%s-attack-%d", h.eventSource, i))
for range res {
}
}
func (h HttpLoadGenerator) SendGCEvent() {
event := cloudevents.NewEvent(cloudevents.VersionV1)
event.SetID(uuid.New().String())
event.SetDataContentType(cloudevents.ApplicationJSON)
event.SetType(common.GCEventType)
event.SetSource(h.eventSource)
_, _, _ = h.ceClient.Send(context.TODO(), event)
}
func (h HttpLoadGenerator) SendEndEvent() {
event := cloudevents.NewEvent(cloudevents.VersionV1)
event.SetID(uuid.New().String())
event.SetDataContentType(cloudevents.ApplicationJSON)
event.SetType(common.EndEventType)
event.SetSource(h.eventSource)
_, _, _ = h.ceClient.Send(context.TODO(), event)
}