forked from grafana/k6
/
k6.go
185 lines (155 loc) · 4.45 KB
/
k6.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
/*
*
* k6 - a next-generation load testing tool
* Copyright (C) 2016 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 k6
import (
"context"
"math/rand"
"sync/atomic"
"time"
"github.com/dop251/goja"
"github.com/pkg/errors"
"github.com/loadimpact/k6/js/common"
"github.com/loadimpact/k6/lib"
"github.com/loadimpact/k6/lib/metrics"
"github.com/loadimpact/k6/stats"
)
type K6 struct{}
// ErrGroupInInitContext is returned when group() are using in the init context
var ErrGroupInInitContext = common.NewInitContextError("Using group() in the init context is not supported")
// ErrCheckInInitContext is returned when check() are using in the init context
var ErrCheckInInitContext = common.NewInitContextError("Using check() in the init context is not supported")
func New() *K6 {
return &K6{}
}
func (*K6) Fail(msg string) (goja.Value, error) {
return goja.Undefined(), errors.New(msg)
}
func (*K6) Sleep(ctx context.Context, secs float64) {
timer := time.NewTimer(time.Duration(secs * float64(time.Second)))
select {
case <-timer.C:
case <-ctx.Done():
timer.Stop()
}
}
func (*K6) RandomSeed(ctx context.Context, seed int64) {
randSource := rand.New(rand.NewSource(seed)).Float64
rt := common.GetRuntime(ctx)
rt.SetRandSource(randSource)
}
func (*K6) Group(ctx context.Context, name string, fn goja.Callable) (goja.Value, error) {
state := lib.GetState(ctx)
if state == nil {
return nil, ErrGroupInInitContext
}
if fn == nil {
return nil, errors.New("group() requires a callback as a second argument")
}
g, err := state.Group.Group(name)
if err != nil {
return goja.Undefined(), err
}
old := state.Group
state.Group = g
shouldUpdateTag := state.Options.SystemTags.Has(stats.TagGroup)
if shouldUpdateTag {
state.Tags["group"] = g.Path
}
defer func() {
state.Group = old
if shouldUpdateTag {
state.Tags["group"] = old.Path
}
}()
startTime := time.Now()
ret, err := fn(goja.Undefined())
t := time.Now()
tags := state.CloneTags()
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{
Time: t,
Metric: metrics.GroupDuration,
Tags: stats.IntoSampleTags(&tags),
Value: stats.D(t.Sub(startTime)),
})
return ret, err
}
func (*K6) Check(ctx context.Context, arg0, checks goja.Value, extras ...goja.Value) (bool, error) {
state := lib.GetState(ctx)
if state == nil {
return false, ErrCheckInInitContext
}
rt := common.GetRuntime(ctx)
t := time.Now()
// Prepare the metric tags
commonTags := state.CloneTags()
if len(extras) > 0 {
obj := extras[0].ToObject(rt)
for _, k := range obj.Keys() {
commonTags[k] = obj.Get(k).String()
}
}
succ := true
var exc error
obj := checks.ToObject(rt)
for _, name := range obj.Keys() {
val := obj.Get(name)
tags := make(map[string]string, len(commonTags))
for k, v := range commonTags {
tags[k] = v
}
// Resolve the check record.
check, err := state.Group.Check(name)
if err != nil {
return false, err
}
if state.Options.SystemTags.Has(stats.TagCheck) {
tags["check"] = check.Name
}
// Resolve callables into values.
fn, ok := goja.AssertFunction(val)
if ok {
tmpVal, err := fn(goja.Undefined(), arg0)
val = tmpVal
if err != nil {
val = rt.ToValue(false)
exc = err
}
}
sampleTags := stats.IntoSampleTags(&tags)
// Emit! (But only if we have a valid context.)
select {
case <-ctx.Done():
default:
if val.ToBoolean() {
atomic.AddInt64(&check.Passes, 1)
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{Time: t, Metric: metrics.Checks, Tags: sampleTags, Value: 1})
} else {
atomic.AddInt64(&check.Fails, 1)
stats.PushIfNotDone(ctx, state.Samples, stats.Sample{Time: t, Metric: metrics.Checks, Tags: sampleTags, Value: 0})
// A single failure makes the return value false.
succ = false
}
}
if exc != nil {
return succ, exc
}
}
return succ, nil
}