Skip to content

armgit / hey

Public Imported from GitHub
0

HTTP load generator, ApacheBench (ab) replacement

Go

Remove manual DNS resolving.

Burcu Dogan69e352b8ec5e
diff --git a/boom.go b/boom.go
index 27c7108..203f3ef 100644
--- a/boom.go
+++ b/boom.go
@@ -120,15 +120,15 @@ func main() {
}
var (
- url, method, originalHost string
+ url, method string
// Username and password for basic auth
username, password string
// request headers
header http.Header = make(http.Header)
)
+ url = flag.Args()[0]
method = strings.ToUpper(*m)
- url, originalHost = resolveUrl(flag.Args()[0])
// set content-type
header.Set("Content-Type", *contentType)
@@ -172,13 +172,12 @@ func main() {
(&boomer.Boomer{
Req: &boomer.ReqOpts{
- Method: method,
- URL: url,
- Body: *body,
- Header: header,
- Username: username,
- Password: password,
- OriginalHost: originalHost,
+ Method: method,
+ URL: url,
+ Body: *body,
+ Header: header,
+ Username: username,
+ Password: password,
},
N: num,
C: conc,
@@ -192,50 +191,6 @@ func main() {
}).Run()
}
-// 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 resolveUrl(url string) (string, string) {
- uri, err := gourl.ParseRequestURI(url)
- if err != nil {
- usageAndExit(err.Error())
- }
- originalHost := uri.Host
-
- serverName, port, err := net.SplitHostPort(uri.Host)
- if err != nil {
- serverName = uri.Host
- }
-
- addrs, err := defaultDNSResolver.Lookup(serverName)
- if err != nil {
- usageAndExit(err.Error())
- }
- ip := addrs[0]
- if port != "" {
- // join automatically puts square brackets around the
- // ipv6 IPs.
- uri.Host = net.JoinHostPort(ip, port)
- } else {
- uri.Host = ip
- // square brackets are required for ipv6 IPs.
- // otherwise, net.Dial fails with a parsing error.
- if strings.Contains(ip, ":") {
- uri.Host = fmt.Sprintf("[%s]", ip)
- }
- }
- return uri.String(), originalHost
-}
-
func usageAndExit(message string) {
if message != "" {
fmt.Fprintf(os.Stderr, message)
diff --git a/boom_test.go b/boom_test.go
index ea44bb1..d025e05 100644
--- a/boom_test.go
+++ b/boom_test.go
@@ -18,66 +18,6 @@ import (
"testing"
)
-type mockDnsResolver struct {
- Addr string
-}
-
-func (r *mockDnsResolver) Lookup(host string) ([]string, error) {
- return []string{r.Addr}, nil
-}
-
-func TestParseURL(t *testing.T) {
- defaultDNSResolver = &mockDnsResolver{Addr: "2a00:1450:400a:806::1007"}
- u, _ := resolveUrl("http://google.com:80/path/to/resource?q=rawquery")
- if u != "http://[2a00:1450:400a:806::1007]:80/path/to/resource?q=rawquery" {
- t.Errorf("Problem during url parsing, %v is found.", u)
- }
-}
-
-func TestParseUrl_IPv4(t *testing.T) {
- defaultDNSResolver = &mockDnsResolver{Addr: "127.0.0.1"}
- u, s := resolveUrl("http://google.com")
- if s != "google.com" {
- t.Errorf("Original server name doesn't match with google.com, %v is found.", s)
- }
- if u != "http://127.0.0.1" {
- t.Errorf("URL is expected to be http://127.0.0.1, %v is found.", u)
- }
-}
-
-func TestParseUrl_IPv4AndPort(t *testing.T) {
- defaultDNSResolver = &mockDnsResolver{Addr: "127.0.0.1"}
- u, s := resolveUrl("http://google.com:80")
- if s != "google.com:80" {
- t.Errorf("Original server name doesn't match with google.com, %v is found.", s)
- }
- if u != "http://127.0.0.1:80" {
- t.Errorf("URL is expected to be http://127.0.0.1, %v is found.", u)
- }
-}
-
-func TestParseUrl_IPv6(t *testing.T) {
- defaultDNSResolver = &mockDnsResolver{Addr: "2a00:1450:400a:806::1007"}
- u, s := resolveUrl("http://google.com")
- if s != "google.com" {
- t.Errorf("Original server name doesn't match with google.com, %v is found.", s)
- }
- if u != "http://[2a00:1450:400a:806::1007]" {
- t.Errorf("URL is expected to be http://[2a00:1450:400a:806::1007], %v is found.", u)
- }
-}
-
-func TestParseUrl_IPv6AndPort(t *testing.T) {
- defaultDNSResolver = &mockDnsResolver{Addr: "2a00:1450:400a:806::1007"}
- u, s := resolveUrl("http://google.com:80")
- if s != "google.com:80" {
- t.Errorf("Original server name doesn't match with google.com, %v is found.", s)
- }
- if u != "http://[2a00:1450:400a:806::1007]:80" {
- t.Errorf("URL is expected to be http://[2a00:1450:400a:806::1007]:80, %v is found.", u)
- }
-}
-
func TestParseValidHeaderFlag(t *testing.T) {
match, err := parseInputWithRegexp("X-Something: !Y10K:;(He@poverflow?)", headerRegexp)
if err != nil {
diff --git a/boomer/boomer.go b/boomer/boomer.go
index cac0f18..65d42e1 100644
--- a/boomer/boomer.go
+++ b/boomer/boomer.go
@@ -38,20 +38,12 @@ type ReqOpts struct {
Body string
Username string
Password string
- // OriginalHost represents the original host name user is provided.
- // Request host is an resolved IP. TLS/SSL handshakes may require
- // the original server name, keep it to initate the TLS client.
- OriginalHost string
}
// Creates a req object from req options
func (r *ReqOpts) Request() *http.Request {
req, _ := http.NewRequest(r.Method, r.URL, strings.NewReader(r.Body))
req.Header = r.Header
-
- // update the Host value in the Request - this is used as the host header in any subsequent request
- req.Host = r.OriginalHost
-
if r.Username != "" && r.Password != "" {
req.SetBasicAuth(r.Username, r.Password)
}
diff --git a/boomer/run.go b/boomer/run.go
index 6cb777d..1139655 100644
--- a/boomer/run.go
+++ b/boomer/run.go
@@ -19,7 +19,6 @@ import (
"sync"
- "net"
"net/http"
"time"
)
@@ -43,11 +42,9 @@ func (b *Boomer) Run() {
}
func (b *Boomer) worker(wg *sync.WaitGroup, ch chan *http.Request) {
- host, _, _ := net.SplitHostPort(b.Req.OriginalHost)
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: b.AllowInsecure,
- ServerName: host,
},
DisableCompression: b.DisableCompression,
DisableKeepAlives: b.DisableKeepAlives,