From f60fa1bad45302631e62466b35b556401b996684 Mon Sep 17 00:00:00 2001 From: Joon Lee Date: Thu, 24 Oct 2019 13:58:38 -0700 Subject: [PATCH 1/4] Add support to fetch request body from file --- bench/bench.go | 5 +++-- web_requester.go | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/bench/bench.go b/bench/bench.go index 08f75cc..cdca1cd 100644 --- a/bench/bench.go +++ b/bench/bench.go @@ -25,7 +25,7 @@ const ( type RequesterFactory interface { // GetRequester returns a new Requester, called for each Benchmark // connection. - GetRequester(number uint64) Requester + GetRequester(number uint64) (Requester, error) } // Requester synchronously issues requests for a particular system under test. @@ -107,7 +107,8 @@ func (b *Benchmark) Run(outputJson bool, forceTightTicker bool) (*Summary, error for i := uint64(0); i < b.connections; i++ { i := i go func() { - b.worker(b.factory.GetRequester(i), ticker, results, errors) + requester, _ := b.factory.GetRequester(i) + b.worker(requester, ticker, results, errors) // log.Printf("Worker %d done\n", i) wg.Done() }() diff --git a/web_requester.go b/web_requester.go index f4d6dab..ce0f138 100644 --- a/web_requester.go +++ b/web_requester.go @@ -92,6 +92,7 @@ type WebRequesterFactory struct { Hosts []string `yaml:"Hosts"` Headers map[string]string `yaml:"Headers"` Body string `yaml:"Body"` + BodyPath string `yaml:"BodyPath"` ExpectedHTTPStatusCode int `yaml:"ExpectedHTTPStatusCode"` HTTPMethod string `yaml:"HTTPMethod"` @@ -99,7 +100,7 @@ type WebRequesterFactory struct { } // GetRequester returns a new Requester, called for each Benchmark connection. -func (w *WebRequesterFactory) GetRequester(uint64) bench.Requester { +func (w *WebRequesterFactory) GetRequester(uint64) (bench.Requester, error) { // if len(w.expandedHeaders) != len(w.Headers) { if w.expandedHeaders == nil { expandedHeaders := make(map[string][]string) @@ -109,7 +110,16 @@ func (w *WebRequesterFactory) GetRequester(uint64) bench.Requester { w.expandedHeaders = expandedHeaders } - return &webRequester{w.URL, w.URLs, w.Hosts, w.expandedHeaders, w.Body, w.ExpectedHTTPStatusCode, w.HTTPMethod} + // if BodyPath is specified Body is ignored + if w.BodyPath != "" { + content, err := ioutil.ReadFile(w.BodyPath) + if err != nil { + return nil, err + } + w.Body = string(content) + } + + return &webRequester{w.URL, w.URLs, w.Hosts, w.expandedHeaders, w.Body, w.ExpectedHTTPStatusCode, w.HTTPMethod}, nil } // webRequester implements Requester by making a GET request to the provided From d727ae44422c3865cbde5f66b53981d4ba1f70cb Mon Sep 17 00:00:00 2001 From: Joon Lee Date: Thu, 24 Oct 2019 17:19:41 -0700 Subject: [PATCH 2/4] Update config sample with RequestBody --- full_config.yaml | 5 ++++- main.go | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/full_config.yaml b/full_config.yaml index 58d72a4..8154d09 100644 --- a/full_config.yaml +++ b/full_config.yaml @@ -38,7 +38,7 @@ Protocol: HTTP/2 OutFile: "out/res.hgrm" Request: - # HTTPMethod defaults to GET if Body (below) is not present and to POST otherwise, but can be specified explicitly + # HTTPMethod defaults to GET if Body or BodyPath (below) is not present and to POST otherwise, but can be specified explicitly HTTPMethod: POST # ExpectedHTTPStatusCode defaults to 200 @@ -76,3 +76,6 @@ Request: } } } + + # POST request body. This will override the Body above. + BodyPath: path/to/file diff --git a/main.go b/main.go index feacbf5..67d58f2 100644 --- a/main.go +++ b/main.go @@ -69,7 +69,7 @@ func main() { } if conf.Request.HTTPMethod == "" { - if conf.Request.Body == "" { + if conf.Request.Body == "" && conf.Request.BodyPath == "" { conf.Request.HTTPMethod = http.MethodGet } else { conf.Request.HTTPMethod = http.MethodPost From 86dc0d90df4918887e159fbf434c77a2f0a9ecbb Mon Sep 17 00:00:00 2001 From: Joon Lee Date: Sun, 3 Nov 2019 21:05:57 -0800 Subject: [PATCH 3/4] Address PR comment, use maybePanic --- bench/bench.go | 5 ++--- main.go | 2 +- web_requester.go | 14 +++++++------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/bench/bench.go b/bench/bench.go index cdca1cd..08f75cc 100644 --- a/bench/bench.go +++ b/bench/bench.go @@ -25,7 +25,7 @@ const ( type RequesterFactory interface { // GetRequester returns a new Requester, called for each Benchmark // connection. - GetRequester(number uint64) (Requester, error) + GetRequester(number uint64) Requester } // Requester synchronously issues requests for a particular system under test. @@ -107,8 +107,7 @@ func (b *Benchmark) Run(outputJson bool, forceTightTicker bool) (*Summary, error for i := uint64(0); i < b.connections; i++ { i := i go func() { - requester, _ := b.factory.GetRequester(i) - b.worker(requester, ticker, results, errors) + b.worker(b.factory.GetRequester(i), ticker, results, errors) // log.Printf("Worker %d done\n", i) wg.Done() }() diff --git a/main.go b/main.go index 67d58f2..412a08b 100644 --- a/main.go +++ b/main.go @@ -69,7 +69,7 @@ func main() { } if conf.Request.HTTPMethod == "" { - if conf.Request.Body == "" && conf.Request.BodyPath == "" { + if conf.Request.Body == "" && conf.Request.BodyFile == "" { conf.Request.HTTPMethod = http.MethodGet } else { conf.Request.HTTPMethod = http.MethodPost diff --git a/web_requester.go b/web_requester.go index ce0f138..784ff5d 100644 --- a/web_requester.go +++ b/web_requester.go @@ -92,7 +92,7 @@ type WebRequesterFactory struct { Hosts []string `yaml:"Hosts"` Headers map[string]string `yaml:"Headers"` Body string `yaml:"Body"` - BodyPath string `yaml:"BodyPath"` + BodyFile string `yaml:"BodyFile"` ExpectedHTTPStatusCode int `yaml:"ExpectedHTTPStatusCode"` HTTPMethod string `yaml:"HTTPMethod"` @@ -100,7 +100,7 @@ type WebRequesterFactory struct { } // GetRequester returns a new Requester, called for each Benchmark connection. -func (w *WebRequesterFactory) GetRequester(uint64) (bench.Requester, error) { +func (w *WebRequesterFactory) GetRequester(uint64) bench.Requester { // if len(w.expandedHeaders) != len(w.Headers) { if w.expandedHeaders == nil { expandedHeaders := make(map[string][]string) @@ -110,16 +110,16 @@ func (w *WebRequesterFactory) GetRequester(uint64) (bench.Requester, error) { w.expandedHeaders = expandedHeaders } - // if BodyPath is specified Body is ignored - if w.BodyPath != "" { - content, err := ioutil.ReadFile(w.BodyPath) + // if BodyFile is specified Body is ignored + if w.BodyFile != "" { + content, err := ioutil.ReadFile(w.BodyFile) if err != nil { - return nil, err + maybePanic(err) } w.Body = string(content) } - return &webRequester{w.URL, w.URLs, w.Hosts, w.expandedHeaders, w.Body, w.ExpectedHTTPStatusCode, w.HTTPMethod}, nil + return &webRequester{w.URL, w.URLs, w.Hosts, w.expandedHeaders, w.Body, w.ExpectedHTTPStatusCode, w.HTTPMethod} } // webRequester implements Requester by making a GET request to the provided From 696d3c7a748c563b3ff3af6b317ebb87f160da63 Mon Sep 17 00:00:00 2001 From: Joon Lee Date: Mon, 4 Nov 2019 18:04:34 -0800 Subject: [PATCH 4/4] Address pr comments --- full_config.yaml | 4 ++-- web_requester.go | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/full_config.yaml b/full_config.yaml index 8154d09..8333146 100644 --- a/full_config.yaml +++ b/full_config.yaml @@ -38,7 +38,7 @@ Protocol: HTTP/2 OutFile: "out/res.hgrm" Request: - # HTTPMethod defaults to GET if Body or BodyPath (below) is not present and to POST otherwise, but can be specified explicitly + # HTTPMethod defaults to GET if Body or BodyFile (below) is not present and to POST otherwise, but can be specified explicitly HTTPMethod: POST # ExpectedHTTPStatusCode defaults to 200 @@ -78,4 +78,4 @@ Request: } # POST request body. This will override the Body above. - BodyPath: path/to/file + BodyFile: path/to/file diff --git a/web_requester.go b/web_requester.go index 784ff5d..2816956 100644 --- a/web_requester.go +++ b/web_requester.go @@ -113,9 +113,7 @@ func (w *WebRequesterFactory) GetRequester(uint64) bench.Requester { // if BodyFile is specified Body is ignored if w.BodyFile != "" { content, err := ioutil.ReadFile(w.BodyFile) - if err != nil { - maybePanic(err) - } + maybePanic(err) w.Body = string(content) }