Skip to content

armgit / hey

Public Imported from GitHub
0

HTTP load generator, ApacheBench (ab) replacement

Go

Only lookup the hostname once

Pascal Hakimc47c36d5bd71
diff --git a/commands/boom.go b/commands/boom.go
index bd63701..86b2e46 100644
--- a/commands/boom.go
+++ b/commands/boom.go
@@ -47,6 +47,8 @@ type Boom struct {
bar *pb.ProgressBar
rpt *report
results chan *result
+
+ ServerName string
}
func newPb(size int) (bar *pb.ProgressBar) {
diff --git a/commands/run.go b/commands/run.go
index 4a972c0..c91eb28 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},
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: b.AllowInsecure, ServerName: b.ServerName},
}
client := &http.Client{Transport: tr}
for _ = range ch {
diff --git a/main.go b/main.go
index 0c5f7bf..b1a99a2 100644
--- a/main.go
+++ b/main.go
@@ -17,6 +17,7 @@ package main
import (
"flag"
"fmt"
+ "net"
"net/http"
gourl "net/url"
"os"
@@ -89,14 +90,28 @@ func main() {
url := flag.Args()[0]
method := strings.ToUpper(*flagMethod)
- if _, err := gourl.ParseRequestURI(url); err != nil {
+ uri, err := gourl.ParseRequestURI(url)
+ if err != nil {
usageAndExit(err.Error())
}
- req, _ := http.NewRequest(method, url, strings.NewReader(*flagD))
+ hostParts := strings.Split(uri.Host, ":")
+ servername := hostParts[0]
+
+ addrs, err := net.LookupHost(hostParts[0])
+ if err != nil {
+ usageAndExit("Hostname " + uri.Host + " is invalid")
+ }
+
+ hostParts[0] = addrs[0]
+ uri.Host = strings.Join(hostParts, ":")
+
+ req, _ := http.NewRequest(method, uri.String(), strings.NewReader(*flagD))
// set content-type
req.Header.Set("Content-Type", *flagType)
+ req.Host = servername
+
// set any other additional headers
if *flagHeaders != "" {
headers := strings.Split(*flagHeaders, ";")
@@ -131,7 +146,9 @@ func main() {
Timeout: t,
Req: req,
AllowInsecure: *flagInsecure,
- Output: *flagOutput}).Run()
+ Output: *flagOutput,
+ ServerName: servername,
+ }).Run()
}
func usageAndExit(message string) {