This repository has been archived by the owner on Jul 14, 2022. It is now read-only.
/
benchmarkdef.go
265 lines (231 loc) · 7.04 KB
/
benchmarkdef.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
// This definition package implements the transformation of a YAML definition
// to a nomi benchmark
package definition
import (
"io/ioutil"
"path/filepath"
"regexp"
"strconv"
"strings"
"gopkg.in/yaml.v2"
"github.com/giantswarm/nomi/log"
)
type StopCommand string
type ExpectRunningSymbol string
const (
instructionStart = "start"
instructionFloat = "float"
instructionExpectRunning = "expect-running"
instructionSleep = "sleep"
instructionStop = "stop"
StopAll StopCommand = "stop-all"
Lower ExpectRunningSymbol = "<"
Greater ExpectRunningSymbol = ">"
)
type Start struct {
Max int `yaml:"max"`
Interval int `yaml:"interval"`
}
type Float struct {
Rate float64 `yaml:"rate"`
Duration int `yaml:"duration"`
}
type ExpectRunning struct {
Symbol ExpectRunningSymbol `yaml:"symbol"`
Amount int `yaml:"amount"`
}
type Instruction struct {
Start Start
Float Float
ExpectRunning ExpectRunning
Sleep int `yaml:"sleep"`
Stop StopCommand `yaml:"stop"`
}
type Instructions []Instruction
type Application struct {
Name string
Image string
Type string
Network string
Volumes Volumes
Ports []int
Args []string
Envs map[string]string
UnitFilePath string `yaml:"unitfile-path"`
}
type Volumes []Volume
type Volume struct {
Source string
Target string
}
type BenchmarkDef struct {
Application Application
Instructions Instructions
InstanceGroupSize int `yaml:"instancegroup-size"`
}
// BenchmarkDefByFile procudes a benchmark definition out of a YAML file
// Return a benchmark definition object and error
func BenchmarkDefByFile(filePath string) (BenchmarkDef, error) {
def, err := parseBenchmarkDef(filePath)
if err != nil {
log.Logger().Fatalf("error when parsing the benchmark definition %v", err)
}
return def, err
}
// BenchmarkDefByRawInstructions creates a benchmark definition using raw
// instructions and instance group size
// Return a benchmark definition and error
func BenchmarkDefByRawInstructions(instructions string, igSize int) (BenchmarkDef, error) {
re := regexp.MustCompile(`\(([^\)]+)\)`)
parsed := re.FindAllStringSubmatch(instructions, -1)
def := BenchmarkDef{}
def.Instructions = make([]Instruction, 0)
def.InstanceGroupSize = igSize
for _, cmdString := range parsed {
if len(cmdString) < 2 {
continue
}
splitted := strings.Fields(cmdString[1])
if len(splitted) == 0 {
continue
}
cmd := splitted[0]
args := []string{}
if len(splitted) > 1 {
args = splitted[1:]
}
switch cmd {
case instructionStart:
if len(args) != 2 {
log.Logger().Fatalf("start requires 2 arguments: max and time between starts. eg: (start 10 100ms)")
}
max, err := strconv.Atoi(args[0])
if err != nil {
log.Logger().Fatalf("%v", err)
}
var interval int
interval, err = strconv.Atoi(args[1])
if err != nil {
log.Logger().Fatalf("%v", err)
}
def.Instructions = append(def.Instructions, Instruction{
Start: Start{
Max: max,
Interval: interval,
},
})
break
case instructionFloat:
if len(args) != 2 {
log.Logger().Fatalf("float requires 2 arguments: rate and duration")
}
rate, err := strconv.ParseFloat(args[0], 64)
if err != nil {
log.Logger().Fatalf("%v", err)
}
var duration int
duration, err = strconv.Atoi(args[1])
if err != nil {
log.Logger().Fatalf("%v", err)
}
def.Instructions = append(def.Instructions, Instruction{
Float: Float{
Rate: rate,
Duration: duration,
},
})
break
case instructionSleep:
timeout, err := strconv.Atoi(args[0])
if err != nil {
log.Logger().Fatalf("%v", err)
}
def.Instructions = append(def.Instructions, Instruction{
Sleep: timeout,
})
break
case instructionExpectRunning:
if len(args) != 2 {
log.Logger().Fatal("expect-running requires 2 arguments: [><] int")
}
qty, err := strconv.Atoi(args[1])
if err != nil {
log.Logger().Fatalf("%v", err)
}
symbol := ExpectRunningSymbol(args[0])
if symbol != Lower && symbol != Greater {
log.Logger().Fatalf("expect-running comparator has to be > or <")
}
def.Instructions = append(def.Instructions, Instruction{
ExpectRunning: ExpectRunning{
Symbol: symbol,
Amount: qty,
},
})
break
case "stop-all":
def.Instructions = append(def.Instructions, Instruction{
Stop: StopCommand(cmd),
})
break
}
}
return def, nil
}
func parseBenchmarkDef(filePath string) (BenchmarkDef, error) {
filename, _ := filepath.Abs(filePath)
yamlFile, err := ioutil.ReadFile(filename)
if err != nil {
log.Logger().Fatalf("unable to read yaml file %v", err)
}
def := BenchmarkDef{}
if err := yaml.Unmarshal(yamlFile, &def); err != nil {
log.Logger().Fatalf("unable to parse the yaml test definition: %v", err)
}
if !validateDefinition(def) {
log.Logger().Fatal("benchmark definition contains wrong values")
}
return def, nil
}
func validateDefinition(benchmark BenchmarkDef) bool {
if benchmark.InstanceGroupSize <= 0 {
log.Logger().Fatal("instance group size has to be greater or equal to 1")
}
// Validate application definition
if benchmark.Application.Type != "" && benchmark.Application.Type != "unitfiles" && benchmark.Application.Type != "docker" && benchmark.Application.Type != "rkt" {
log.Logger().Errorf("wrong application type %v", benchmark.Application.Type)
return false
}
if benchmark.Application.Type == "unitfiles" && benchmark.Application.UnitFilePath == "" {
log.Logger().Errorf("application unit file path is required for type %v", benchmark.Application.Type)
return false
}
if benchmark.Application.Image == "" && benchmark.Application.Type == "docker" && benchmark.Application.Type == "rkt" {
log.Logger().Warning("application image is empty using standard container")
}
emptyInstruction := &Instruction{}
for _, instruction := range benchmark.Instructions {
if instruction.Start != emptyInstruction.Start && (instruction.Start.Max == 0 || instruction.Start.Interval < 0) {
log.Logger().Errorf("wrong values for the start operation of instruction %v", instruction)
return false
}
if instruction.Float != emptyInstruction.Float && (instruction.Float.Rate <= 0 || instruction.Float.Duration <= 0) {
log.Logger().Errorf("wrong values for the start operation of instruction %v", instruction)
return false
}
if instruction.ExpectRunning != emptyInstruction.ExpectRunning &&
((instruction.ExpectRunning.Symbol != Lower && instruction.ExpectRunning.Symbol != Greater) || instruction.ExpectRunning.Amount < 0) {
log.Logger().Errorf("wrong values for the expect-running operation of instruction %v", instruction)
return false
}
if instruction.Sleep < 0 {
log.Logger().Errorf("wrong values for the sleep operation of instruction %v", instruction)
return false
}
if instruction.Stop != "" && instruction.Stop != StopAll {
log.Logger().Errorf("wrong values for the stop operation of instruction %v", instruction)
return false
}
}
return true
}