mirror of
https://github.com/cubixle/l1.git
synced 2026-04-30 16:38:43 +01:00
updates
This commit is contained in:
@@ -0,0 +1,7 @@
|
|||||||
|
package l1
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNoTarget = fmt.Errorf("no target has been set")
|
||||||
|
)
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,22 +1,41 @@
|
|||||||
package l1
|
package l1
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// F defines the function type for runners.
|
||||||
|
type F func(target string) error
|
||||||
|
|
||||||
// Runner
|
// Runner
|
||||||
type Runner struct {
|
type Runner struct {
|
||||||
MaxConnections int
|
MaxConnections int
|
||||||
Timeout int
|
MaxParrellConnections int
|
||||||
|
Timeout time.Duration
|
||||||
RunTime time.Duration
|
RunTime time.Duration
|
||||||
|
RunFunc F
|
||||||
|
Target string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRunner(opts ...Opt) {
|
func NewRunner(opts ...Opt) (*Runner, error) {
|
||||||
r := &Runner{
|
r := &Runner{
|
||||||
RunTime: 60 * time.Second,
|
RunTime: 60 * time.Second,
|
||||||
|
Timeout: 30 * time.Second,
|
||||||
|
MaxParrellConnections: 10,
|
||||||
|
MaxConnections: 10,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(r)
|
o(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if r.Target == "" {
|
||||||
|
return nil, ErrNoTarget
|
||||||
}
|
}
|
||||||
|
|
||||||
// F defines the function type for runners.
|
return r, nil
|
||||||
type F func(url string) error
|
}
|
||||||
|
|
||||||
|
func (r *Runner) SetOpt(o Opt) {
|
||||||
|
o(r)
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ func WithMaxConns(amount int) Opt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func WithTimeout(s int) Opt {
|
func WithTimeout(timeInSecs int) Opt {
|
||||||
return func(r *Runner) {
|
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
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
@@ -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
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
// }
|
||||||
Reference in New Issue
Block a user