forked from grafana/k6
/
thresholds.go
154 lines (133 loc) · 3.07 KB
/
thresholds.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
/*
*
* 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 stats
import (
"encoding/json"
"github.com/dop251/goja"
"github.com/pkg/errors"
)
const jsEnvSrc = `
function p(pct) {
return __sink__.P(pct/100.0);
};
`
var jsEnv *goja.Program
func init() {
pgm, err := goja.Compile("__env__", jsEnvSrc, true)
if err != nil {
panic(err)
}
jsEnv = pgm
}
type Threshold struct {
Source string
Failed bool
pgm *goja.Program
rt *goja.Runtime
}
func NewThreshold(src string, rt *goja.Runtime) (*Threshold, error) {
pgm, err := goja.Compile("__threshold__", src, true)
if err != nil {
return nil, err
}
return &Threshold{
Source: src,
pgm: pgm,
rt: rt,
}, nil
}
func (t Threshold) RunNoTaint() (bool, error) {
v, err := t.rt.RunProgram(t.pgm)
if err != nil {
return false, err
}
return v.ToBoolean(), nil
}
func (t *Threshold) Run() (bool, error) {
b, err := t.RunNoTaint()
if !b {
t.Failed = true
}
return b, err
}
type Thresholds struct {
Runtime *goja.Runtime
Thresholds []*Threshold
}
func NewThresholds(sources []string) (Thresholds, error) {
rt := goja.New()
if _, err := rt.RunProgram(jsEnv); err != nil {
return Thresholds{}, errors.Wrap(err, "builtin")
}
ts := make([]*Threshold, len(sources))
for i, src := range sources {
t, err := NewThreshold(src, rt)
if err != nil {
return Thresholds{}, errors.Wrapf(err, "%d", i)
}
ts[i] = t
}
return Thresholds{rt, ts}, nil
}
func (ts *Thresholds) UpdateVM(sink Sink) error {
ts.Runtime.Set("__sink__", sink)
for k, v := range sink.Format() {
ts.Runtime.Set(k, v)
}
return nil
}
func (ts *Thresholds) RunAll() (bool, error) {
succ := true
for i, th := range ts.Thresholds {
b, err := th.Run()
if err != nil {
return false, errors.Wrapf(err, "%d", i)
}
if !b {
succ = false
}
}
return succ, nil
}
func (ts *Thresholds) Run(sink Sink) (bool, error) {
if err := ts.UpdateVM(sink); err != nil {
return false, err
}
return ts.RunAll()
}
func (ts *Thresholds) UnmarshalJSON(data []byte) error {
var sources []string
if err := json.Unmarshal(data, &sources); err != nil {
return err
}
newts, err := NewThresholds(sources)
if err != nil {
return err
}
*ts = newts
return nil
}
func (ts Thresholds) MarshalJSON() ([]byte, error) {
sources := make([]string, len(ts.Thresholds))
for i, t := range ts.Thresholds {
sources[i] = t.Source
}
return json.Marshal(sources)
}