-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (84 loc) · 2.14 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
93
94
95
96
97
98
99
100
101
102
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"os/exec"
"strconv"
"strings"
"sync"
"github.com/pkg/errors"
)
type apachBecnhArgs struct {
n int
c int
t int
A string
P string
X string
url string
goRoutineNum int
}
var abargs apachBecnhArgs
func init() {
flag.IntVar(&abargs.n, "n", 0, "requests")
flag.IntVar(&abargs.c, "c", 0, "concurrency")
flag.IntVar(&abargs.t, "t", 0, "timelimit")
flag.StringVar(&abargs.A, "A", "", "Add Basic WWW Authentication, the attribute are a colon separated username and password.")
flag.StringVar(&abargs.P, "P", "", "Add Basic Proxy Authentication, the attributes are a colon separated username and password.")
flag.StringVar(&abargs.X, "X", "", "proxy:port Proxyserver and port number to use")
flag.StringVar(&abargs.url, "url", "", "apache bench url")
flag.IntVar(&abargs.goRoutineNum, "goroutineNum", 0, "goroutine number")
}
func main() {
flag.Parse()
var wg sync.WaitGroup
wg.Add(abargs.goRoutineNum)
for i := 0; i < abargs.goRoutineNum; i++ {
abargs := abargs
go func(abargs apachBecnhArgs) {
out, err := runApachBench(abargs)
if err != nil {
log.Fatal(err)
}
fmt.Println(out)
wg.Done()
}(abargs)
}
wg.Wait()
}
func runApachBench(abargs apachBecnhArgs) (io.Writer, error) {
cmdString := []string{}
if abargs.n > 0 {
cmdString = append(cmdString, "-n", strconv.Itoa(abargs.n))
}
if abargs.c > 0 {
cmdString = append(cmdString, "-c", strconv.Itoa(abargs.c))
}
if abargs.t > 0 {
cmdString = append(cmdString, "-t", strconv.Itoa(abargs.t))
}
if abargs.A != "" {
cmdString = append(cmdString, "-A", abargs.A)
}
if abargs.P != "" {
cmdString = append(cmdString, "-P", abargs.A)
}
if abargs.X != "" {
cmdString = append(cmdString, "-X", abargs.A)
}
if abargs.url == "" {
return nil, errors.New("url cannnot empty")
}
cmd := exec.Command("ab", strings.Join(cmdString, " "), abargs.url)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
return nil, errors.Wrap(err, "cmd.Run")
}
return cmd.Stdout, nil
}