Skip to content

Commit

Permalink
Simplify requirements calculation in k6 inspect
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Mar 7, 2022
1 parent fea17a1 commit 0a21f8f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
27 changes: 6 additions & 21 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/spf13/afero"
"github.com/spf13/cobra"

"go.k6.io/k6/core/local"
"go.k6.io/k6/js"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/metrics"
Expand All @@ -57,7 +56,6 @@ func getInspectCmd(logger *logrus.Logger, globalFlags *commandFlags) *cobra.Comm
return err
}
registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)

var b *js.Bundle
typ := globalFlags.runType
Expand Down Expand Up @@ -85,7 +83,7 @@ func getInspectCmd(logger *logrus.Logger, globalFlags *commandFlags) *cobra.Comm
inspectOutput := interface{}(b.Options)

if addExecReqs {
inspectOutput, err = addExecRequirements(b, builtinMetrics, registry, logger, globalFlags)
inspectOutput, err = addExecRequirements(b, logger, globalFlags)
if err != nil {
return err
}
Expand All @@ -112,37 +110,24 @@ func getInspectCmd(logger *logrus.Logger, globalFlags *commandFlags) *cobra.Comm
return inspectCmd
}

func addExecRequirements(b *js.Bundle,
builtinMetrics *metrics.BuiltinMetrics, registry *metrics.Registry,
logger *logrus.Logger, globalFlags *commandFlags) (interface{}, error) {
// TODO: after #1048 issue, consider rewriting this without a Runner:
// just creating ExecutionPlan directly from validated options

runner, err := js.NewFromBundle(logger, b, builtinMetrics, registry)
if err != nil {
return nil, err
}

func addExecRequirements(b *js.Bundle, logger *logrus.Logger, globalFlags *commandFlags) (interface{}, error) {
conf, err := getConsolidatedConfig(
afero.NewOsFs(), Config{}, runner.GetOptions(), buildEnvMap(os.Environ()), globalFlags)
afero.NewOsFs(), Config{}, b.Options, buildEnvMap(os.Environ()), globalFlags)
if err != nil {
return nil, err
}

conf, err = deriveAndValidateConfig(conf, runner.IsExecutable, logger)
conf, err = deriveAndValidateConfig(conf, b.IsExecutable, logger)
if err != nil {
return nil, err
}

if err = runner.SetOptions(conf.Options); err != nil {
return nil, err
}
execScheduler, err := local.NewExecutionScheduler(runner, logger)
et, err := lib.NewExecutionTuple(conf.ExecutionSegment, conf.ExecutionSegmentSequence)
if err != nil {
return nil, err
}

executionPlan := execScheduler.GetExecutionPlan()
executionPlan := conf.Scenarios.GetFullExecutionRequirements(et)
duration, _ := lib.GetEndOffset(executionPlan)

return struct {
Expand Down
7 changes: 7 additions & 0 deletions js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,13 @@ func (b *Bundle) Instantiate(
return bi, instErr
}

// IsExecutable returns whether the given name is an exported and
// executable function in the script.
func (b *Bundle) IsExecutable(name string) bool {
_, exists := b.exports[name]
return exists
}

// Instantiates the bundle into an existing runtime. Not public because it also messes with a bunch
// of other things, will potentially thrash data and makes a mess in it if the operation fails.
func (b *Bundle) instantiate(logger logrus.FieldLogger, rt *goja.Runtime, init *InitContext, vuID uint64) error {
Expand Down
5 changes: 3 additions & 2 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,10 @@ func (r *Runner) GetOptions() lib.Options {

// IsExecutable returns whether the given name is an exported and
// executable function in the script.
//
// TODO: completely remove this?
func (r *Runner) IsExecutable(name string) bool {
_, exists := r.Bundle.exports[name]
return exists
return r.Bundle.IsExecutable(name)
}

// HandleSummary calls the specified summary callback, if supplied.
Expand Down

0 comments on commit 0a21f8f

Please sign in to comment.