HTTP load generator, ApacheBench (ab) replacement
Go
Handling IPv6 and TLS handshake after IP resolve. Some cleanup.
Burcu Dogan232d50ef5b32
diff --git a/commands/boom.go b/commands/boom.goindex 44d60d3..5db4982 100644--- a/commands/boom.go+++ b/commands/boom.go@@ -31,6 +31,9 @@ type result struct {type Boom struct {// Request to make.Req *http.Request+ // Req.Host is an resolved IP. TLS/SSL handshakes may require+ // the original server name, keep it to initate the TLS client.+ OrigServerName string// Total number of requests to make.N int// Concurrency level, the number of concurrent workers to run.diff --git a/commands/run.go b/commands/run.goindex 47fec6e..c461343 100644--- a/commands/run.go+++ b/commands/run.go@@ -32,7 +32,7 @@ func (b *Boom) Run() {func (b *Boom) worker(ch chan bool) {tr := &http.Transport{- TLSClientConfig: &tls.Config{InsecureSkipVerify: b.AllowInsecure, ServerName: b.Req.Host},+ TLSClientConfig: &tls.Config{InsecureSkipVerify: b.AllowInsecure, ServerName: b.OrigServerName},}client := &http.Client{Transport: tr}for _ = range ch {diff --git a/main.go b/main.goindex 55fc788..0a90fb8 100644--- a/main.go+++ b/main.go@@ -90,13 +90,9 @@ func main() {url := flag.Args()[0]method := strings.ToUpper(*flagMethod)- uri, err := gourl.ParseRequestURI(url)- if err != nil {- usageAndExit(err.Error())- }-- req := newReq(method, uri, flagD)-+ ipResolvedUrl, serverName := newURL(url)+ req, _ := http.NewRequest(+ method, ipResolvedUrl.String(), strings.NewReader(*flagD))// set content-typereq.Header.Set("Content-Type", *flagType)@@ -128,37 +124,54 @@ func main() {}(&commands.Boom{- N: n,- C: c,- Qps: q,- Timeout: t,- Req: req,- AllowInsecure: *flagInsecure,- Output: *flagOutput}).Run()+ N: n,+ C: c,+ Qps: q,+ Timeout: t,+ Req: req,+ OrigServerName: serverName,+ AllowInsecure: *flagInsecure,+ Output: *flagOutput}).Run()}-func newReq(method string, uri *gourl.URL, flagD *string) *http.Request {- servername, port, err := net.SplitHostPort(uri.Host)+// Replaces host with an IP and returns the provided+// string URL as a *url.URL.+//+// DNS lookups are not cached in the package level in Go,+// and it's a huge overhead to resolve a host+// before each request in our case. Instead we resolve+// the domain and replace it with the resolved IP to avoid+// lookups during request time. Supported url strings:+//+// <schema>://google.com[:port]+// <schema>://173.194.116.73[:port]+// <schema>://\[2a00:1450:400a:806::1007\][:port]+func newURL(url string) (*gourl.URL, string) {+ uri, err := gourl.ParseRequestURI(url)if err != nil {- servername = uri.Host- port = ""+ usageAndExit(err.Error())}- addrs, err := net.LookupHost(servername)+ serverName, port, err := net.SplitHostPort(uri.Host)if err != nil {- usageAndExit("Hostname " + uri.Host + " is invalid")+ serverName = uri.Host+ }++ addrs, err := net.LookupHost(serverName)+ if err != nil {+ usageAndExit("Hostname " + serverName + " cannot be resolved")}if port != "" {+ // join automatically puts square brackets around the+ // ipv6 IPs.uri.Host = net.JoinHostPort(addrs[0], port)} else {- uri.Host = addrs[0]+ // square brackets are required for ipv6 IPs.+ // otherwise, net.Dial fails with a parsing error.+ uri.Host = fmt.Sprintf("[%s]", addrs[0])}- req, _ := http.NewRequest(method, uri.String(), strings.NewReader(*flagD))-- req.Host = servername-- return req+ return uri, serverName}func usageAndExit(message string) {