Skip to content

Commit

Permalink
Merge pull request #14 from rogerwelin/post-data
Browse files Browse the repository at this point in the history
Post data
  • Loading branch information
rogerwelin authored Jan 27, 2020
2 parents 9aebfed + 1bb47d6 commit 05c62a2
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 13 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ Features
- **2 Load Testing modes**: one standard and one spread mode where URL Paths can be specified from a file (ideal if you want to hit several underlying microservices)
- **CI Friendly**: Well-suited to be part of a CI pipeline step
- **Flexible metrics**: Prometheus metrics (pushing metrics to Prometheus PushGateway), JSON file
- **Configurable**: Able to pass in arbitrary HTTP headers
- **Configurable**: Able to pass in arbitrary HTTP headers, able to configure the HTTP client
- **Supports GET, POST & PUT** - POST and PUT data can be defined in a file
- **Cross Platform**: One single pre-built binary for Linux, Mac OSX and Windows
- **Importable** - Besides the CLI tool cassowary can be imported as a module in your Go app

Expand Down Expand Up @@ -146,6 +147,17 @@ Starting Load Test with 100000 requests using 125 concurrent users

```

Example hitting a POST endpoint where POST json data is defined in a file:

```bash
$ ./cassowary run -u http://localhost:8000/add-user -c 10 -n 1000 --post-file user.json

Starting Load Test with 1000 requests using 10 concurrent users

[ omitted for brevity ]

```

Example adding an HTTP header when running **cassowary**

```bash
Expand Down Expand Up @@ -213,6 +225,10 @@ func main() {
}
```

Versioning
--------

Cassowary follows semantic versioning. The public library (pkg/client) may break backwards compability until it hits a stable v1.0.0 release.

Contributing
--------
Expand Down
48 changes: 44 additions & 4 deletions cmd/cassowary/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"

Expand Down Expand Up @@ -66,10 +67,20 @@ func runLoadTest(c *client.Cassowary) error {
return nil
}

func readFile(file string) ([]byte, error) {
fileContent, err := ioutil.ReadFile(file)
if err != nil {
return []byte{}, err
}
return fileContent, nil
}

func validateCLI(c *cli.Context) error {

prometheusEnabled := false
var header []string
var httpMethod string
var data []byte

if c.Int("concurrency") == 0 {
return errConcurrencyLevel
Expand All @@ -95,6 +106,24 @@ func validateCLI(c *cli.Context) error {
}
}

if c.String("postfile") != "" {
httpMethod = "POST"
fileData, err := readFile(c.String("postfile"))
if err != nil {
return err
}
data = fileData
} else if c.String("putfile") != "" {
httpMethod = "PUT"
fileData, err := readFile(c.String("putfile"))
if err != nil {
return err
}
data = fileData
} else {
httpMethod = "GET"
}

cass := &client.Cassowary{
FileMode: false,
BaseURL: c.String("url"),
Expand All @@ -107,6 +136,8 @@ func validateCLI(c *cli.Context) error {
ExportMetricsFile: c.String("json-metrics-file"),
DisableKeepAlive: c.Bool("disable-keep-alive"),
Timeout: c.Int("timeout"),
HTTPMethod: httpMethod,
Data: data,
}

return runLoadTest(cass)
Expand Down Expand Up @@ -149,6 +180,7 @@ func validateCLIFile(c *cli.Context) error {
DisableKeepAlive: c.Bool("diable-keep-alive"),
Timeout: c.Int("timeout"),
Requests: c.Int("requests"),
HTTPMethod: "GET",
}

return runLoadTest(cass)
Expand Down Expand Up @@ -197,7 +229,7 @@ func runCLI(args []string) {
},
cli.StringFlag{
Name: "H, header",
Usage: "add Arbitrary header line, eg. 'Host: www.example.com'",
Usage: "add arbitrary header, eg. 'Host: www.example.com'",
},
cli.BoolFlag{
Name: "F, json-metrics",
Expand All @@ -209,7 +241,7 @@ func runCLI(args []string) {
},
cli.BoolFlag{
Name: "disable-keep-alive",
Usage: "Use this flag to not use http keep-alive",
Usage: "use this flag to disable http keep-alive",
},
},
Action: validateCLIFile,
Expand Down Expand Up @@ -244,19 +276,27 @@ func runCLI(args []string) {
},
cli.StringFlag{
Name: "H, header",
Usage: "add Arbitrary header line, eg. 'Host: www.example.com'",
Usage: "add arbitrary header, eg. 'Host: www.example.com'",
},
cli.BoolFlag{
Name: "F, json-metrics",
Usage: "outputs metrics to a json file by setting flag to true",
},
cli.StringFlag{
Name: "postfile",
Usage: "file containing data to POST (content type will default to application/json)",
},
cli.StringFlag{
Name: "putfile",
Usage: "file containig data to PUT (content type will default to application/json)",
},
cli.StringFlag{
Name: "json-metrics-file",
Usage: "outputs metrics to a custom json filepath, if json-metrics is set to true",
},
cli.BoolFlag{
Name: "disable-keep-alive",
Usage: "Use this flag to not use http keep-alive",
Usage: "use this flag to disable http keep-alive",
},
},
Action: validateCLI,
Expand Down
32 changes: 24 additions & 8 deletions pkg/client/load.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"bytes"
"context"
"crypto/tls"
"fmt"
Expand Down Expand Up @@ -37,9 +38,24 @@ func (c *Cassowary) runLoadTest(outPutChan chan<- durationMetrics, workerChan ch
panic(err)
}
} else {
request, err = http.NewRequest("GET", c.BaseURL, nil)
if err != nil {
panic(err)
switch c.HTTPMethod {
case "POST":
request, err = http.NewRequest("POST", c.BaseURL, bytes.NewBuffer(c.Data))
request.Header.Set("Content-Type", "application/json")
if err != nil {
panic(err)
}
case "PUT":
request, err = http.NewRequest("PUT", c.BaseURL, bytes.NewBuffer(c.Data))
request.Header.Set("Content-Type", "application/json")
if err != nil {
panic(err)
}
default:
request, err = http.NewRequest("GET", c.BaseURL, nil)
if err != nil {
panic(err)
}
}
}

Expand Down Expand Up @@ -134,11 +150,11 @@ func (c *Cassowary) Coordinate() (ResultMetrics, error) {
c.Client = &http.Client{
Timeout: time.Second * time.Duration(c.Timeout),
Transport: &http.Transport{
MaxIdleConns: 300,
MaxIdleConnsPerHost: 300,
MaxConnsPerHost: 300,
DisableCompression: false,
DisableKeepAlives: c.DisableKeepAlive,
//MaxIdleConns: 300,
MaxIdleConnsPerHost: 10000,
//MaxConnsPerHost: 300,
DisableCompression: false,
DisableKeepAlives: c.DisableKeepAlive,
},
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/client/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type Cassowary struct {
Client *http.Client
Bar *progressbar.ProgressBar
Timeout int
HTTPMethod string
Data []byte
}

// ResultMetrics are the aggregated metrics after the load test
Expand Down

0 comments on commit 05c62a2

Please sign in to comment.