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 requirements calculation in k6 inspect #2444

Merged
merged 1 commit into from
Mar 15, 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) {
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)
}
Comment on lines +350 to 354
Copy link
Contributor

Choose a reason for hiding this comment

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

This is used so unless we change the Runner interfaces it's needed to stay. Arguably the Runner interface is quite big and IMO has parts that have just been piled one over the years. A full redesign though is probably better left for later.

I just did look into it and I think IsExecutualbe as well as Setup/Teardown around stuff should probably be moved to not the Runner and consolidated a bunch. Unfortunately that looks like it will need a lot of changes across k6 and some unknown unknowns that I don't think we should try to figure out now.

But tl;dr: you can't remove this, easily

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I already addressed it in #2409 (comment), but I remove this TODO in a future PR, for now I'd prefer to merge it just so I don't have to rebase everything again.

But yeah, mid to long term, now that we have a (hopefully) viable solution for refactoring the mess of the Engine away, we really need to start looking at refactoring the Runner mess 😞 See also this comment from one of the other PRs #2412 (comment)


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