forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minirunner.go
183 lines (153 loc) · 4.71 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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2019 Load Impact
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package minirunner
import (
"context"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/stats"
)
// 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, out chan<- stats.SampleContainer) error
SetupFn func(ctx context.Context, out chan<- stats.SampleContainer) ([]byte, error)
TeardownFn func(ctx context.Context, out chan<- stats.SampleContainer) error
SetupData []byte
NextVUID int64
Group *lib.Group
Options lib.Options
}
// 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(id int64, out chan<- stats.SampleContainer) (lib.InitializedVU, error) {
return &VU{R: r, Out: out, ID: id}, nil
}
// Setup calls the supplied mock setup() function, if present.
func (r *MiniRunner) Setup(ctx context.Context, out chan<- stats.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<- stats.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
return nil
}
// VU is a mock VU, spawned by a MiniRunner.
type VU struct {
R *MiniRunner
Out chan<- stats.SampleContainer
ID int64
Iteration int64
}
// ActiveVU holds a VU and its activation parameters
type ActiveVU struct {
*VU
*lib.VUActivationParams
busy chan struct{}
}
// GetID returns the unique VU ID.
func (vu *VU) GetID() int64 {
return vu.ID
}
// Activate the VU so it will be able to run code.
func (vu *VU) Activate(params *lib.VUActivationParams) lib.ActiveVU {
avu := &ActiveVU{
VU: vu,
VUActivationParams: params,
busy: make(chan struct{}, 1),
}
go func() {
<-params.RunContext.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
}
// 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
}()
state := &lib.State{
Vu: vu.ID,
Iteration: vu.Iteration,
}
newctx := lib.WithState(vu.RunContext, state)
vu.Iteration++
return vu.R.Fn(newctx, vu.Out)
}