-
Notifications
You must be signed in to change notification settings - Fork 0
/
minirunner.go
227 lines (189 loc) · 5.78 KB
/
minirunner.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
package minirunner
import (
"context"
"io"
"github.com/gesemaya/k6/lib"
"github.com/gesemaya/k6/metrics"
)
// Ensure mock implementations conform to the interfaces.
var (
_ lib.Runner = &MiniRunner{}
_ lib.InitializedVU = &VU{}
_ lib.ActiveVU = &ActiveVU{}
)
// MiniRunner partially implements the lib.Runner interface, but instead of
// using a real JS runtime, it allows us to directly specify the options and
// functions with Go code.
type MiniRunner struct {
Fn func(ctx context.Context, state *lib.State, out chan<- metrics.SampleContainer) error
SetupFn func(ctx context.Context, out chan<- metrics.SampleContainer) ([]byte, error)
TeardownFn func(ctx context.Context, out chan<- metrics.SampleContainer) error
HandleSummaryFn func(context.Context, *lib.Summary) (map[string]io.Reader, error)
SetupData []byte
Group *lib.Group
Options lib.Options
PreInitState *lib.TestPreInitState
runTags *metrics.TagSet
}
// MakeArchive isn't implemented, it always returns nil and is just here to
// satisfy the lib.Runner interface.
func (r MiniRunner) MakeArchive() *lib.Archive {
return nil
}
// NewVU returns a new VU with an incremental ID.
func (r *MiniRunner) NewVU(
_ context.Context, idLocal, idGlobal uint64, out chan<- metrics.SampleContainer,
) (lib.InitializedVU, error) {
state := &lib.State{VUID: idLocal, VUIDGlobal: idGlobal, Iteration: int64(-1)}
if r.runTags != nil {
state.Tags = lib.NewVUStateTags(r.runTags)
}
return &VU{
R: r,
Out: out,
ID: idLocal,
IDGlobal: idGlobal,
state: state,
scenarioIter: make(map[string]uint64),
}, nil
}
// Setup calls the supplied mock setup() function, if present.
func (r *MiniRunner) Setup(ctx context.Context, out chan<- metrics.SampleContainer) (err error) {
if fn := r.SetupFn; fn != nil {
r.SetupData, err = fn(ctx, out)
}
return
}
// GetSetupData returns json representation of the setup data if setup() is
// specified and was ran, nil otherwise.
func (r MiniRunner) GetSetupData() []byte {
return r.SetupData
}
// SetSetupData saves the externally supplied setup data as JSON in the runner.
func (r *MiniRunner) SetSetupData(data []byte) {
r.SetupData = data
}
// Teardown calls the supplied mock teardown() function, if present.
func (r MiniRunner) Teardown(ctx context.Context, out chan<- metrics.SampleContainer) error {
if fn := r.TeardownFn; fn != nil {
return fn(ctx, out)
}
return nil
}
// GetDefaultGroup returns the default group.
func (r MiniRunner) GetDefaultGroup() *lib.Group {
if r.Group == nil {
r.Group = &lib.Group{}
}
return r.Group
}
// IsExecutable satisfies lib.Runner, but is mocked for MiniRunner since
// it doesn't deal with JS.
func (r MiniRunner) IsExecutable(name string) bool {
return true
}
// GetOptions returns the supplied options struct.
func (r MiniRunner) GetOptions() lib.Options {
return r.Options
}
// SetOptions allows you to override the runner options.
func (r *MiniRunner) SetOptions(opts lib.Options) error {
r.Options = opts
if r.PreInitState != nil {
r.runTags = r.PreInitState.Registry.RootTagSet().WithTagsFromMap(r.Options.RunTags)
}
return nil
}
// HandleSummary calls the specified summary callback, if supplied.
func (r *MiniRunner) HandleSummary(ctx context.Context, s *lib.Summary) (map[string]io.Reader, error) {
if r.HandleSummaryFn != nil {
return r.HandleSummaryFn(ctx, s)
}
return nil, nil
}
// VU is a mock VU, spawned by a MiniRunner.
type VU struct {
R *MiniRunner
Out chan<- metrics.SampleContainer
ID, IDGlobal uint64
Iteration int64
state *lib.State
// count of iterations executed by this VU in each scenario
scenarioIter map[string]uint64
}
// ActiveVU holds a VU and its activation parameters
type ActiveVU struct {
*VU
*lib.VUActivationParams
busy chan struct{}
scenarioName string
getNextIterations func() (uint64, uint64)
scIterLocal, scIterGlobal uint64
}
// GetID returns the unique VU ID.
func (vu *VU) GetID() uint64 {
return vu.ID
}
// State returns the VU's State.
func (vu *VU) State() *lib.State {
return vu.state
}
// Activate the VU so it will be able to run code.
func (vu *VU) Activate(params *lib.VUActivationParams) lib.ActiveVU {
ctx := params.RunContext
vu.state.GetScenarioVUIter = func() uint64 {
return vu.scenarioIter[params.Scenario]
}
avu := &ActiveVU{
VU: vu,
VUActivationParams: params,
busy: make(chan struct{}, 1),
scenarioName: params.Scenario,
scIterLocal: ^uint64(0),
scIterGlobal: ^uint64(0),
getNextIterations: params.GetNextIterationCounters,
}
vu.state.GetScenarioLocalVUIter = func() uint64 {
return avu.scIterLocal
}
vu.state.GetScenarioGlobalVUIter = func() uint64 {
return avu.scIterGlobal
}
go func() {
<-ctx.Done()
// Wait for the VU to stop running, if it was, and prevent it from
// running again for this activation
avu.busy <- struct{}{}
if params.DeactivateCallback != nil {
params.DeactivateCallback(vu)
}
}()
return avu
}
func (vu *ActiveVU) incrIteration() {
vu.Iteration++
vu.state.Iteration = vu.Iteration
if _, ok := vu.scenarioIter[vu.scenarioName]; ok {
vu.scenarioIter[vu.scenarioName]++
} else {
vu.scenarioIter[vu.scenarioName] = 0
}
vu.scIterLocal, vu.scIterGlobal = vu.getNextIterations()
}
// RunOnce runs the mock default function once, incrementing its iteration.
func (vu *ActiveVU) RunOnce() error {
if vu.R.Fn == nil {
return nil
}
select {
case <-vu.RunContext.Done():
return vu.RunContext.Err() // we are done, return
case vu.busy <- struct{}{}:
// nothing else can run now, and the VU cannot be deactivated
}
defer func() {
<-vu.busy // unlock deactivation again
}()
vu.incrIteration()
return vu.R.Fn(vu.RunContext, vu.State(), vu.Out)
}