forked from apache/cloudstack-csbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsbench.go
538 lines (469 loc) · 16.9 KB
/
csbench.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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 main
import (
"csbench/domain"
"csbench/network"
"csbench/vm"
"csbench/volume"
"flag"
"fmt"
"io"
"math"
"os"
"strings"
"time"
"csbench/apirunner"
"csbench/config"
log "github.com/sirupsen/logrus"
"github.com/apache/cloudstack-go/v2/cloudstack"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/montanaflynn/stats"
"github.com/sourcegraph/conc/pool"
)
var (
profiles = make(map[int]*config.Profile)
)
type Result struct {
Success bool
Duration float64
}
func init() {
logFile, err := os.OpenFile("csmetrics.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Failed to create log file: %v", err)
}
mw := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(mw)
}
func readConfigurations(configFile string) map[int]*config.Profile {
profiles, err := config.ReadProfiles(configFile)
if err != nil {
log.Fatal("Error reading profiles:", err)
}
return profiles
}
func logConfigurationDetails(profiles map[int]*config.Profile) {
apiURL := config.URL
iterations := config.Iterations
page := config.Page
pagesize := config.PageSize
host := config.Host
userProfileNames := make([]string, 0, len(profiles))
for _, profile := range profiles {
userProfileNames = append(userProfileNames, profile.Name)
}
fmt.Printf("\n\n\033[1;34mBenchmarking the CloudStack environment [%s] with the following configuration\033[0m\n\n", apiURL)
fmt.Printf("Management server : %s\n", host)
fmt.Printf("Roles : %s\n", strings.Join(userProfileNames, ","))
fmt.Printf("Iterations : %d\n", iterations)
fmt.Printf("Page : %d\n", page)
fmt.Printf("PageSize : %d\n\n", pagesize)
log.Infof("Found %d profiles in the configuration: ", len(profiles))
log.Infof("Management server : %s", host)
}
func logReport() {
fmt.Printf("\n\n\nLog file : csmetrics.log\n")
fmt.Printf("Reports directory per API : report/%s/\n", config.Host)
fmt.Printf("Number of APIs : %d\n", apirunner.APIscount)
fmt.Printf("Successful APIs : %d\n", apirunner.SuccessAPIs)
fmt.Printf("Failed APIs : %d\n", apirunner.FailedAPIs)
fmt.Printf("Time in seconds per API: %.2f (avg)\n", apirunner.TotalTime/float64(apirunner.APIscount))
fmt.Printf("\n\n\033[1;34m--------------------------------------------------------------------------------\033[0m\n" +
" Done with benchmarking\n" +
"\033[1;34m--------------------------------------------------------------------------------\033[0m\n\n")
}
func getSamples(results []*Result) (stats.Float64Data, stats.Float64Data, stats.Float64Data) {
var allExecutionsSample stats.Float64Data
var successfulExecutionSample stats.Float64Data
var failedExecutionSample stats.Float64Data
for _, result := range results {
duration := math.Round(result.Duration*1000) / 1000
allExecutionsSample = append(allExecutionsSample, duration)
if result.Success {
successfulExecutionSample = append(successfulExecutionSample, duration)
} else {
failedExecutionSample = append(failedExecutionSample, duration)
}
}
return allExecutionsSample, successfulExecutionSample, failedExecutionSample
}
func getRowFromSample(key string, sample stats.Float64Data) table.Row {
min, _ := sample.Min()
min = math.Round(min*1000) / 1000
max, _ := sample.Max()
max = math.Round(max*1000) / 1000
mean, _ := sample.Mean()
mean = math.Round(mean*1000) / 1000
median, _ := sample.Median()
median = math.Round(median*1000) / 1000
percentile90, _ := sample.Percentile(90)
percentile90 = math.Round(percentile90*1000) / 1000
percentile95, _ := sample.Percentile(95)
percentile95 = math.Round(percentile95*1000) / 1000
percentile99, _ := sample.Percentile(99)
percentile99 = math.Round(percentile99*1000) / 1000
return table.Row{key, len(sample), min, max, mean, median, percentile90, percentile95, percentile99}
}
/*
This function will generate a report with the following details:
1. Total Number of executions
2. Number of successful executions
3. Number of failed exections
4. Different statistics like min, max, avg, median, 90th percentile, 95th percentile, 99th percentile for above 3
Output format:
1. CSV
2. TSV
3. Table
*/
func generateReport(results map[string][]*Result, format string, outputFile string) {
fmt.Println("Generating report")
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"Type", "Count", "Min", "Max", "Avg", "Median", "90th percentile", "95th percentile", "99th percentile"})
for key, result := range results {
allExecutionsSample, successfulExecutionSample, failedExecutionSample := getSamples(result)
t.AppendRow(getRowFromSample(fmt.Sprintf("%s - All", key), allExecutionsSample))
if failedExecutionSample.Len() != 0 {
t.AppendRow(getRowFromSample(fmt.Sprintf("%s - Successful", key), successfulExecutionSample))
t.AppendRow(getRowFromSample(fmt.Sprintf("%s - Failed", key), failedExecutionSample))
}
}
if outputFile != "" {
f, err := os.Create(outputFile)
if err != nil {
log.Error("Error creating file: ", err)
}
defer f.Close()
t.SetOutputMirror(f)
}
switch format {
case "csv":
t.RenderCSV()
case "tsv":
t.RenderTSV()
case "table":
t.Render()
}
}
func main() {
dbprofile := flag.Int("dbprofile", 0, "DB profile number")
create := flag.Bool("create", false, "Create resources")
benchmark := flag.Bool("benchmark", false, "Benchmark list APIs")
domainFlag := flag.Bool("domain", false, "Create domain")
limitsFlag := flag.Bool("limits", false, "Update limits to -1")
networkFlag := flag.Bool("network", false, "Create shared network")
vmFlag := flag.Bool("vm", false, "Deploy VMs")
volumeFlag := flag.Bool("volume", false, "Attach Volumes to VMs")
tearDown := flag.Bool("teardown", false, "Tear down all subdomains")
workers := flag.Int("workers", 10, "number of workers to use while creating resources")
format := flag.String("format", "table", "Format of the report (csv, tsv, table). Valid only for create")
outputFile := flag.String("output", "", "Path to output file. Valid only for create")
configFile := flag.String("config", "config/config", "Path to config file")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: go run csmetrictool.go -dbprofile <DB profile number>\n")
fmt.Fprintf(os.Stderr, "Options:\n")
flag.PrintDefaults()
}
flag.Parse()
if !(*create || *benchmark || *tearDown) {
log.Fatal("Please provide one of the following options: -create, -benchmark, -teardown")
}
if *create && !(*domainFlag || *limitsFlag || *networkFlag || *vmFlag || *volumeFlag) {
log.Fatal("Please provide one of the following options with create: -domain, -limits, -network, -vm, -volume")
}
switch *format {
case "csv", "tsv", "table":
// valid format, continue
default:
log.Fatal("Invalid format. Please provide one of the following: csv, tsv, table")
}
if *dbprofile < 0 {
log.Fatal("Invalid DB profile number. Please provide a positive integer.")
}
profiles = readConfigurations(*configFile)
apiURL := config.URL
iterations := config.Iterations
page := config.Page
pagesize := config.PageSize
if *create {
results := createResources(domainFlag, limitsFlag, networkFlag, vmFlag, volumeFlag, workers)
generateReport(results, *format, *outputFile)
}
if *benchmark {
log.Infof("\nStarted benchmarking the CloudStack environment [%s]", apiURL)
logConfigurationDetails(profiles)
for i, profile := range profiles {
userProfileName := profile.Name
log.Infof("Using profile %d.%s for benchmarking", i, userProfileName)
fmt.Printf("\n\033[1;34m============================================================\033[0m\n")
fmt.Printf(" Profile: [%s]\n", userProfileName)
fmt.Printf("\033[1;34m============================================================\033[0m\n")
apirunner.RunAPIs(userProfileName, apiURL, profile.ApiKey, profile.SecretKey, profile.Expires, profile.SignatureVersion, iterations, page, pagesize, *dbprofile)
}
logReport()
log.Infof("Done with benchmarking the CloudStack environment [%s]", apiURL)
}
if *tearDown {
tearDownEnv()
}
}
func createResources(domainFlag, limitsFlag, networkFlag, vmFlag, volumeFlag *bool, workers *int) map[string][]*Result {
apiURL := config.URL
for _, profile := range profiles {
if profile.Name == "admin" {
numVmsPerNetwork := config.NumVms
numVolumesPerVM := config.NumVolumes
cs := cloudstack.NewAsyncClient(apiURL, profile.ApiKey, profile.SecretKey, false)
var results = make(map[string][]*Result)
if *domainFlag {
workerPool := pool.NewWithResults[*Result]().WithMaxGoroutines(*workers)
results["domain"] = createDomains(workerPool, cs, config.ParentDomainId, config.NumDomains)
}
if *limitsFlag {
workerPool := pool.NewWithResults[*Result]().WithMaxGoroutines(*workers)
results["limits"] = updateLimits(workerPool, cs, config.ParentDomainId)
}
if *networkFlag {
workerPool := pool.NewWithResults[*Result]().WithMaxGoroutines(*workers)
results["network"] = createNetwork(workerPool, cs, config.ParentDomainId)
}
if *vmFlag {
workerPool := pool.NewWithResults[*Result]().WithMaxGoroutines(*workers)
results["vm"] = createVms(workerPool, cs, config.ParentDomainId, numVmsPerNetwork)
}
if *volumeFlag {
workerPool := pool.NewWithResults[*Result]().WithMaxGoroutines(*workers)
results["volume"] = createVolumes(workerPool, cs, config.ParentDomainId, numVolumesPerVM)
}
return results
}
}
return nil
}
func createDomains(workerPool *pool.ResultPool[*Result], cs *cloudstack.CloudStackClient, parentDomainId string, count int) []*Result {
progressMarker := int(math.Ceil(float64(count) / 10.0))
start := time.Now()
log.Infof("Creating %d domains", count)
for i := 0; i < count; i++ {
if (i+1)%progressMarker == 0 {
log.Infof("Created %d domains", i+1)
}
workerPool.Go(func() *Result {
taskStart := time.Now()
dmn, err := domain.CreateDomain(cs, parentDomainId)
if err != nil {
return &Result{
Success: false,
Duration: time.Since(taskStart).Seconds(),
}
}
_, err = domain.CreateAccount(cs, dmn.Id)
if err != nil {
return &Result{
Success: false,
Duration: time.Since(taskStart).Seconds(),
}
}
return &Result{
Success: true,
Duration: time.Since(taskStart).Seconds(),
}
})
}
res := workerPool.Wait()
log.Infof("Created %d domains in %.2f seconds", count, time.Since(start).Seconds())
return res
}
func updateLimits(workerPool *pool.ResultPool[*Result], cs *cloudstack.CloudStackClient, parentDomainId string) []*Result {
log.Infof("Fetching subdomains for domain %s", parentDomainId)
domains := domain.ListSubDomains(cs, parentDomainId)
accounts := make([]*cloudstack.Account, 0)
for _, dmn := range domains {
accounts = append(accounts, domain.ListAccounts(cs, dmn.Id)...)
}
progressMarker := int(math.Ceil(float64(len(accounts)) / 10.0))
start := time.Now()
log.Infof("Updating limits for %d accounts", len(accounts))
for i, account := range accounts {
if (i+1)%progressMarker == 0 {
log.Infof("Updated limits for %d accounts", i+1)
}
account := account
workerPool.Go(func() *Result {
taskStart := time.Now()
resp := domain.UpdateLimits(cs, account)
return &Result{
Success: resp,
Duration: time.Since(taskStart).Seconds(),
}
})
}
res := workerPool.Wait()
log.Infof("Updated limits for %d accounts in %.2f seconds", len(accounts), time.Since(start).Seconds())
return res
}
func createNetwork(workerPool *pool.ResultPool[*Result], cs *cloudstack.CloudStackClient, parentDomainId string) []*Result {
log.Infof("Fetching subdomains for domain %s", parentDomainId)
domains := domain.ListSubDomains(cs, parentDomainId)
progressMarker := int(math.Ceil(float64(len(domains)) / 10.0))
start := time.Now()
log.Infof("Creating %d networks", len(domains))
for i, dmn := range domains {
if (i+1)%progressMarker == 0 {
log.Infof("Created %d networks", i+1)
}
i := i
dmn := dmn
workerPool.Go(func() *Result {
taskStart := time.Now()
_, err := network.CreateNetwork(cs, dmn.Id, i)
if err != nil {
return &Result{
Success: false,
Duration: time.Since(taskStart).Seconds(),
}
}
return &Result{
Success: true,
Duration: time.Since(taskStart).Seconds(),
}
})
}
res := workerPool.Wait()
log.Infof("Created %d networks in %.2f seconds", len(domains), time.Since(start).Seconds())
return res
}
func createVms(workerPool *pool.ResultPool[*Result], cs *cloudstack.CloudStackClient, parentDomainId string, numVmPerNetwork int) []*Result {
log.Infof("Fetching subdomains & accounts for domain %s", parentDomainId)
domains := domain.ListSubDomains(cs, parentDomainId)
var accounts []*cloudstack.Account
for i := 0; i < len(domains); i++ {
account := domain.ListAccounts(cs, domains[i].Id)
accounts = append(accounts, account...)
}
domainIdAccountMapping := make(map[string]*cloudstack.Account)
for _, account := range accounts {
domainIdAccountMapping[account.Domainid] = account
}
log.Infof("Fetching networks for subdomains in domain %s", parentDomainId)
var allNetworks []*cloudstack.Network
for _, domain := range domains {
network, _ := network.ListNetworks(cs, domain.Id)
allNetworks = append(allNetworks, network...)
}
progressMarker := int(math.Ceil(float64(len(allNetworks)*numVmPerNetwork) / 10.0))
start := time.Now()
log.Infof("Creating %d VMs", len(allNetworks)*numVmPerNetwork)
for i, network := range allNetworks {
network := network
for j := 1; j <= numVmPerNetwork; j++ {
if (i*j+j)%progressMarker == 0 {
log.Infof("Created %d VMs", i*j+j)
}
workerPool.Go(func() *Result {
taskStart := time.Now()
_, err := vm.DeployVm(cs, network.Domainid, network.Id, domainIdAccountMapping[network.Domainid].Name)
if err != nil {
return &Result{
Success: false,
Duration: time.Since(taskStart).Seconds(),
}
}
return &Result{
Success: true,
Duration: time.Since(taskStart).Seconds(),
}
})
}
}
res := workerPool.Wait()
log.Infof("Created %d VMs in %.2f seconds", len(allNetworks)*numVmPerNetwork, time.Since(start).Seconds())
return res
}
func createVolumes(workerPool *pool.ResultPool[*Result], cs *cloudstack.CloudStackClient, parentDomainId string, numVolumesPerVM int) []*Result {
log.Infof("Fetching all VMs in subdomains for domain %s", parentDomainId)
domains := domain.ListSubDomains(cs, parentDomainId)
var allVMs []*cloudstack.VirtualMachine
for _, dmn := range domains {
vms, err := vm.ListVMs(cs, dmn.Id)
if err != nil {
log.Warn("Error listing VMs: ", err)
continue
}
allVMs = append(allVMs, vms...)
}
progressMarker := int(math.Ceil(float64(len(allVMs)*numVolumesPerVM) / 10.0))
start := time.Now()
log.Infof("Creating %d volumes", len(allVMs)*numVolumesPerVM)
unsuitableVmCount := 0
for i, vm := range allVMs {
vm := vm
if vm.State != "Running" && vm.State != "Stopped" {
unsuitableVmCount++
continue
}
for j := 1; j <= numVolumesPerVM; j++ {
if (i*j+j)%progressMarker == 0 {
log.Infof("Created %d volumes", i*j+j)
}
workerPool.Go(func() *Result {
taskStart := time.Now()
vol, err := volume.CreateVolume(cs, vm.Domainid, vm.Account)
if err != nil {
return &Result{
Success: false,
Duration: time.Since(taskStart).Seconds(),
}
}
_, err = volume.AttachVolume(cs, vol.Id, vm.Id)
if err != nil {
return &Result{
Success: false,
Duration: time.Since(taskStart).Seconds(),
}
}
return &Result{
Success: true,
Duration: time.Since(taskStart).Seconds(),
}
})
}
}
if unsuitableVmCount > 0 {
log.Warnf("Found %d VMs in unsuitable state", unsuitableVmCount)
}
res := workerPool.Wait()
log.Infof("Created %d volumes in %.2f seconds", (len(allVMs)-unsuitableVmCount)*numVolumesPerVM, time.Since(start).Seconds())
return res
}
func tearDownEnv() {
parentDomain := config.ParentDomainId
apiURL := config.URL
for _, profile := range profiles {
userProfileName := profile.Name
if userProfileName == "admin" {
cs := cloudstack.NewAsyncClient(apiURL, profile.ApiKey, profile.SecretKey, false)
domains := domain.ListSubDomains(cs, parentDomain)
log.Infof("Deleting %d domains", len(domains))
for _, subdomain := range domains {
domain.DeleteDomain(cs, subdomain.Id)
}
break
}
}
}