From 39b65bfdd9d412995094cd0e03689750d5c1aca7 Mon Sep 17 00:00:00 2001 From: Hendrik Haase Date: Thu, 5 Sep 2024 12:09:18 +0200 Subject: [PATCH] added group options --- examples/dummy_executor.go | 2 +- examples/main.go | 8 +-- go.mod | 2 +- group.go | 25 -------- group_options.go | 76 +++++++++++++++++++++++++ http/http_endpoint.go | 2 +- http/options.go | 4 +- http/url_builder/query_param.go | 2 +- http/url_builder/query_param_options.go | 2 +- http/url_builder/url_builder.go | 2 +- loadtest.go | 4 +- options.go | 2 +- runner.go | 2 +- 13 files changed, 92 insertions(+), 41 deletions(-) create mode 100644 group_options.go diff --git a/examples/dummy_executor.go b/examples/dummy_executor.go index 9eb4c20..ff76bba 100644 --- a/examples/dummy_executor.go +++ b/examples/dummy_executor.go @@ -3,7 +3,7 @@ package main import ( "context" "fmt" - "github.com/scayle/goload" + "github.com/HenriBeck/goload" ) type DummyExecutor struct { diff --git a/examples/main.go b/examples/main.go index eee078c..4442ed2 100644 --- a/examples/main.go +++ b/examples/main.go @@ -2,10 +2,10 @@ package main import ( "fmt" - "github.com/scayle/goload" - goload_http "github.com/scayle/goload/http" - "github.com/scayle/goload/http/url_builder" - "github.com/scayle/goload/pacer" + "github.com/HenriBeck/goload" + goload_http "github.com/HenriBeck/goload/http" + "github.com/HenriBeck/goload/http/url_builder" + "github.com/HenriBeck/goload/pacer" "time" ) diff --git a/go.mod b/go.mod index 5f86590..abb94c3 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/scayle/goload +module github.com/HenriBeck/goload go 1.21 diff --git a/group.go b/group.go index 4ddae1f..385dd4b 100644 --- a/group.go +++ b/group.go @@ -3,7 +3,6 @@ package goload import ( "context" "github.com/mroth/weightedrand/v2" - "github.com/rs/zerolog/log" "time" ) @@ -14,30 +13,6 @@ type executorGroup struct { timeout time.Duration } -// TODO: add options -func NewGroup(name string, weight int, executors []Executor) Executor { - if len(executors) == 0 { - log.Fatal().Msg("group can't be empty") - } - choises := make([]weightedrand.Choice[Executor, int], 0, len(executors)) - for _, exec := range executors { - choises = append(choises, weightedrand.NewChoice(exec, exec.Options().Weight)) - } - chooser, err := weightedrand.NewChooser( - choises..., - ) - if err != nil { - log.Fatal().Err(err).Msg("can't create chooser") - } - - return &executorGroup{ - name: name, - chooser: chooser, - weight: weight, - timeout: 0, - } -} - func (e *executorGroup) Execute(ctx context.Context) ExecutionResponse { return e.chooser.Pick().Execute(ctx) } diff --git a/group_options.go b/group_options.go new file mode 100644 index 0000000..95c8087 --- /dev/null +++ b/group_options.go @@ -0,0 +1,76 @@ +package goload + +import ( + "github.com/mroth/weightedrand/v2" + "github.com/rs/zerolog/log" + "time" +) + +type GroupOptions struct { + name string + weight int + timeout time.Duration + executors []Executor +} + +type GroupOption func(*GroupOptions) + +func WithGroup(opts ...GroupOption) Executor { + options := &GroupOptions{} + for _, opt := range opts { + opt(options) + } + + if len(options.executors) == 0 { + log.Fatal().Msg("group can't be empty") + } + choises := make([]weightedrand.Choice[Executor, int], 0, len(options.executors)) + for _, exec := range options.executors { + choises = append(choises, weightedrand.NewChoice(exec, exec.Options().Weight)) + } + chooser, err := weightedrand.NewChooser( + choises..., + ) + if err != nil { + log.Fatal().Err(err).Msg("can't create chooser") + } + + if options.weight == 0 { + weightSum := 0 + for _, exec := range options.executors { + weightSum += exec.Options().Weight + } + options.weight = weightSum + } + + return &executorGroup{ + name: options.name, + chooser: chooser, + weight: options.weight, + timeout: options.timeout, + } +} + +func WithGroupWeight(weight int) GroupOption { + return func(options *GroupOptions) { + options.weight = weight + } +} + +func WithGroupTimeout(timeout time.Duration) GroupOption { + return func(options *GroupOptions) { + options.timeout = timeout + } +} + +func WithGroupExecutors(executors ...Executor) GroupOption { + return func(options *GroupOptions) { + options.executors = append(options.executors, executors...) + } +} + +func WithGroupName(name string) GroupOption { + return func(options *GroupOptions) { + options.name = name + } +} diff --git a/http/http_endpoint.go b/http/http_endpoint.go index 6112668..b6b8a50 100644 --- a/http/http_endpoint.go +++ b/http/http_endpoint.go @@ -3,8 +3,8 @@ package goload_http import ( "context" "fmt" + "github.com/HenriBeck/goload" "github.com/rs/zerolog/log" - "github.com/scayle/goload" "io" "net/http" "net/url" diff --git a/http/options.go b/http/options.go index 725faac..08186bc 100644 --- a/http/options.go +++ b/http/options.go @@ -2,8 +2,8 @@ package goload_http import ( "errors" - "github.com/scayle/goload" - "github.com/scayle/goload/http/url_builder" + "github.com/HenriBeck/goload" + "github.com/HenriBeck/goload/http/url_builder" "io" "net/http" "net/url" diff --git a/http/url_builder/query_param.go b/http/url_builder/query_param.go index 52eb8e9..bffeefb 100644 --- a/http/url_builder/query_param.go +++ b/http/url_builder/query_param.go @@ -1,9 +1,9 @@ package url_builder import ( + "github.com/HenriBeck/goload/utils/random" "github.com/mroth/weightedrand/v2" "github.com/rs/zerolog/log" - "github.com/scayle/goload/utils/random" "net/url" ) diff --git a/http/url_builder/query_param_options.go b/http/url_builder/query_param_options.go index bc66e65..f136463 100644 --- a/http/url_builder/query_param_options.go +++ b/http/url_builder/query_param_options.go @@ -1,9 +1,9 @@ package url_builder import ( + "github.com/HenriBeck/goload/utils/random" "github.com/mroth/weightedrand/v2" "github.com/rs/zerolog/log" - "github.com/scayle/goload/utils/random" "strconv" ) diff --git a/http/url_builder/url_builder.go b/http/url_builder/url_builder.go index a33f2ae..927ef8f 100644 --- a/http/url_builder/url_builder.go +++ b/http/url_builder/url_builder.go @@ -2,8 +2,8 @@ package url_builder import ( "fmt" + "github.com/HenriBeck/goload/utils/random" "github.com/rs/zerolog/log" - "github.com/scayle/goload/utils/random" "net/url" "strings" ) diff --git a/loadtest.go b/loadtest.go index 42ef1bd..7b90248 100644 --- a/loadtest.go +++ b/loadtest.go @@ -3,10 +3,10 @@ package goload import ( "context" "fmt" + "github.com/HenriBeck/goload/pacer" + ctx_utils "github.com/HenriBeck/goload/utils/ctx" "github.com/rs/zerolog" "github.com/rs/zerolog/log" - "github.com/scayle/goload/pacer" - ctx_utils "github.com/scayle/goload/utils/ctx" "math" "os" "time" diff --git a/options.go b/options.go index efd0e4c..2946477 100644 --- a/options.go +++ b/options.go @@ -2,7 +2,7 @@ package goload import ( "context" - "github.com/scayle/goload/pacer" + "github.com/HenriBeck/goload/pacer" "time" ) diff --git a/runner.go b/runner.go index 4ed5d8b..ca5bc6e 100644 --- a/runner.go +++ b/runner.go @@ -2,9 +2,9 @@ package goload import ( "context" + "github.com/HenriBeck/goload/pacer" "github.com/mroth/weightedrand/v2" "github.com/rs/zerolog/log" - "github.com/scayle/goload/pacer" "sync" "time" )