Split Attack and Report into sub commands
This commit is contained in:
114
main.go
114
main.go
@@ -2,105 +2,49 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
vegeta "github.com/tsenart/vegeta/lib"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
rate = flag.Uint64("rate", 50, "Requests per second")
|
||||
targetsf = flag.String("targets", "targets.txt", "Targets file")
|
||||
ordering = flag.String("ordering", "random", "Attack ordering [sequential, random]")
|
||||
duration = flag.Duration("duration", 10*time.Second, "Duration of the test")
|
||||
reporter = flag.String("reporter", "text", "Reporter to use [text, json, plot:timings]")
|
||||
output = flag.String("output", "stdout", "Reporter output file")
|
||||
cpus = flag.Int("cpus", runtime.NumCPU(), "Number of CPUs to use")
|
||||
)
|
||||
// Global flags
|
||||
cpus := flag.Int("cpus", runtime.NumCPU(), "Number of CPUs to use")
|
||||
flag.Parse()
|
||||
|
||||
if flag.NFlag() == 0 {
|
||||
flag.Usage()
|
||||
return
|
||||
}
|
||||
|
||||
runtime.GOMAXPROCS(*cpus)
|
||||
|
||||
if err := run(*rate, *duration, *targetsf, *ordering, *reporter, *output); err != nil {
|
||||
log.Fatal(err)
|
||||
args := flag.Args()
|
||||
if len(args) < 1 {
|
||||
log.Fatal("Unspecified command")
|
||||
}
|
||||
}
|
||||
cmd, cmdf := args[0], flag.NewFlagSet(args[0], flag.ExitOnError)
|
||||
|
||||
const (
|
||||
errRatePrefix = "Rate: "
|
||||
errDurationPrefix = "Duration: "
|
||||
errOutputFilePrefix = "Output file: "
|
||||
errTargetsFilePrefix = "Targets file: "
|
||||
errOrderingPrefix = "Ordering: "
|
||||
errReportingPrefix = "Reporting: "
|
||||
)
|
||||
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")
|
||||
|
||||
// run is an utility function that validates the attack arguments, sets up the
|
||||
// required resources, launches the attack and reports the results
|
||||
func run(rate uint64, duration time.Duration, targetsf, ordering, reporter, output string) error {
|
||||
if rate == 0 {
|
||||
return fmt.Errorf(errRatePrefix + "can't be zero")
|
||||
}
|
||||
|
||||
if duration == 0 {
|
||||
return fmt.Errorf(errDurationPrefix + "can't be zero")
|
||||
}
|
||||
|
||||
var out io.Writer
|
||||
switch output {
|
||||
case "stdout":
|
||||
out = os.Stdout
|
||||
default:
|
||||
file, err := os.Create(output)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errOutputFilePrefix+"(%s): %s", output, err)
|
||||
if err := cmdf.Parse(args[1:]); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
out = file
|
||||
}
|
||||
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")
|
||||
|
||||
var rep vegeta.Reporter
|
||||
switch reporter {
|
||||
case "text":
|
||||
rep = vegeta.ReportText
|
||||
case "json":
|
||||
rep = vegeta.ReportJSON
|
||||
case "plot:timings":
|
||||
rep = vegeta.ReportTimingsPlot
|
||||
if err := cmdf.Parse(args[1:]); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err := report(*reporter, *input, *output); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
default:
|
||||
log.Println("Reporter provided is not supported. Using text")
|
||||
rep = vegeta.ReportText
|
||||
log.Fatalf("Unknown command: %s", cmd)
|
||||
}
|
||||
|
||||
targets, err := vegeta.NewTargetsFromFile(targetsf)
|
||||
if err != nil {
|
||||
return fmt.Errorf(errTargetsFilePrefix+"(%s): %s", targetsf, err)
|
||||
}
|
||||
|
||||
switch ordering {
|
||||
case "random":
|
||||
targets.Shuffle(time.Now().UnixNano())
|
||||
case "sequential":
|
||||
break
|
||||
default:
|
||||
return fmt.Errorf(errOrderingPrefix+"`%s` is invalid", ordering)
|
||||
}
|
||||
|
||||
log.Printf("Vegeta is attacking %d targets in %s order for %s...\n", len(targets), ordering, duration)
|
||||
results := vegeta.Attack(targets, rate, duration)
|
||||
log.Println("Done!")
|
||||
log.Printf("Writing report to '%s'...", output)
|
||||
if err = rep(results, out); err != nil {
|
||||
return fmt.Errorf(errReportingPrefix+"%s", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user