Abstract command building and setup with closures
This commit is contained in:
35
attack.go
35
attack.go
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
vegeta "github.com/tsenart/vegeta/lib"
|
vegeta "github.com/tsenart/vegeta/lib"
|
||||||
"io"
|
"io"
|
||||||
@@ -9,17 +10,22 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
func attackCmd(args []string) command {
|
||||||
errRatePrefix = "Rate: "
|
fs := flag.NewFlagSet("attack", flag.ExitOnError)
|
||||||
errDurationPrefix = "Duration: "
|
rate := fs.Uint64("rate", 50, "Requests per second")
|
||||||
errOutputFilePrefix = "Output file: "
|
targetsf := fs.String("targets", "targets.txt", "Targets file")
|
||||||
errTargetsFilePrefix = "Targets file: "
|
ordering := fs.String("ordering", "random", "Attack ordering [sequential, random]")
|
||||||
errOrderingPrefix = "Ordering: "
|
duration := fs.Duration("duration", 10*time.Second, "Duration of the test")
|
||||||
errReportingPrefix = "Reporting: "
|
output := fs.String("output", "stdout", "Vegeta Results file")
|
||||||
)
|
fs.Parse(args)
|
||||||
|
|
||||||
// attack is the command function that validates the attack arguments, sets up the
|
return func() error {
|
||||||
// required resources, launches the attack and reports the results
|
return attack(*rate, *duration, *targetsf, *ordering, *output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// attack validates the attack arguments, sets up the
|
||||||
|
// required resources, launches the attack and writes the results
|
||||||
func attack(rate uint64, duration time.Duration, targetsf, ordering, output string) error {
|
func attack(rate uint64, duration time.Duration, targetsf, ordering, output string) error {
|
||||||
if rate == 0 {
|
if rate == 0 {
|
||||||
return fmt.Errorf(errRatePrefix + "can't be zero")
|
return fmt.Errorf(errRatePrefix + "can't be zero")
|
||||||
@@ -65,3 +71,12 @@ func attack(rate uint64, duration time.Duration, targetsf, ordering, output stri
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
errRatePrefix = "Rate: "
|
||||||
|
errDurationPrefix = "Duration: "
|
||||||
|
errOutputFilePrefix = "Output file: "
|
||||||
|
errTargetsFilePrefix = "Targets file: "
|
||||||
|
errOrderingPrefix = "Ordering: "
|
||||||
|
errReportingPrefix = "Reporting: "
|
||||||
|
)
|
||||||
|
|||||||
48
main.go
48
main.go
@@ -4,47 +4,27 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// command is a closure function which each command constructor
|
||||||
|
// builds and returns
|
||||||
|
type command func() error
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
commands := map[string]func([]string) command{
|
||||||
|
"attack": attackCmd,
|
||||||
|
"report": reportCmd,
|
||||||
|
}
|
||||||
// Global flags
|
// Global flags
|
||||||
cpus := flag.Int("cpus", runtime.NumCPU(), "Number of CPUs to use")
|
cpus := flag.Int("cpus", runtime.NumCPU(), "Number of CPUs to use")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
args := flag.Args()
|
||||||
|
|
||||||
runtime.GOMAXPROCS(*cpus)
|
runtime.GOMAXPROCS(*cpus)
|
||||||
|
|
||||||
args := flag.Args()
|
if cmd, ok := commands[args[0]]; !ok {
|
||||||
if len(args) < 1 {
|
log.Fatalf("Unknown command: %s", args[0])
|
||||||
log.Fatal("Unspecified command")
|
} else if err := cmd(args[1:])(); err != nil {
|
||||||
}
|
log.Fatal(err)
|
||||||
cmd, cmdf := args[0], flag.NewFlagSet(args[0], flag.ExitOnError)
|
|
||||||
|
|
||||||
switch cmd {
|
|
||||||
case "attack":
|
|
||||||
rate := cmdf.Uint64("rate", 50, "Requests per second")
|
|
||||||
targetsf := cmdf.String("targets", "targets.txt", "Targets file")
|
|
||||||
ordering := cmdf.String("ordering", "random", "Attack ordering [sequential, random]")
|
|
||||||
duration := cmdf.Duration("duration", 10*time.Second, "Duration of the test")
|
|
||||||
output := cmdf.String("output", "stdout", "Vegeta Results file")
|
|
||||||
|
|
||||||
if err := cmdf.Parse(args[1:]); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := attack(*rate, *duration, *targetsf, *ordering, *output); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
case "report":
|
|
||||||
reporter := cmdf.String("reporter", "text", "Reporter [text, json, plot:timings]")
|
|
||||||
input := cmdf.String("input", "stdin", "Vegeta Results file")
|
|
||||||
output := cmdf.String("output", "stdout", "Output file")
|
|
||||||
|
|
||||||
if err := cmdf.Parse(args[1:]); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
if err := report(*reporter, *input, *output); err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
log.Fatalf("Unknown command: %s", cmd)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
15
report.go
15
report.go
@@ -1,12 +1,27 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
vegeta "github.com/tsenart/vegeta/lib"
|
vegeta "github.com/tsenart/vegeta/lib"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func reportCmd(args []string) command {
|
||||||
|
fs := flag.NewFlagSet("report", flag.ExitOnError)
|
||||||
|
reporter := fs.String("reporter", "text", "Reporter [text, json, plot:timings]")
|
||||||
|
input := fs.String("input", "stdin", "Vegeta Results file")
|
||||||
|
output := fs.String("output", "stdout", "Output file")
|
||||||
|
fs.Parse(args)
|
||||||
|
|
||||||
|
return func() error {
|
||||||
|
return report(*reporter, *input, *output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// report validates the report arguments, sets up the required resources
|
||||||
|
// and writes the report
|
||||||
func report(reporter, input, output string) error {
|
func report(reporter, input, output string) error {
|
||||||
var rep vegeta.Reporter
|
var rep vegeta.Reporter
|
||||||
switch reporter {
|
switch reporter {
|
||||||
|
|||||||
Reference in New Issue
Block a user