Skip to content

Commit

Permalink
Drop newModuleVUImpl and some more refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Jul 7, 2022
1 parent cccf2b3 commit 21163f1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 44 deletions.
19 changes: 11 additions & 8 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type BundleInstance struct {
env map[string]string

exports map[string]goja.Callable

moduleVUImpl *moduleVUImpl
}

// NewBundle creates a new bundle from a source file and a filesystem.
Expand Down Expand Up @@ -250,22 +252,21 @@ func (b *Bundle) getExports(logger logrus.FieldLogger, rt *goja.Runtime, options
}

// Instantiate creates a new runtime from this bundle.
func (b *Bundle) Instantiate(
logger logrus.FieldLogger, vuID uint64, vuImpl *moduleVUImpl,
) (bi *BundleInstance, instErr error) {
func (b *Bundle) Instantiate(logger logrus.FieldLogger, vuID uint64) (*BundleInstance, error) {
// Instantiate the bundle into a new VM using a bound init context. This uses a context with a
// runtime, but no state, to allow module-provided types to function within the init context.
vuImpl.runtime = goja.New()
vuImpl := &moduleVUImpl{runtime: goja.New()}
init := newBoundInitContext(b.BaseInitContext, vuImpl)
if err := b.instantiate(logger, vuImpl.runtime, init, vuID); err != nil {
return nil, err
}

rt := vuImpl.runtime
bi = &BundleInstance{
Runtime: rt,
exports: make(map[string]goja.Callable),
env: b.RuntimeOptions.Env,
bi := &BundleInstance{
Runtime: rt,
exports: make(map[string]goja.Callable),
env: b.RuntimeOptions.Env,
moduleVUImpl: vuImpl,
}

// Grab any exported functions that could be executed. These were
Expand All @@ -284,6 +285,8 @@ func (b *Bundle) Instantiate(
} else {
jsOptionsObj = jsOptions.ToObject(rt)
}

var instErr error
b.Options.ForEachSpecified("json", func(key string, val interface{}) {
if err := jsOptionsObj.Set(key, val); err != nil {
instErr = err
Expand Down
18 changes: 9 additions & 9 deletions js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ func TestNewBundleFromArchive(t *testing.T) {
logger := testutils.NewLogger(t)
checkBundle := func(t *testing.T, b *Bundle) {
require.Equal(t, lib.Options{VUs: null.IntFrom(12345)}, b.Options)
bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
val, err := bi.exports[consts.DefaultFn](goja.Undefined())
require.NoError(t, err)
Expand Down Expand Up @@ -574,7 +574,7 @@ func TestNewBundleFromArchive(t *testing.T) {
}
b, err := NewBundleFromArchive(logger, arc, lib.RuntimeOptions{}, metrics.NewRegistry())
require.NoError(t, err)
bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
val, err := bi.exports[consts.DefaultFn](goja.Undefined())
require.NoError(t, err)
Expand Down Expand Up @@ -718,7 +718,7 @@ func TestOpen(t *testing.T) {
for source, b := range map[string]*Bundle{"source": sourceBundle, "archive": arcBundle} {
b := b
t.Run(source, func(t *testing.T) {
bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
v, err := bi.exports[consts.DefaultFn](goja.Undefined())
require.NoError(t, err)
Expand Down Expand Up @@ -754,7 +754,7 @@ func TestBundleInstantiate(t *testing.T) {
require.NoError(t, err)
logger := testutils.NewLogger(t)

bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
v, err := bi.exports[consts.DefaultFn](goja.Undefined())
require.NoError(t, err)
Expand All @@ -774,7 +774,7 @@ func TestBundleInstantiate(t *testing.T) {
require.NoError(t, err)
logger := testutils.NewLogger(t)

bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
bi.Runtime.Set("val", false)
v, err := bi.exports[consts.DefaultFn](goja.Undefined())
Expand All @@ -795,7 +795,7 @@ func TestBundleInstantiate(t *testing.T) {
require.NoError(t, err)
logger := testutils.NewLogger(t)

bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
// Ensure `options` properties are correctly marshalled
jsOptions := bi.Runtime.Get("options").ToObject(bi.Runtime)
Expand All @@ -807,7 +807,7 @@ func TestBundleInstantiate(t *testing.T) {
// Ensure options propagate correctly from outside to the script
optOrig := b.Options.VUs
b.Options.VUs = null.IntFrom(10)
bi2, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi2, err := b.Instantiate(logger, 0)
require.NoError(t, err)
jsOptions = bi2.Runtime.Get("options").ToObject(bi2.Runtime)
vus = jsOptions.Get("vus").Export()
Expand Down Expand Up @@ -843,7 +843,7 @@ func TestBundleEnv(t *testing.T) {
require.Equal(t, "1", b.RuntimeOptions.Env["TEST_A"])
require.Equal(t, "", b.RuntimeOptions.Env["TEST_B"])

bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
_, err = bi.exports[consts.DefaultFn](goja.Undefined())
require.NoError(t, err)
Expand Down Expand Up @@ -879,7 +879,7 @@ func TestBundleNotSharable(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
for i := 0; i < vus; i++ {
bi, err := b.Instantiate(logger, uint64(i), newModuleVUImpl())
bi, err := b.Instantiate(logger, uint64(i))
require.NoError(t, err)
for j := 0; j < iters; j++ {
bi.Runtime.Set("__ITER", j)
Expand Down
4 changes: 0 additions & 4 deletions js/initcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,6 @@ type moduleVUImpl struct {
eventLoop *eventloop.EventLoop
}

func newModuleVUImpl() *moduleVUImpl {
return &moduleVUImpl{}
}

func (m *moduleVUImpl) Context() context.Context {
return m.ctx
}
Expand Down
34 changes: 16 additions & 18 deletions js/initcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestInitContextRequire(t *testing.T) {
`)
require.NoError(t, err, "bundle error")

bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
assert.NoError(t, err, "instance error")

exports := bi.Runtime.Get("exports").ToObject(bi.Runtime)
Expand All @@ -93,7 +93,7 @@ func TestInitContextRequire(t *testing.T) {
`)
require.NoError(t, err)

bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)

exports := bi.Runtime.Get("exports").ToObject(bi.Runtime)
Expand Down Expand Up @@ -203,7 +203,7 @@ func TestInitContextRequire(t *testing.T) {
assert.Contains(t, b.BaseInitContext.programs, "file://"+constPath)
}

_, err = b.Instantiate(logger, 0, newModuleVUImpl())
_, err = b.Instantiate(logger, 0)
require.NoError(t, err)
})
}
Expand All @@ -227,7 +227,7 @@ func TestInitContextRequire(t *testing.T) {
b, err := getSimpleBundle(t, "/script.js", data, fs)
require.NoError(t, err)

bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
_, err = bi.exports[consts.DefaultFn](goja.Undefined())
assert.NoError(t, err)
Expand Down Expand Up @@ -256,7 +256,7 @@ func createAndReadFile(t *testing.T, file string, content []byte, expectedLength
return nil, err
}

bi, err := b.Instantiate(testutils.NewLogger(t), 0, newModuleVUImpl())
bi, err := b.Instantiate(testutils.NewLogger(t), 0)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -372,8 +372,7 @@ func TestRequestWithBinaryFile(t *testing.T) {
`, srv.URL), fs)
require.NoError(t, err)

vuImpl := newModuleVUImpl()
bi, err := b.Instantiate(testutils.NewLogger(t), 0, vuImpl)
bi, err := b.Instantiate(testutils.NewLogger(t), 0)
require.NoError(t, err)

root, err := lib.NewGroup("", nil)
Expand All @@ -385,7 +384,7 @@ func TestRequestWithBinaryFile(t *testing.T) {

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
vuImpl.state = &lib.State{
bi.moduleVUImpl.state = &lib.State{
Options: lib.Options{},
Logger: logger,
Group: root,
Expand All @@ -407,7 +406,7 @@ func TestRequestWithBinaryFile(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
vuImpl.ctx = ctx
bi.moduleVUImpl.ctx = ctx

v, err := bi.exports[consts.DefaultFn](goja.Undefined())
require.NoError(t, err)
Expand Down Expand Up @@ -520,8 +519,7 @@ func TestRequestWithMultipleBinaryFiles(t *testing.T) {
`, srv.URL), fs)
require.NoError(t, err)

vuImpl := newModuleVUImpl()
bi, err := b.Instantiate(testutils.NewLogger(t), 0, vuImpl)
bi, err := b.Instantiate(testutils.NewLogger(t), 0)
require.NoError(t, err)

root, err := lib.NewGroup("", nil)
Expand All @@ -533,7 +531,7 @@ func TestRequestWithMultipleBinaryFiles(t *testing.T) {

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
vuImpl.state = &lib.State{
bi.moduleVUImpl.state = &lib.State{
Options: lib.Options{},
Logger: logger,
Group: root,
Expand All @@ -555,7 +553,7 @@ func TestRequestWithMultipleBinaryFiles(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())
defer cancel()
vuImpl.ctx = ctx
bi.moduleVUImpl.ctx = ctx

v, err := bi.exports[consts.DefaultFn](goja.Undefined())
require.NoError(t, err)
Expand All @@ -572,7 +570,7 @@ func TestInitContextVU(t *testing.T) {
export default function() { return vu; }
`)
require.NoError(t, err)
bi, err := b.Instantiate(testutils.NewLogger(t), 5, newModuleVUImpl())
bi, err := b.Instantiate(testutils.NewLogger(t), 5)
require.NoError(t, err)
v, err := bi.exports[consts.DefaultFn](goja.Undefined())
require.NoError(t, err)
Expand Down Expand Up @@ -605,7 +603,7 @@ export default function(){
b, err := getSimpleBundle(t, "/script.js", data, fs)
require.NoError(t, err)

bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
_, err = bi.exports[consts.DefaultFn](goja.Undefined())
require.Error(t, err)
Expand Down Expand Up @@ -636,7 +634,7 @@ export default function () {
b, err := getSimpleBundle(t, "/script.js", data, fs)
require.NoError(t, err)

bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
_, err = bi.exports[consts.DefaultFn](goja.Undefined())
require.Error(t, err)
Expand Down Expand Up @@ -668,7 +666,7 @@ export default function () {
b, err := getSimpleBundle(t, "/script.js", data, fs)
require.NoError(t, err)

bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
_, err = bi.exports[consts.DefaultFn](goja.Undefined())
require.Error(t, err)
Expand Down Expand Up @@ -699,7 +697,7 @@ export default function () {
b, err := getSimpleBundle(t, "/script.js", data, fs)
require.NoError(t, err)

bi, err := b.Instantiate(logger, 0, newModuleVUImpl())
bi, err := b.Instantiate(logger, 0)
require.NoError(t, err)
_, err = bi.exports[consts.DefaultFn](goja.Undefined())
require.Error(t, err)
Expand Down
6 changes: 1 addition & 5 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ func (r *Runner) NewVU(idLocal, idGlobal uint64, samplesOut chan<- metrics.Sampl
// nolint:funlen
func (r *Runner) newVU(idLocal, idGlobal uint64, samplesOut chan<- metrics.SampleContainer) (*VU, error) {
// Instantiate a new bundle, make a VU out of it.
moduleVUImpl := newModuleVUImpl()
bi, err := r.Bundle.Instantiate(r.Logger, idLocal, moduleVUImpl)
bi, err := r.Bundle.Instantiate(r.Logger, idLocal)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -252,7 +251,6 @@ func (r *Runner) newVU(idLocal, idGlobal uint64, samplesOut chan<- metrics.Sampl
BPool: bpool.NewBufferPool(100),
Samples: samplesOut,
scenarioIter: make(map[string]uint64),
moduleVUImpl: moduleVUImpl,
}

vu.state = &lib.State{
Expand Down Expand Up @@ -602,8 +600,6 @@ type VU struct {
state *lib.State
// count of iterations executed by this VU in each scenario
scenarioIter map[string]uint64

moduleVUImpl *moduleVUImpl
}

// Verify that interfaces are implemented
Expand Down

0 comments on commit 21163f1

Please sign in to comment.