Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify execution requirements calculation in k6 inspect #2409

Merged
merged 1 commit into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
oleiade marked this conversation as resolved.
Show resolved Hide resolved
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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO: completely remove this?
// TODO: Bundle.IsExecutable is now available,
// it should be used directly and this method removed

I think we could be a bit lost if we read the comment from outside of the context of this PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I am not sure if we can remove this without a lot more refactoring 🤔 Bundle.IsExecutable is not available everywhere as a substitute... 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh true, I always forgot about the Runner interface. Should we remove the comment? 🤔 It seems not really possible with the current code.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dug a little more here and realized that the need for the public method in Bundle disappears because of the other simplifications I did in #2412 with loadedTest 🎉 So I ended up reverting this part of the change there, see c3fec32

I think it's fine to merge this as it is and remove it in the other PR, given that it allows us to merge these PRs one by one instead of all at once.

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