Skip to content

Commit

Permalink
bombardier: change internal URL handling and set timeouts for dialing
Browse files Browse the repository at this point in the history
Type of url field in config struct was changed from string to net/url.URL.
The way fasthttpClient is setup was adjusted to resolve the issue mentioned in #105.
Dial function used by fasthttp client was adjusted to respect --timeout
flag, which should fix #103.
  • Loading branch information
codesenberg committed Oct 30, 2023
1 parent 0e03804 commit faae8e1
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 160 deletions.
17 changes: 1 addition & 16 deletions args_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (k *kingpinParser) parse(args []string) (config, error) {
"unknown format or invalid format spec %q", k.formatSpec,
)
}
url, err := tryParseURL(k.url)
url, err := urlx.Parse(k.url)
if err != nil {
return emptyConf, err
}
Expand Down Expand Up @@ -264,18 +264,3 @@ func parsePrintSpec(spec string) (bool, bool, bool, error) {
}
return pi, pp, pr, nil
}

func tryParseURL(raw string) (string, error) {
u, err := urlx.Parse(raw)
if err != nil {
return "", fmt.Errorf("%q does not appear to be a URL: %v", raw, err)
}

if u.Scheme != "http" && u.Scheme != "https" {
return "", fmt.Errorf(
"only http and https schemes are supported, which %q is not, url was %q", u.Scheme, raw,
)
}

return u.String(), nil
}
61 changes: 23 additions & 38 deletions args_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "http://localhost:8080",
url: ParseURLOrPanic("http://localhost:8080"),
printIntro: true,
printProgress: true,
printResult: true,
Expand All @@ -75,7 +75,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://localhost",
url: ParseURLOrPanic("https://localhost"),
printIntro: true,
printProgress: true,
printResult: true,
Expand All @@ -89,7 +89,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -133,7 +133,7 @@ func TestArgsParsing(t *testing.T) {
headers: new(headersList),
method: "GET",
numReqs: &defaultNumberOfReqs,
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand All @@ -159,7 +159,7 @@ func TestArgsParsing(t *testing.T) {
headers: new(headersList),
printLatencies: true,
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand All @@ -185,7 +185,7 @@ func TestArgsParsing(t *testing.T) {
headers: new(headersList),
insecure: true,
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestArgsParsing(t *testing.T) {
method: "GET",
keyPath: "testclient.key",
certPath: "testclient.cert",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -254,7 +254,7 @@ func TestArgsParsing(t *testing.T) {
headers: new(headersList),
method: "POST",
body: "reqbody",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestArgsParsing(t *testing.T) {
{"Two", "Value two"},
},
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -325,7 +325,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
rate: &ten,
printIntro: true,
printProgress: true,
Expand All @@ -350,7 +350,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
clientType: fhttp,
printIntro: true,
printProgress: true,
Expand All @@ -371,7 +371,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
clientType: nhttp1,
printIntro: true,
printProgress: true,
Expand All @@ -392,7 +392,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
clientType: nhttp2,
printIntro: true,
printProgress: true,
Expand Down Expand Up @@ -424,7 +424,7 @@ func TestArgsParsing(t *testing.T) {
headers: new(headersList),
method: "GET",
bodyFilePath: "testbody.txt",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand All @@ -450,7 +450,7 @@ func TestArgsParsing(t *testing.T) {
headers: new(headersList),
method: "GET",
stream: true,
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand All @@ -469,7 +469,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -514,7 +514,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -559,7 +559,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: false,
printResult: true,
Expand All @@ -584,7 +584,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: false,
printProgress: false,
printResult: false,
Expand Down Expand Up @@ -629,7 +629,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -674,7 +674,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -704,7 +704,7 @@ func TestArgsParsing(t *testing.T) {
timeout: defaultTimeout,
headers: new(headersList),
method: "GET",
url: "https://somehost.somedomain",
url: ParseURLOrPanic("https://somehost.somedomain"),
printIntro: true,
printProgress: true,
printResult: true,
Expand Down Expand Up @@ -823,29 +823,14 @@ func TestArgsParsingWithInvalidPrintSpec(t *testing.T) {
}
}

func TestTryParseUrl(t *testing.T) {
invalid := []string{
"ftp://bla:89",
"http://bla:bla:bla",
"htp:/bla:bla:bla",
}

for _, url := range invalid {
gotURL, err := tryParseURL(url)
if err == nil {
t.Errorf("%q is not a valid URL, parsed as %q", url, gotURL)
}
}
}

func TestEmbeddedURLParsing(t *testing.T) {
p := newKingpinParser()
url := "http://127.0.0.1:8080/to?url=http://10.100.99.41:38667"
c, err := p.parse([]string{programName, url})
if err != nil {
t.Error(err)
}
if c.url != url {
if c.url.String() != url {
t.Errorf("got %q, wanted %q", c.url, url)
}
}
2 changes: 1 addition & 1 deletion bombardier.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func newBombardier(c config) (*bombardier, error) {
disableKeepAlives: c.disableKeepAlives,

headers: c.headers,
url: c.url,
requestURL: c.url,
method: c.method,
body: pbody,
bodProd: bsp,
Expand Down
6 changes: 4 additions & 2 deletions bombardier_performance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ func BenchmarkBombardierSingleReqPerf(b *testing.B) {
numConns: defaultNumberOfConns,
numReqs: nil,
duration: &longDuration,
url: "http://" + addr,
url: ParseURLOrPanic("http://" + addr),
headers: new(headersList),
timeout: defaultTimeout,
method: "GET",
body: "",
printLatencies: false,
clientType: clientTypeFromString(*clientType),
format: knownFormat("json"),
}, b)
}

Expand All @@ -40,14 +41,15 @@ func BenchmarkBombardierRateLimitPerf(b *testing.B) {
numConns: defaultNumberOfConns,
numReqs: nil,
duration: &longDuration,
url: "http://" + addr,
url: ParseURLOrPanic("http://" + addr),
headers: new(headersList),
timeout: defaultTimeout,
method: "GET",
body: "",
printLatencies: false,
rate: &highRate,
clientType: clientTypeFromString(*clientType),
format: knownFormat("json"),
}, b)
}

Expand Down
Loading

0 comments on commit faae8e1

Please sign in to comment.