-
Notifications
You must be signed in to change notification settings - Fork 1
/
digits.go
166 lines (142 loc) · 3.69 KB
/
digits.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Command digits is an HTTP load generator.
package main
import (
"flag"
"fmt"
"math"
"os"
"os/signal"
"regexp"
"runtime"
"time"
"github.com/jkassismz/digits/requester"
)
const (
digitsUA = "digits/0.0.1"
)
var (
headers = flag.String("h", "", "")
body = flag.String("d", "", "")
bodyFile = flag.String("D", "", "")
accept = flag.String("A", "", "")
authHeader = flag.String("a", "", "")
hostHeader = flag.String("host", "", "")
output = flag.String("o", "", "")
c = flag.Int("c", 50, "")
n = flag.Int("n", 200, "")
q = flag.Float64("q", 0, "")
t = flag.Int("t", 20, "")
z = flag.Duration("z", 0, "")
cpus = flag.Int("cpus", runtime.GOMAXPROCS(-1), "")
)
var usage = `Usage: digits [options...] <url>
Options:
-n Number of requests to run. Default is 200.
-c Number of workers to run concurrently. Total number of requests cannot
be smaller than the concurrency level. Default is 50.
-q Rate limit, in queries per second (QPS) per worker. Default is no rate limit.
-t Timeout per request
-z Duration of application to send requests. When duration is reached,
application stops and exits. If duration is specified, n is ignored.
Examples: -z 10s -z 3m.
-o Output type. If none provided, a summary is printed.
"csv" is the only supported alternative. Dumps the response
metrics in comma-separated values format.
-cpus Number of used cpu cores.
(default for current machine is %d cores)
`
func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, fmt.Sprintf(usage, runtime.NumCPU()))
}
var hs headerSlice
flag.Var(&hs, "H", "")
flag.Parse()
if flag.NArg() < 1 {
usageAndExit("")
}
runtime.GOMAXPROCS(*cpus)
num := *n
conc := *c
q := *q
dur := *z
if dur > 0 {
num = math.MaxInt32
if conc <= 0 {
usageAndExit("-c cannot be smaller than 1.")
}
} else {
if num <= 0 || conc <= 0 {
usageAndExit("-n and -c cannot be smaller than 1.")
}
if num < conc {
usageAndExit("-n cannot be less than -c.")
}
}
host := flag.Args()[0]
w := &requester.Work{
Host: host,
N: num,
C: conc,
QPS: q,
Timeout: *t,
Output: *output,
}
w.Init()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
w.Stop()
}()
if dur > 0 {
go func() {
time.Sleep(dur)
w.Stop()
}()
}
w.Run()
}
func errAndExit(msg string) {
fmt.Fprintf(os.Stderr, msg)
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}
func usageAndExit(msg string) {
if msg != "" {
fmt.Fprintf(os.Stderr, msg)
fmt.Fprintf(os.Stderr, "\n\n")
}
flag.Usage()
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}
func parseInputWithRegexp(input, regx string) ([]string, error) {
re := regexp.MustCompile(regx)
matches := re.FindStringSubmatch(input)
if len(matches) < 1 {
return nil, fmt.Errorf("could not parse the provided input; input = %v", input)
}
return matches, nil
}
type headerSlice []string
func (h *headerSlice) String() string {
return fmt.Sprintf("%s", *h)
}
func (h *headerSlice) Set(value string) error {
*h = append(*h, value)
return nil
}