From 2ddeeea267b733b58af8e25dbadea6e316b881ee Mon Sep 17 00:00:00 2001 From: cubixle Date: Sun, 25 Jul 2021 20:52:56 +0100 Subject: [PATCH] updates --- errors.go | 7 +++++++ examples/simple/main.go | 23 +++++++++++++++++++++++ l1.go | 35 +++++++++++++++++++++++++++-------- opts.go | 22 ++++++++++++++++++++-- pool.go | 21 +++++++++++++++++++++ results.go | 26 ++++++++++++++++++++++++++ results_test.go | 16 ++++++++++++++++ 7 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 errors.go create mode 100644 pool.go create mode 100644 results.go create mode 100644 results_test.go diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..5d0af69 --- /dev/null +++ b/errors.go @@ -0,0 +1,7 @@ +package l1 + +import "fmt" + +var ( + ErrNoTarget = fmt.Errorf("no target has been set") +) diff --git a/examples/simple/main.go b/examples/simple/main.go index e69de29..8f8a929 100644 --- a/examples/simple/main.go +++ b/examples/simple/main.go @@ -0,0 +1,23 @@ +package main + +import ( + "log" + + "github.com/cubixle/l1" +) + +func main() { + r, err := l1.NewRunner( + l1.WithTarget("http://google.com"), + l1.WithRunFunc(func(target string) error { + return nil + }), + ) + if err != nil { + log.Fatal(err) + } + err = r.Start() + if err != nil { + log.Fatal(err) + } +} diff --git a/l1.go b/l1.go index 538f08f..ce1dd5a 100644 --- a/l1.go +++ b/l1.go @@ -1,22 +1,41 @@ package l1 -import "time" +import ( + "time" +) + +// F defines the function type for runners. +type F func(target string) error // Runner type Runner struct { - MaxConnections int - Timeout int - RunTime time.Duration + MaxConnections int + MaxParrellConnections int + Timeout time.Duration + RunTime time.Duration + RunFunc F + Target string } -func NewRunner(opts ...Opt) { +func NewRunner(opts ...Opt) (*Runner, error) { r := &Runner{ - RunTime: 60 * time.Second, + RunTime: 60 * time.Second, + Timeout: 30 * time.Second, + MaxParrellConnections: 10, + MaxConnections: 10, } + for _, o := range opts { o(r) } + + if r.Target == "" { + return nil, ErrNoTarget + } + + return r, nil } -// F defines the function type for runners. -type F func(url string) error +func (r *Runner) SetOpt(o Opt) { + o(r) +} diff --git a/opts.go b/opts.go index 0e42641..18d9750 100644 --- a/opts.go +++ b/opts.go @@ -11,9 +11,9 @@ func WithMaxConns(amount int) Opt { } } -func WithTimeout(s int) Opt { +func WithTimeout(timeInSecs int) Opt { return func(r *Runner) { - r.Timeout = s + r.Timeout = time.Duration(timeInSecs) * time.Second } } @@ -22,3 +22,21 @@ func WithRunTime(timeInSecs int) Opt { r.RunTime = time.Duration(timeInSecs) * time.Second } } + +func WithMaxParrellConns(amount int) Opt { + return func(r *Runner) { + r.MaxParrellConnections = amount + } +} + +func WithTarget(target string) Opt { + return func(r *Runner) { + r.Target = target + } +} + +func WithRunFunc(f F) Opt { + return func(r *Runner) { + r.RunFunc = f + } +} diff --git a/pool.go b/pool.go new file mode 100644 index 0000000..7e73835 --- /dev/null +++ b/pool.go @@ -0,0 +1,21 @@ +package l1 + +import ( + "fmt" +) + +// Start starts the runner. +func (r *Runner) Start() error { + jobChan := make(chan string, r.MaxParrellConnections) + // resultsChan := make(chan struct{}) + + for i := 0; i < r.MaxParrellConnections; i++ { + go func(jobChan chan string) { + for t := range jobChan { + r.RunFunc(t) + } + }(jobChan) + } + + return fmt.Errorf("unimplemented") +} diff --git a/results.go b/results.go new file mode 100644 index 0000000..f9a4efc --- /dev/null +++ b/results.go @@ -0,0 +1,26 @@ +package l1 + +type Results struct { + Target string + Count int + Results []Result +} + +type Result struct { + // CompletedIn is in seconds + CompletedIn int + Error error + StatusCode int +} + +func (r *Results) RequestsPerMin() int { + totalCompletedIn := 0 + for _, res := range r.Results { + totalCompletedIn += res.CompletedIn + } + return totalCompletedIn / r.Count +} + +func (r *Results) AvgCompletionTime() int { + return 0 +} diff --git a/results_test.go b/results_test.go new file mode 100644 index 0000000..49eb929 --- /dev/null +++ b/results_test.go @@ -0,0 +1,16 @@ +package l1_test + +// func TestRequestsPerMin(t *testing.T) { +// rs := l1.Results{ +// Count: 4, +// Results: []l1.Result{ +// {CompletedIn: 10}, +// {CompletedIn: 10}, +// {CompletedIn: 10}, +// {CompletedIn: 10}, +// }, +// } + +// rpm := rs.RequestsPerMin() +// log.Println(rpm) +// }