-
Notifications
You must be signed in to change notification settings - Fork 315
/
config.go
175 lines (149 loc) · 3.42 KB
/
config.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
167
168
169
170
171
172
173
174
175
package main
import (
"fmt"
"net/url"
"sort"
"time"
)
type config struct {
numConns uint64
numReqs *uint64
disableKeepAlives bool
duration *time.Duration
url *url.URL
method, certPath, keyPath string
body, bodyFilePath string
stream bool
headers *headersList
timeout time.Duration
// TODO(codesenberg): printLatencies should probably be
// re(named&maked) into printPercentiles or even let
// users provide their own percentiles and not just
// calculate for [0.5, 0.75, 0.9, 0.99]
printLatencies, insecure bool
rate *uint64
clientType clientTyp
printIntro, printProgress, printResult bool
format format
}
type testTyp int
const (
none testTyp = iota
timed
counted
)
type invalidHTTPMethodError struct {
method string
}
func (i *invalidHTTPMethodError) Error() string {
return fmt.Sprintf("Unknown HTTP method: %v", i.method)
}
func (c *config) checkArgs() error {
c.checkOrSetDefaultTestType()
checks := []func() error{
c.checkURL,
c.checkRate,
c.checkRunParameters,
c.checkTimeoutDuration,
c.checkHTTPParameters,
c.checkCertPaths,
}
for _, check := range checks {
if err := check(); err != nil {
return err
}
}
return nil
}
func (c *config) checkOrSetDefaultTestType() {
if c.testType() == none {
c.duration = &defaultTestDuration
}
}
func (c *config) testType() testTyp {
typ := none
if c.numReqs != nil {
typ = counted
} else if c.duration != nil {
typ = timed
}
return typ
}
func (c *config) checkURL() error {
if c.url.Scheme != "http" && c.url.Scheme != "https" {
return errUnsupportedScheme
}
return nil
}
func (c *config) checkRate() error {
if c.rate != nil && *c.rate < 1 {
return errZeroRate
}
return nil
}
func (c *config) checkRunParameters() error {
if c.numConns < uint64(1) {
return errInvalidNumberOfConns
}
if c.testType() == counted && *c.numReqs < uint64(1) {
return errInvalidNumberOfRequests
}
if c.testType() == timed && *c.duration < time.Second {
return errInvalidTestDuration
}
return nil
}
func (c *config) checkTimeoutDuration() error {
if c.timeout < 0 {
return errNegativeTimeout
}
return nil
}
func (c *config) checkHTTPParameters() error {
if !allowedHTTPMethod(c.method) {
return &invalidHTTPMethodError{method: c.method}
}
if !canHaveBody(c.method) && (c.body != "" || c.bodyFilePath != "") {
return errBodyNotAllowed
}
if c.body != "" && c.bodyFilePath != "" {
return errBodyProvidedTwice
}
return nil
}
func (c *config) checkCertPaths() error {
if c.certPath != "" && c.keyPath == "" {
return errNoPathToKey
} else if c.certPath == "" && c.keyPath != "" {
return errNoPathToCert
}
return nil
}
func (c *config) timeoutMillis() uint64 {
return uint64(c.timeout.Nanoseconds() / 1000)
}
func allowedHTTPMethod(method string) bool {
i := sort.SearchStrings(httpMethods, method)
return i < len(httpMethods) && httpMethods[i] == method
}
func canHaveBody(method string) bool {
i := sort.SearchStrings(cantHaveBody, method)
return !(i < len(cantHaveBody) && cantHaveBody[i] == method)
}
type clientTyp int
const (
fhttp clientTyp = iota
nhttp1
nhttp2
)
func (ct clientTyp) String() string {
switch ct {
case fhttp:
return "FastHTTP"
case nhttp1:
return "net/http v1.x"
case nhttp2:
return "net/http v2.0"
}
return "unknown client"
}