Skip to content

Commit

Permalink
Merge pull request #41 from richardmarshall/http_request_body
Browse files Browse the repository at this point in the history
Add flag to set request body payload
  • Loading branch information
klingerf authored Jan 11, 2017
2 parents b669f5d + 66331f6 commit 992c8f3
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ requests that fall into that bucket.

Exit after sending this many requests.

`-data <string>`

Include the specified body data in requests. If the data starts with a '@' the
remaining value will be treated as a file path to read the data contents from,
or '-' to read from stdin.

# Using multiple Host headers

If you want to send multiple Host headers to a backend, pass a comma separated
Expand Down
38 changes: 36 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"crypto/tls"
"flag"
"fmt"
Expand Down Expand Up @@ -65,11 +66,12 @@ func sendRequest(
url *url.URL,
host string,
headers headerSet,
requestData []byte,
reqID uint64,
received chan *MeasuredResponse,
bodyBuffer []byte,
) {
req, err := http.NewRequest(method, url.String(), nil)
req, err := http.NewRequest(method, url.String(), bytes.NewBuffer(requestData))
if err != nil {
fmt.Fprintln(os.Stderr, err.Error())
fmt.Fprintf(os.Stderr, "\n")
Expand Down Expand Up @@ -151,6 +153,35 @@ func (h *headerSet) Set(s string) error {
return nil
}

func loadData(data string) []byte {
var file *os.File
var requestData []byte
var err error
if strings.HasPrefix(data, "@") {
path := data[1:]
if path == "-" {
file = os.Stdin
} else {
file, err = os.Open(path)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
defer file.Close()
}

requestData, err = ioutil.ReadAll(file)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
} else {
requestData = []byte(data)
}

return requestData
}

func main() {
qps := flag.Int("qps", 1, "QPS to send to backends per request thread")
concurrency := flag.Int("concurrency", 1, "Number of request threads")
Expand All @@ -166,6 +197,7 @@ func main() {
totalRequests := flag.Uint64("totalRequests", 0, "total number of requests to send before exiting")
headers := make(headerSet)
flag.Var(&headers, "header", "HTTP request header. (can be repeated.)")
data := flag.String("data", "", "HTTP request data")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s <url> [flags]\n", path.Base(os.Args[0]))
Expand Down Expand Up @@ -199,6 +231,8 @@ func main() {

hosts := strings.Split(*host, ",")

requestData := loadData(*data)

// Repsonse tracking metadata.
count := uint64(0)
size := uint64(0)
Expand Down Expand Up @@ -238,7 +272,7 @@ func main() {
shouldFinishLock.RLock()
if !shouldFinish {
shouldFinishLock.RUnlock()
sendRequest(client, *method, dstURL, hosts[rand.Intn(len(hosts))], headers, atomic.AddUint64(&reqID, 1), received, bodyBuffer)
sendRequest(client, *method, dstURL, hosts[rand.Intn(len(hosts))], headers, requestData, atomic.AddUint64(&reqID, 1), received, bodyBuffer)
} else {
shouldFinishLock.RUnlock()
sendTraffic.Done()
Expand Down

0 comments on commit 992c8f3

Please sign in to comment.