This commit is contained in:
cubixle
2021-07-25 20:52:56 +01:00
parent 645ff9d698
commit 2ddeeea267
7 changed files with 140 additions and 10 deletions

7
errors.go Normal file
View File

@@ -0,0 +1,7 @@
package l1
import "fmt"
var (
ErrNoTarget = fmt.Errorf("no target has been set")
)

View File

@@ -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)
}
}

35
l1.go
View File

@@ -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)
}

22
opts.go
View File

@@ -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
}
}

21
pool.go Normal file
View File

@@ -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")
}

26
results.go Normal file
View File

@@ -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
}

16
results_test.go Normal file
View File

@@ -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)
// }