Skip to content

Commit

Permalink
add missing parts
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrik-haase committed Aug 27, 2024
1 parent 0a501fc commit 3df116e
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 89 deletions.
15 changes: 8 additions & 7 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"github.com/HenriBeck/goload"
goload_http "github.com/HenriBeck/goload/http"
"github.com/HenriBeck/goload/http/query_param"
"github.com/HenriBeck/goload/http/url_builder"
"github.com/HenriBeck/goload/pacer"
"time"
)
Expand All @@ -14,20 +14,21 @@ func main() {
goload.WithDuration(5*time.Minute),
//goload.WithLinearRampUpPacer(pacer.Rate{Freq: 30, Per: time.Minute}, pacer.Rate{Freq: 2, Per: time.Second}, 1*time.Minute),
goload.WithConstantPacer(pacer.Rate{Freq: 2, Per: time.Second}),
goload_http.WithBasePath("http://test.k6.io"),
goload.WithExecutors(
goload_http.NewEndpoint(
goload_http.WithName("test"),
goload_http.WithURL("http://test.k6.io"),
goload_http.WithURL("/"),
goload_http.WithValidateResponse(goload_http.Status2xxResponseValidation),
),
goload_http.NewEndpoint(
goload_http.WithName("pi"),
goload_http.WithURLBuilder(
goload_http.WithRawURL("https://test.k6.io/pi.php"),
goload_http.WithQueryParams(
query_param.New(
query_param.WithName("decimals"),
query_param.WithRandomNumberValue(1, 20),
url_builder.WithRawURL("/pi.php"),
url_builder.WithQueryParams(
url_builder.NewQueryParameter(
url_builder.WithParamName("decimals"),
url_builder.WithRandomNumberParamValue(1, 20),
),
),
),
Expand Down
53 changes: 53 additions & 0 deletions group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package goload

import (
"context"
"github.com/mroth/weightedrand/v2"
"time"
)

type executorGroup struct {
name string
chooser *weightedrand.Chooser[Executor, int]
weight int
timeout time.Duration
}

// TODO: add options
func NewGroup(name string, weight int, executors []Executor) Executor {
if len(executors) == 0 {
panic("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 {
panic(err)
}

return &executorGroup{
name: name,
chooser: chooser,
weight: weight,
timeout: 0,
}
}

func (e *executorGroup) Execute(ctx context.Context) ExecutionResponse {
return e.chooser.Pick().Execute(ctx)
}

func (e *executorGroup) Name() string {
return e.name
}

func (e *executorGroup) Options() *ExecutorOptions {
return &ExecutorOptions{
Weight: e.weight,
Timeout: e.timeout,
}
}
4 changes: 3 additions & 1 deletion http/http_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"time"
)

var basePath *string

type EndpointOption func(ep *endpoint)

func NewEndpoint(opts ...EndpointOption) goload.Executor {
Expand Down Expand Up @@ -96,7 +98,7 @@ func (e *endpoint) Options() *goload.ExecutorOptions {
}

func Status2xxResponseValidation(response *http.Response) error {
if response.StatusCode >= 200 && response.StatusCode < 300 {
if response.StatusCode < 200 && response.StatusCode >= 300 {
return fmt.Errorf("non 2xx status code: %d", response.StatusCode)
}
return nil
Expand Down
38 changes: 21 additions & 17 deletions http/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package goload_http

import (
"errors"
"github.com/HenriBeck/goload/http/query_param"
"github.com/HenriBeck/goload"
"github.com/HenriBeck/goload/http/url_builder"
"io"
"net/http"
"net/url"
Expand Down Expand Up @@ -65,11 +66,18 @@ func WithClient(client http.Client) EndpointOption {

func WithURL(rawURL string) EndpointOption {
return func(ep *endpoint) {
u, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
ep.urlFunc = func() *url.URL {
if basePath != nil {
var err error
rawURL, err = url.JoinPath(*basePath, rawURL)
if err != nil {
panic(err)
}
}
u, err := url.Parse(rawURL)
if err != nil {
panic(err)
}
return u
}
}
Expand Down Expand Up @@ -121,21 +129,17 @@ func WithValidateResponse(validationFunc func(response *http.Response) error) En
}
}

func WithURLBuilder(opts ...URLBuilderOption) EndpointOption {
builder := NewURLBuilder(opts)
func WithURLBuilder(opts ...url_builder.URLBuilderOption) EndpointOption {
builder := url_builder.NewURLBuilder(opts)
return func(ep *endpoint) {
ep.urlFunc = builder.Build
}
}

func WithRawURL(rawURL string) URLBuilderOption {
return func(builder *URLBuilder) {
builder.rawURL = rawURL
ep.urlFunc = func() *url.URL {
return builder.Build(basePath)
}
}
}

func WithQueryParams(queryParams ...query_param.Builder) URLBuilderOption {
return func(builder *URLBuilder) {
builder.queryParams = append(builder.queryParams, queryParams...)
func WithBasePath(path string) goload.LoadTestOption {
return func(_ *goload.LoadTestOptions) {
basePath = &path
}
}
22 changes: 0 additions & 22 deletions http/query_param/usage_opts.go

This file was deleted.

19 changes: 19 additions & 0 deletions http/url_builder/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package url_builder

func WithRawURL(rawURL string) URLBuilderOption {
return func(builder *URLBuilder) {
builder.rawURL = rawURL
}
}

func WithQueryParams(queryParams ...QueryParamBuilder) URLBuilderOption {
return func(builder *URLBuilder) {
builder.queryParams = append(builder.queryParams, queryParams...)
}
}

func WithURLParam(key string, values []string) URLBuilderOption {
return func(builder *URLBuilder) {
builder.urlParameterRandomizers = append(builder.urlParameterRandomizers, URLParameterRandomizer{key, values})
}
}
54 changes: 23 additions & 31 deletions http/query_param/parameter.go → http/url_builder/query_param.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package query_param
package url_builder

import (
"github.com/HenriBeck/goload/utils/random"
"github.com/mroth/weightedrand/v2"
"net/url"
)

type Builder interface {
type QueryParamBuilder interface {
Build() url.Values
}

Expand All @@ -20,9 +20,9 @@ type QueryParameter struct {
Value ValuesFn
}

type Opt func(param *QueryParameter)
type QueryParameterOption func(param *QueryParameter)

func New(opts ...Opt) *QueryParameter {
func NewQueryParameter(opts ...QueryParameterOption) *QueryParameter {
param := &QueryParameter{
//set defaults
ShouldBeUsed: UseAlways(),
Expand All @@ -31,7 +31,7 @@ func New(opts ...Opt) *QueryParameter {
opt(param)
}
if param.Name == nil || param.Value == nil {
panic("query_param.New must contain opts for name and value")
panic("query_param.NewQueryParameter must contain opts for name and value")
}
return param
}
Expand All @@ -53,21 +53,29 @@ func UseAlways() ShouldBeUsedFn {
}
}

func WithName(name string) Opt {
return func(param *QueryParameter) {
param.Name = func() string {
return name
}
type oneOfParam struct {
params []QueryParamBuilder
}

func WithOneOfParam(params ...QueryParamBuilder) QueryParamBuilder {
if len(params) == 0 {
panic("NewOneOfParam must contain at least one parameter")
}
return &oneOfParam{params: params}
}

type pctParam struct {
func (p *oneOfParam) Build() url.Values {
index := random.Number(0, int64(len(p.params)-1))
return p.params[index].Build()
}

type chanceParam struct {
chance int
param Builder
param QueryParamBuilder
r *weightedrand.Chooser[bool, int]
}

func NewPctParam(chance int, param Builder) Builder {
func NewParamWithUsageChange(chance int, param QueryParamBuilder) QueryParamBuilder {
if chance > 100 || chance < 0 {
panic("chance value must be between 0 and 100")
}
Expand All @@ -79,28 +87,12 @@ func NewPctParam(chance int, param Builder) Builder {
panic(err)
}

return &pctParam{chance: chance, param: param, r: r}
return &chanceParam{chance: chance, param: param, r: r}
}

func (p *pctParam) Build() url.Values {
func (p *chanceParam) Build() url.Values {
if p.r.Pick() {
return p.param.Build()
}
return url.Values{}
}

type oneOfParam struct {
params []Builder
}

func (o *oneOfParam) Build() url.Values {
index := random.Number(0, int64(len(o.params)-1))
return o.params[index].Build()
}

func NewOneOfParam(params ...Builder) Builder {
if len(params) == 0 {
panic("params cant be empty")
}
return &oneOfParam{params: params}
}
Loading

0 comments on commit 3df116e

Please sign in to comment.