-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.go
208 lines (177 loc) · 3.95 KB
/
model.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
package ddosy
import (
"bytes"
"net/http"
"text/template"
"time"
vegeta "github.com/tsenart/vegeta/v12/lib"
)
type ScheduleResponseWeb struct {
Id uint64 `json:"id"`
Error string `json:"error"`
}
type ScheduleRequestWeb struct {
Endpoint string `json:"endpoint"`
LoadPatterns []LoadPatternWeb `json:"load"`
TrafficPatterns []TrafficPatternWeb `json:"traffic"`
}
type LoadPatternWeb struct {
Duration string `json:"duration"`
Linear *LinearLoadWeb `json:"linear"`
Sine *SineLoadWeb `json:"sine"`
}
type TrafficPatternWeb struct {
Weight float64 `json:"weight"`
Payload string `json:"payload"`
}
type LinearLoadWeb struct {
StartRate int `json:"startRate"`
EndRate int `json:"endRate"`
}
type SineLoadWeb struct {
Mean int `json:"mean"`
Amp int `json:"amplitude"`
Period string `json:"period"`
}
// database
type DatabaseTask struct {
Id uint64
StatusId TaskStatus
CreatedAt time.Time
StartedAt *time.Time
KilledAt *time.Time
DoneAt *time.Time
Request ScheduleRequestWeb
Results string
}
type TaskStatus int8
const (
Unknown TaskStatus = iota
Scheduled
Running
Killed
Done
)
var TaskStatusStr = map[TaskStatus]string{
Unknown: "Unknown",
Scheduled: "Scheduled",
Running: "Running",
Killed: "Killed",
Done: "Done",
}
// internal
type LoadTask struct {
id uint64
traffic TrafficPattern
load []LoadPattern
}
type LoadPattern struct {
duration time.Duration
pacer vegeta.Pacer
}
type TrafficPattern struct {
endpoint string
header http.Header
method string
dist TrafficDistribution
}
type TrafficDistribution struct {
weigths []float64
payloads [][]byte
}
func NewLoadTask(req ScheduleRequestWeb) *LoadTask {
load := make([]LoadPattern, len(req.LoadPatterns))
for i, p := range req.LoadPatterns {
load[i] = NewLoadPattern(p)
}
return &LoadTask{
traffic: NewTrafficPattern(req.Endpoint, req.TrafficPatterns),
load: load,
}
}
func NewTrafficPattern(endpoint string, patterns []TrafficPatternWeb) TrafficPattern {
ws := make([]float64, len(patterns))
payloads := make([][]byte, len(patterns))
for i, p := range patterns {
ws[i] = p.Weight
payloads[i] = []byte(p.Payload)
}
dist := TrafficDistribution{
weigths: CreateDistribution(ws),
payloads: payloads,
}
header := http.Header{}
header.Add("Content-Type", "application/json")
return TrafficPattern{
endpoint: endpoint,
header: header,
method: http.MethodPost,
dist: dist,
}
}
func NewLoadPattern(pattern LoadPatternWeb) LoadPattern {
duration, _ := time.ParseDuration(pattern.Duration)
var pacer vegeta.Pacer
if pattern.Linear != nil {
l := pattern.Linear
slope := float64(l.EndRate-l.StartRate) / duration.Seconds()
pacer = vegeta.LinearPacer{
StartAt: vegeta.Rate{Freq: l.StartRate, Per: time.Second},
Slope: slope,
}
} else {
s := pattern.Sine
p, _ := time.ParseDuration(s.Period)
pacer = vegeta.SinePacer{
Period: p,
Mean: vegeta.Rate{Freq: s.Mean, Per: time.Second},
Amp: vegeta.Rate{Freq: s.Amp, Per: time.Second},
}
}
return LoadPattern{
duration: duration,
pacer: pacer,
}
}
func (t *LoadTask) Targeter() vegeta.Targeter {
return NewWeightedTargeter(t.traffic)
}
// reporting
const reportTemplate = `
Endpoint: {{ .Endpoint }}
Status: {{ .Status }}
Created: {{ .CreatedAt }}
{{- if .StartedAt }}
Started: {{ .StartedAt }}
{{- end}}
{{- if .DoneAt }}
Done: {{ .DoneAt }}
{{- end}}
{{- if .KilledAt }}
Killed: {{ .KilledAt }}
{{- end}}
{{.Metrics}}
Request: {{.Request}}
`
type LoadTaskReport struct {
Endpoint string
Status string
CreatedAt string
StartedAt string
KilledAt string
DoneAt string
Metrics string
Request string
}
func (r LoadTaskReport) String() (string, error) {
t, err := template.New("report").Parse(reportTemplate)
if err != nil {
return "", err
}
var buf bytes.Buffer
err = t.Execute(&buf, r)
if err != nil {
return "", err
}
return buf.String(), nil
}