forked from tsenart/vegeta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (77 loc) · 2.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"strings"
)
func main() {
commands := map[string]command{
"attack": attackCmd(),
"report": reportCmd(),
"dump": dumpCmd(),
}
fs := flag.NewFlagSet("vegeta", flag.ExitOnError)
cpus := fs.Int("cpus", runtime.NumCPU(), "Number of CPUs to use")
profile := fs.String("profile", "", "Enable profiling of [cpu, heap]")
version := fs.Bool("version", false, "Print version and exit")
fs.Usage = func() {
fmt.Println("Usage: vegeta [global flags] <command> [command flags]")
fmt.Printf("\nglobal flags:\n")
fs.PrintDefaults()
for name, cmd := range commands {
fmt.Printf("\n%s command:\n", name)
cmd.fs.PrintDefaults()
}
fmt.Println(examples)
}
fs.Parse(os.Args[1:])
if *version {
fmt.Printf("Version: %s\nCommit: %s\nGo version: %s\nDate: %s\n", version, commit, goversion, date)
return
}
runtime.GOMAXPROCS(*cpus)
for _, prof := range strings.Split(*profile, ",") {
if prof = strings.TrimSpace(prof); prof == "" {
continue
}
f, err := os.Create(prof + ".pprof")
if err != nil {
log.Fatal(err)
}
defer f.Close()
switch {
case strings.HasPrefix(prof, "cpu"):
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
case strings.HasPrefix(prof, "heap"):
defer pprof.Lookup("heap").WriteTo(f, 0)
}
}
args := fs.Args()
if len(args) == 0 {
fs.Usage()
os.Exit(1)
}
if cmd, ok := commands[args[0]]; !ok {
log.Fatalf("Unknown command: %s", args[0])
} else if err := cmd.fn(args[1:]); err != nil {
log.Fatal(err)
}
}
var version, commit, date, goversion string
const examples = `
examples:
echo "GET http://localhost/" | vegeta attack -duration=5s | tee results.bin | vegeta report
vegeta attack -targets=targets.txt > results.bin
vegeta report -inputs=results.bin -reporter=json > metrics.json
cat results.bin | vegeta report -reporter=plot > plot.html
cat results.bin | vegeta report -reporter="hist[0,100ms,200ms,300ms]"
`
type command struct {
fs *flag.FlagSet
fn func(args []string) error
}