Skip to content

Commit

Permalink
Moved threshold execution to goja
Browse files Browse the repository at this point in the history
  • Loading branch information
Emily Ekberg committed May 10, 2017
1 parent 438b652 commit 12d51c0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 37 deletions.
49 changes: 27 additions & 22 deletions stats/thresholds.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,53 @@ package stats
import (
"encoding/json"

"github.com/dop251/goja"
"github.com/pkg/errors"
"github.com/robertkrimen/otto"
)

const jsEnv = `
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

script *otto.Script
vm *otto.Otto
pgm *goja.Program
rt *goja.Runtime
}

func NewThreshold(src string, vm *otto.Otto) (*Threshold, error) {
script, err := vm.Compile("__threshold__", src)
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,
script: script,
vm: vm,
pgm: pgm,
rt: rt,
}, nil
}

func (t Threshold) RunNoTaint() (bool, error) {
v, err := t.vm.Run(t.script)
v, err := t.rt.RunProgram(t.pgm)
if err != nil {
return false, err
}
return v.ToBoolean()
return v.ToBoolean(), nil
}

func (t *Threshold) Run() (bool, error) {
Expand All @@ -71,36 +81,31 @@ func (t *Threshold) Run() (bool, error) {
}

type Thresholds struct {
VM *otto.Otto
Runtime *goja.Runtime
Thresholds []*Threshold
}

func NewThresholds(sources []string) (Thresholds, error) {
vm := otto.New()

if _, err := vm.Eval(jsEnv); err != nil {
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, vm)
t, err := NewThreshold(src, rt)
if err != nil {
return Thresholds{}, errors.Wrapf(err, "%d", i)
}
ts[i] = t
}
return Thresholds{vm, ts}, nil
return Thresholds{rt, ts}, nil
}

func (ts *Thresholds) UpdateVM(sink Sink) error {
if err := ts.VM.Set("__sink__", sink); err != nil {
return err
}
ts.Runtime.Set("__sink__", sink)
for k, v := range sink.Format() {
if err := ts.VM.Set(k, v); err != nil {
return errors.Wrapf(err, "%s", k)
}
ts.Runtime.Set(k, v)
}
return nil
}
Expand Down
25 changes: 10 additions & 15 deletions stats/thresholds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ import (
"encoding/json"
"testing"

"github.com/robertkrimen/otto"
"github.com/dop251/goja"
"github.com/stretchr/testify/assert"
)

func TestNewThreshold(t *testing.T) {
src := `1+1==2`
vm := otto.New()
th, err := NewThreshold(src, vm)
rt := goja.New()
th, err := NewThreshold(src, rt)
assert.NoError(t, err)

assert.Equal(t, src, th.Source)
assert.False(t, th.Failed)
assert.NotNil(t, th.script)
assert.Equal(t, vm, th.vm)
assert.NotNil(t, th.pgm)
assert.Equal(t, rt, th.rt)
}

func TestThresholdRun(t *testing.T) {
t.Run("true", func(t *testing.T) {
th, err := NewThreshold(`1+1==2`, otto.New())
th, err := NewThreshold(`1+1==2`, goja.New())
assert.NoError(t, err)

t.Run("no taint", func(t *testing.T) {
Expand All @@ -61,7 +61,7 @@ func TestThresholdRun(t *testing.T) {
})

t.Run("false", func(t *testing.T) {
th, err := NewThreshold(`1+1==4`, otto.New())
th, err := NewThreshold(`1+1==4`, goja.New())
assert.NoError(t, err)

t.Run("no taint", func(t *testing.T) {
Expand Down Expand Up @@ -94,8 +94,8 @@ func TestNewThresholds(t *testing.T) {
for i, th := range ts.Thresholds {
assert.Equal(t, sources[i], th.Source)
assert.False(t, th.Failed)
assert.NotNil(t, th.script)
assert.Equal(t, ts.VM, th.vm)
assert.NotNil(t, th.pgm)
assert.Equal(t, ts.Runtime, th.rt)
}
})
}
Expand All @@ -104,12 +104,7 @@ func TestThresholdsUpdateVM(t *testing.T) {
ts, err := NewThresholds(nil)
assert.NoError(t, err)
assert.NoError(t, ts.UpdateVM(DummySink{"a": 1234.5}))

v, err := ts.VM.Get("a")
assert.NoError(t, err)
f, err := v.ToFloat()
assert.NoError(t, err)
assert.Equal(t, 1234.5, f)
assert.Equal(t, 1234.5, ts.Runtime.Get("a").ToFloat())
}

func TestThresholdsRunAll(t *testing.T) {
Expand Down

0 comments on commit 12d51c0

Please sign in to comment.