Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command line interface in draft - 1 #30

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

evundler is a performance testing framework written in Go.


## Installation
```
go get -u github.com/github.com/go-loadtest/evbundler/cmd/evbundler
```

## Usage
`t.b.d`
13 changes: 13 additions & 0 deletions cmd/evbundler/internal/base/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package base

import "flag"

type Command struct {
Name string
Doc string
Run func(args []string) error
Flags *flag.FlagSet
}

// Commands is register subcommands
var Commands []*Command
50 changes: 50 additions & 0 deletions cmd/evbundler/internal/ping/ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package ping

import (
"context"
"flag"
"time"

"github.com/go-loadtest/evbundler"
"github.com/go-loadtest/evbundler/cmd/evbundler/internal/base"
"github.com/go-loadtest/evbundler/dispatcher"
"github.com/go-loadtest/evbundler/event"
)

const name = "ping"

var Cmd = &base.Command{
Name: name,
Doc: "icmpv4 event generator",
Run: run,
}

var target string
var interval string

func run(args []string) error {
flags := flag.NewFlagSet(name, flag.ContinueOnError)
flags.StringVar(&target, "target", "", `send to host or ip`)
flags.StringVar(&interval, "interval", "1s", `interval of send each packets e.g. 1s, 300ms, 1m`)

if err := flags.Parse(args); err != nil {
return err
}

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

d, err := time.ParseDuration(interval)
if err != nil {
return err
}
evCh := evbundler.TickerProducer(ctx, d, func(t time.Time, evCh chan evbundler.Event) {
evCh <- event.NewICMPv4Event(1, "0.0.0.0", target)
})

wp := evbundler.NewWorkerPool(10, nil)
disp := dispatcher.NewGoChannel(wp)
_ = disp.Dispatch(ctx, evCh)

return nil
}
46 changes: 46 additions & 0 deletions cmd/evbundler/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/go-loadtest/evbundler/cmd/evbundler/internal/base"
"github.com/go-loadtest/evbundler/cmd/evbundler/internal/ping"
)

type Commands []*base.Command

func (cs Commands) Usage() {
usage := `
Usage of evbundler:
evbundler <command> [arguments]

The commands are:

`
fmt.Fprint(os.Stderr, usage)
for i := range cs {
fmt.Fprintf(os.Stderr, "\t\t%s\t%s\n", cs[i].Name, cs[i].Doc)
}
os.Exit(2)
}

func main() {
commands := Commands{
ping.Cmd,
}

flag.Usage = commands.Usage
flag.Parse()
args := flag.Args()
if len(args) < 1 {
commands.Usage()
}

for _, c := range commands {
if c.Name == args[0] {
c.Run(args[1:])
}
}
}
2 changes: 1 addition & 1 deletion event/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type HTTPEvent struct {

// Name is Event name each event types
func (he HTTPEvent) Name() string {
return "HTTP"
return "http"
}

// Fire execute a Event by parameters
Expand Down
2 changes: 1 addition & 1 deletion event/icmpv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewICMPv4Event(seq int, listenAddr, addr string) *ICMPv4Event {
}

func (e ICMPv4Event) Name() string {
return "ICMPv4"
return "ping"
}

func (e ICMPv4Event) Fire(ctx context.Context) error {
Expand Down