-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner.go
120 lines (98 loc) · 2.43 KB
/
runner.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
package ben
import (
"errors"
"fmt"
"strings"
"github.com/drish/ben/builders"
"github.com/drish/ben/config"
"github.com/drish/ben/reporter"
"github.com/drish/ben/utils"
)
// Runner defines the top-level runner struct
type Runner struct {
config *config.Config
}
// Run is the entrypoint method
func (r *Runner) Run(output string, display bool) error {
utils.Welcome()
var reports []reporter.ReportData
for _, env := range r.config.Environments {
// set version as latest if no set
if env.Version == "" {
env.Version = "latest"
}
// set default command or exit
if env.Command == "" {
defaultCmd := config.DefaultCommand(env.Runtime)
if defaultCmd == "" {
return errors.New("command can not be blank")
}
env.Command = defaultCmd
}
before := utils.PrepareBeforeCommands(env.Before)
image := utils.PrepareImage(env.Runtime, env.Version)
command := utils.PrepareCommand(env.Command)
if env.Machine == "local" {
builder := &builders.LocalBuilder{
Image: image,
Before: before,
Command: command,
}
rp, err := r.BuildRuntime(builder, output, display)
if err != nil {
return err
}
reports = append(reports, rp)
} else {
builder := &builders.HyperBuilder{
Image: image,
Before: before,
HyperSize: strings.Split(env.Machine, "-")[1],
Command: command,
}
rp, err := r.BuildRuntime(builder, output, display)
if err != nil {
return err
}
reports = append(reports, rp)
}
}
// generate reports
rep := reporter.NewReporter(output)
if err := rep.Run(reports); err != nil {
return err
}
return nil
}
// BuildRuntime builds the appropriate runtime
func (r *Runner) BuildRuntime(b builders.RuntimeBuilder, output string, display bool) (reporter.ReportData, error) {
// sets up necessary variables
if err := b.Init(); err != nil {
return reporter.ReportData{}, err
}
// pulls base image, run before commands and create benchmark image
if err := b.PrepareImage(); err != nil {
return reporter.ReportData{}, err
}
if err := b.SetupContainer(); err != nil {
return reporter.ReportData{}, err
}
if err := b.Benchmark(); err != nil {
return reporter.ReportData{}, err
}
if err := b.Cleanup(); err != nil {
return reporter.ReportData{}, err
}
if display {
b.Display()
} else {
fmt.Println()
}
return b.Report(), nil
}
// New is the Runner initializer
func New(c *config.Config) *Runner {
return &Runner{
config: c,
}
}