Skip to content

Commit

Permalink
Add Work.RequestFunc (#149)
Browse files Browse the repository at this point in the history
* Remove unused variable in test

* Add Work.RequestFunc

With this commit, people using hey as a library can generate different
requests, rather than repeating the same request each time. This can be
useful for simulating a variety of requests or replaying traffic.
  • Loading branch information
twpayne committed Aug 6, 2020
1 parent f3676ef commit af17706
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
11 changes: 10 additions & 1 deletion requester/requester.go
Expand Up @@ -53,6 +53,10 @@ type Work struct {

RequestBody []byte

// RequestFunc is a function to generate requests. If it is nil, then
// Request and RequestData are cloned for each request.
RequestFunc func() *http.Request

// N is the total number of requests to make.
N int

Expand Down Expand Up @@ -146,7 +150,12 @@ func (b *Work) makeRequest(c *http.Client) {
var code int
var dnsStart, connStart, resStart, reqStart, delayStart time.Duration
var dnsDuration, connDuration, resDuration, reqDuration, delayDuration time.Duration
req := cloneRequest(b.Request, b.RequestBody)
var req *http.Request
if b.RequestFunc != nil {
req = b.RequestFunc()
} else {
req = cloneRequest(b.Request, b.RequestBody)
}
trace := &httptrace.ClientTrace{
DNSStart: func(info httptrace.DNSStartInfo) {
dnsStart = now()
Expand Down
3 changes: 1 addition & 2 deletions requester/requester_test.go
Expand Up @@ -73,10 +73,9 @@ func TestQps(t *testing.T) {
}

func TestRequest(t *testing.T) {
var uri, contentType, some, method, auth string
var uri, contentType, some, auth string
handler := func(w http.ResponseWriter, r *http.Request) {
uri = r.RequestURI
method = r.Method
contentType = r.Header.Get("Content-type")
some = r.Header.Get("X-some")
auth = r.Header.Get("Authorization")
Expand Down

0 comments on commit af17706

Please sign in to comment.