forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctl.go
188 lines (158 loc) · 4.67 KB
/
ctl.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
package main
import (
log "github.com/Sirupsen/logrus"
"github.com/ghodss/yaml"
"github.com/loadimpact/speedboat/api"
"github.com/loadimpact/speedboat/lib"
"gopkg.in/guregu/null.v3"
"gopkg.in/urfave/cli.v1"
"os"
"strconv"
)
var commandStatus = cli.Command{
Name: "status",
Usage: "Looks up the status of a running test",
ArgsUsage: " ",
Action: actionStatus,
Description: `Status will print the status of a running test to stdout in YAML format.
Use the global --address/-a flag to specify the host to connect to; the
default is port 6565 on the local machine.
Endpoint: /v1/status`,
}
var commandScale = cli.Command{
Name: "scale",
Usage: "Scales a running test",
ArgsUsage: "vus",
Action: actionScale,
Description: `Scale will change the number of active VUs of a running test.
It is an error to scale a test beyond vus-max; this is because instantiating
new VUs is a very expensive operation, which may skew test results if done
during a running test, and should thus be done deliberately.
Endpoint: /v1/status`,
}
var commandCap = cli.Command{
Name: "cap",
Usage: "Changes the VU cap for a running test",
ArgsUsage: "max",
Action: actionCap,
Description: `Cap will change the maximum number of VUs for a test.
Because instantiating new VUs is a potentially very expensive operation,
both in terms of CPU and RAM, you should be aware that you may see a bump in
response times and skewed averages if you increase the cap during a running
test.
It's recommended to pause the test before creating a large number of VUs.
Endpoint: /v1/status`,
}
var commandPause = cli.Command{
Name: "pause",
Usage: "Pauses a running test",
ArgsUsage: " ",
Action: actionPause,
Description: `Pause pauses a running test.
Running VUs will finish their current iterations, then suspend themselves
until woken by the test's resumption. A sleeping VU will consume no CPU
cycles, but will still occupy memory.
Endpoint: /v1/status`,
}
var commandResume = cli.Command{
Name: "resume",
Usage: "Resumes a paused test",
ArgsUsage: " ",
Action: actionResume,
Description: `Resume resumes a previously paused test.
This is the opposite of the pause command, and will do nothing to an already
running test.
Endpoint: /v1/status`,
}
func dumpYAML(v interface{}) error {
bytes, err := yaml.Marshal(v)
if err != nil {
log.WithError(err).Error("Serialization Error")
return err
}
_, _ = os.Stdout.Write(bytes)
return nil
}
func actionStatus(cc *cli.Context) error {
client, err := api.NewClient(cc.GlobalString("address"))
if err != nil {
log.WithError(err).Error("Couldn't create a client")
return err
}
status, err := client.Status()
if err != nil {
log.WithError(err).Error("Error")
return err
}
return dumpYAML(status)
}
func actionScale(cc *cli.Context) error {
args := cc.Args()
if len(args) != 1 {
return cli.NewExitError("Wrong number of arguments!", 1)
}
vus, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
log.WithError(err).Error("Error")
return err
}
client, err := api.NewClient(cc.GlobalString("address"))
if err != nil {
log.WithError(err).Error("Couldn't create a client")
return err
}
status, err := client.UpdateStatus(lib.Status{VUs: null.IntFrom(vus)})
if err != nil {
log.WithError(err).Error("Error")
return err
}
return dumpYAML(status)
}
func actionCap(cc *cli.Context) error {
args := cc.Args()
if len(args) != 1 {
return cli.NewExitError("Wrong number of arguments!", 1)
}
max, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
log.WithError(err).Error("Error")
return err
}
client, err := api.NewClient(cc.GlobalString("address"))
if err != nil {
log.WithError(err).Error("Couldn't create a client")
return err
}
status, err := client.UpdateStatus(lib.Status{VUsMax: null.IntFrom(max)})
if err != nil {
log.WithError(err).Error("Error")
return err
}
return dumpYAML(status)
}
func actionPause(cc *cli.Context) error {
client, err := api.NewClient(cc.GlobalString("address"))
if err != nil {
log.WithError(err).Error("Couldn't create a client")
return err
}
status, err := client.UpdateStatus(lib.Status{Running: null.BoolFrom(false)})
if err != nil {
log.WithError(err).Error("Error")
return err
}
return dumpYAML(status)
}
func actionResume(cc *cli.Context) error {
client, err := api.NewClient(cc.GlobalString("address"))
if err != nil {
log.WithError(err).Error("Couldn't create a client")
return err
}
status, err := client.UpdateStatus(lib.Status{Running: null.BoolFrom(true)})
if err != nil {
log.WithError(err).Error("Error")
return err
}
return dumpYAML(status)
}