Working Vegeta
This commit is contained in:
67
README.md
67
README.md
@@ -1,4 +1,65 @@
|
|||||||
vegeta
|
# Vegeta
|
||||||
======
|
|
||||||
|
Vegeta is a versatile HTTP load testing tool built out of need to drill
|
||||||
|
HTTP services with a relatively constant request rate.
|
||||||
|
|
||||||
|
## Install
|
||||||
|
You need go installed and `GOBIN` in your `PATH`. Once that is done, run the
|
||||||
|
command:
|
||||||
|
```shell
|
||||||
|
$ go install github.com/tsenart/vegeta
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
```shell
|
||||||
|
$ vegeta -h
|
||||||
|
Usage of vegeta:
|
||||||
|
-duration=10s: Duration of the test
|
||||||
|
-ordering="random": sequential or random
|
||||||
|
-rate=50: Requests per second
|
||||||
|
-reporter="text": Reporter to use [text]
|
||||||
|
-targets="targets.txt": Targets file
|
||||||
|
```
|
||||||
|
|
||||||
|
#### -duration
|
||||||
|
Specifies the amount of time to issue request to the targets.
|
||||||
|
The internal concurrency structure's setup has this value as a variable.
|
||||||
|
The actual run time of the test can be longer than specified due to the
|
||||||
|
responses delay.
|
||||||
|
|
||||||
|
#### -ordering
|
||||||
|
Specifies the ordering of target attack. The default is `random` and
|
||||||
|
it will randomly pick one of the targets per request without ever choosing
|
||||||
|
that target again.
|
||||||
|
The other option is `sequential` and it does what you would expect it to
|
||||||
|
do.
|
||||||
|
|
||||||
|
#### -rate
|
||||||
|
Specifies the requests per second rate to issue requests with against
|
||||||
|
the targets
|
||||||
|
|
||||||
|
#### -reporter
|
||||||
|
Specifies the reporting type to display the results with.
|
||||||
|
The default is a text report printed to stdout.
|
||||||
|
|
||||||
|
#### -targets
|
||||||
|
Specifies the attack targets in a line sepated file. The format should
|
||||||
|
be as follows:
|
||||||
|
```
|
||||||
|
GET http://goku:9090/path/to/dragon?item=balls
|
||||||
|
HEAD http://goku:9090/path/to/success
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
## TODO
|
||||||
|
* Add timeout options to the requests
|
||||||
|
* Graphical reports
|
||||||
|
* Test
|
||||||
|
|
||||||
|
## Licence
|
||||||
|
See the `LICENSE` file.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
HTTP load testing tool
|
|
||||||
|
|||||||
73
client.go
73
client.go
@@ -1,49 +1,62 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Client is an http.Client with rate limiting and time series instrumentation.
|
// Client is an http.Client with rate limiting
|
||||||
|
// TODO: Add timeouts
|
||||||
type Client struct {
|
type Client struct {
|
||||||
cli http.Client
|
http.Client
|
||||||
qps uint
|
rate uint
|
||||||
codes []uint64
|
|
||||||
timings []time.Duration
|
|
||||||
bytesOut []int64
|
|
||||||
bytesIn []int64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(qps uint) *Client {
|
// Response represents the metrics we want out of an http.Response
|
||||||
return &Client{
|
type Response struct {
|
||||||
cli: http.Client{},
|
code uint64
|
||||||
qps: qps,
|
timestamp time.Time
|
||||||
codes: []uint64{},
|
timing time.Duration
|
||||||
timings: []time.Duration{},
|
bytesOut uint64
|
||||||
bytesOut: []int64{},
|
bytesIn uint64
|
||||||
bytesIn: []int64{},
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewClient returns an initialized Client
|
||||||
|
func NewClient(rate uint) *Client {
|
||||||
|
return &Client{http.Client{}, rate}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Drill loops over the passed reqs channel and executes each request.
|
// Drill loops over the passed reqs channel and executes each request.
|
||||||
// It is throttled to the qps specified in the initializer
|
// It is throttled to the qps specified in the initializer
|
||||||
func (c *Client) Drill(reqs chan *http.Request) {
|
func (c *Client) Drill(reqs chan *http.Request, res chan *Response) {
|
||||||
throttle := time.Tick(time.Duration(1e9 / c.qps))
|
for _ = range time.Tick(time.Duration(1e9 / c.rate)) {
|
||||||
for req := range reqs {
|
if req, ok := <-reqs; !ok {
|
||||||
<-throttle
|
break
|
||||||
go c.Do(req)
|
} else {
|
||||||
|
go c.Do(req, res)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Do executes the passed http.Request and saves some metrics
|
// Do executes the passed http.Request and puts a generated *Response into res.
|
||||||
// (timings, bytesIn, bytesOut, codes)
|
func (c *Client) Do(req *http.Request, res chan *Response) {
|
||||||
func (c *Client) Do(req *http.Request) (*http.Response, error) {
|
|
||||||
began := time.Now()
|
began := time.Now()
|
||||||
resp, err := c.cli.Do(req)
|
r, err := c.Client.Do(req)
|
||||||
c.timings = append(c.timings, time.Since(began))
|
resp := &Response{
|
||||||
c.bytesOut = append(c.bytesOut, req.ContentLength)
|
timestamp: began,
|
||||||
c.bytesIn = append(c.bytesIn, resp.ContentLength)
|
timing: time.Since(began),
|
||||||
c.codes[resp.StatusCode]++
|
bytesOut: uint64(req.ContentLength),
|
||||||
return resp, err
|
err: err,
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
resp.bytesIn, resp.code = uint64(r.ContentLength), uint64(r.StatusCode)
|
||||||
|
if body, err := ioutil.ReadAll(r.Body); err != nil && resp.code < 200 || resp.code >= 300 {
|
||||||
|
resp.err = errors.New(string(body))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
res <- resp
|
||||||
}
|
}
|
||||||
|
|||||||
96
main.go
96
main.go
@@ -2,72 +2,90 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// -qps=10 -urls=xxx.txt -mode={sequential,random} {-requests=N,-duration=T}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var (
|
var (
|
||||||
// Flags
|
// Flags
|
||||||
qps = flag.Uint("qps", 50, "Queries Per Second")
|
rate = flag.Uint("rate", 50, "Requests per second")
|
||||||
urlsFile = flag.String("urls", "urls.txt", "URLs file")
|
targetsf = flag.String("targets", "targets.txt", "Targets file")
|
||||||
mode = flag.String("mode", "random", "sequential or random")
|
ordering = flag.String("ordering", "random", "Attack ordering [sequential, random]")
|
||||||
requests = flag.Uint("requests", 5000, "Number of requests to do")
|
duration = flag.Duration("duration", 10*time.Second, "Duration of the test")
|
||||||
duration = flag.Duration("duration", 10*time.Second, "Maximum duration of execution")
|
reporter = flag.String("reporter", "text", "Reporter to use [text]")
|
||||||
)
|
)
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Validate QPS argument
|
if flag.NFlag() == 0 {
|
||||||
if *qps == 0 {
|
flag.Usage()
|
||||||
log.Fatal("qps can't be zero")
|
return
|
||||||
}
|
|
||||||
// Magic formula that assumes each client can
|
|
||||||
// sustain 500 QPS under normal circumstances
|
|
||||||
clients := make([]*Client, int(math.Ceil(float64(*qps)/500.0)))
|
|
||||||
qpsClient := *qps / uint(len(clients))
|
|
||||||
for i := 0; i < len(clients); i++ {
|
|
||||||
clients[i] = NewClient(qpsClient)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse URLs file
|
// Validate rate argument
|
||||||
urls, err := NewURLsFromFile(*urlsFile)
|
if *rate == 0 {
|
||||||
|
log.Fatal("rate can't be zero")
|
||||||
|
}
|
||||||
|
// Magic formula that assumes each client can
|
||||||
|
// sustain 200 RPS under normal circumstances
|
||||||
|
clients := make([]*Client, int(math.Ceil(float64(*rate)/200.0)))
|
||||||
|
ratePerClient := *rate / uint(len(clients))
|
||||||
|
for i := 0; i < len(clients); i++ {
|
||||||
|
clients[i] = NewClient(ratePerClient)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse targets file
|
||||||
|
targets, err := NewTargetsFromFile(*targetsf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse mode argument
|
// Parse ordering argument
|
||||||
random := false
|
random := false
|
||||||
if *mode == "random" {
|
if *ordering == "random" {
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.Seed(time.Now().UnixNano())
|
||||||
random = true
|
random = true
|
||||||
} else if *mode != "sequential" {
|
} else if *ordering != "sequential" {
|
||||||
log.Fatal("Unknown mode %s", *mode)
|
log.Fatalf("Unknown ordering %s", *ordering)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse number of requests and duration
|
// Parse duration
|
||||||
if *requests == 0 && *duration == 0 {
|
if *duration == 0 {
|
||||||
log.Fatal("Neither requests or duration was provided")
|
log.Fatal("Duration provided is invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Hitting %d URLs in %s mode for %s with %d requests and %d clients.",
|
hits := make(chan *http.Request, *rate*uint((*duration).Seconds()))
|
||||||
len(urls), *mode, duration.String(), *requests, len(clients))
|
for i, idxs := 0, targets.Iter(random); i < cap(hits); i++ {
|
||||||
|
hits <- targets[idxs[i%len(idxs)]]
|
||||||
reqs := make(chan *http.Request, *requests)
|
}
|
||||||
|
// Attack!
|
||||||
|
responses := make(chan *Response, cap(hits))
|
||||||
for _, client := range clients {
|
for _, client := range clients {
|
||||||
go client.Drill(reqs)
|
go client.Drill(hits, responses)
|
||||||
}
|
}
|
||||||
for _, index := range urls.Iter(random) {
|
log.Printf("Vegeta is attacking ")
|
||||||
url := urls[index]
|
log.Printf("%d targets in %s order for %s with %d clients.\n", len(targets), *ordering, duration, len(clients))
|
||||||
req, err := http.NewRequest("GET", url.String(), nil)
|
|
||||||
if err != nil {
|
var rep Reporter
|
||||||
log.Fatal("Bad request: %s", err)
|
switch *reporter {
|
||||||
}
|
case "text":
|
||||||
reqs <- req
|
rep = NewTextReporter(len(responses))
|
||||||
|
default:
|
||||||
|
log.Println("reporter provided is not supported. using text")
|
||||||
|
rep = NewTextReporter(len(responses))
|
||||||
|
}
|
||||||
|
// Wait for all requests to finish
|
||||||
|
for i := 0; i < cap(responses); i++ {
|
||||||
|
rep.Add(<-responses)
|
||||||
|
}
|
||||||
|
close(hits)
|
||||||
|
close(responses)
|
||||||
|
|
||||||
|
if rep.Report(os.Stdout) != nil {
|
||||||
|
log.Fatal("Failed to report!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
76
reporters.go
Normal file
76
reporters.go
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Reporter represents any reporter of the results of the test
|
||||||
|
type Reporter interface {
|
||||||
|
Add(res *Response)
|
||||||
|
Report(io.Writer) error
|
||||||
|
}
|
||||||
|
|
||||||
|
type TextReporter struct {
|
||||||
|
responses []*Response
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTextReporter initializes a TextReporter with n responses
|
||||||
|
func NewTextReporter(n int) *TextReporter {
|
||||||
|
return &TextReporter{responses: make([]*Response, n)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add adds a response to be used in the report
|
||||||
|
// Order of arrival is not relevant for this reporter
|
||||||
|
func (r *TextReporter) Add(res *Response) {
|
||||||
|
r.responses = append(r.responses, res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Report computes and writes the report to out.
|
||||||
|
// It returns an error in case of failure.
|
||||||
|
func (r *TextReporter) Report(out io.Writer) error {
|
||||||
|
totalRequests := len(r.responses)
|
||||||
|
totalTime := time.Duration(0)
|
||||||
|
totalBytesOut := uint64(0)
|
||||||
|
totalBytesIn := uint64(0)
|
||||||
|
totalSuccess := uint64(0)
|
||||||
|
histogram := map[uint64]uint64{}
|
||||||
|
errors := map[string]struct{}{}
|
||||||
|
|
||||||
|
for _, res := range r.responses {
|
||||||
|
histogram[res.code]++
|
||||||
|
totalTime += res.timing
|
||||||
|
totalBytesOut += res.bytesOut
|
||||||
|
totalBytesIn += res.bytesIn
|
||||||
|
if res.code >= 200 && res.code < 300 {
|
||||||
|
totalSuccess++
|
||||||
|
}
|
||||||
|
if res.err != nil {
|
||||||
|
errors[res.err.Error()] = struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
avgTime := time.Duration(float64(totalTime) / float64(totalRequests))
|
||||||
|
avgBytesOut := float64(totalBytesOut) / float64(totalRequests)
|
||||||
|
avgBytesIn := float64(totalBytesIn) / float64(totalRequests)
|
||||||
|
avgSuccess := float64(totalSuccess) / float64(totalRequests)
|
||||||
|
|
||||||
|
buf := ""
|
||||||
|
buf += fmt.Sprintln("Results: ")
|
||||||
|
buf += fmt.Sprintf("Time (avg): %s\n", avgTime)
|
||||||
|
buf += fmt.Sprintf("Bytes out (avg): %f\n", avgBytesOut)
|
||||||
|
buf += fmt.Sprintf("Bytes in (avg): %f\n", avgBytesIn)
|
||||||
|
buf += fmt.Sprintf("Success ratio: %f\n", avgSuccess)
|
||||||
|
buf += fmt.Sprintf("Requests: %d\n", totalRequests)
|
||||||
|
buf += fmt.Sprintln("\nStatus codes histogram:")
|
||||||
|
for code, count := range histogram {
|
||||||
|
buf += fmt.Sprintf("%3d\t%d\n", code, count)
|
||||||
|
}
|
||||||
|
buf += fmt.Sprintln("\nError set:")
|
||||||
|
for err, _ := range errors {
|
||||||
|
buf += fmt.Sprintln(err)
|
||||||
|
}
|
||||||
|
_, err := out.Write([]byte(buf))
|
||||||
|
return err
|
||||||
|
}
|
||||||
61
targets.go
Normal file
61
targets.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"math/rand"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Targets []*http.Request
|
||||||
|
|
||||||
|
func NewTargetsFromFile(filename string) (Targets, error) {
|
||||||
|
file, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return Targets{}, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
return NewTargets(file)
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTargets(source io.Reader) (Targets, error) {
|
||||||
|
reader := bufio.NewReader(source)
|
||||||
|
targets := make([]*http.Request, 0)
|
||||||
|
|
||||||
|
for {
|
||||||
|
line, err := reader.ReadString('\n')
|
||||||
|
if err != nil && err == io.EOF {
|
||||||
|
break
|
||||||
|
} else if err != nil {
|
||||||
|
return targets, err
|
||||||
|
}
|
||||||
|
if line = strings.TrimSpace(line); line == "" { // Empty line
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
parts := strings.SplitN(line, " ", 2)
|
||||||
|
if len(parts) != 2 {
|
||||||
|
return targets, fmt.Errorf("Invalid request format: `%s`", line)
|
||||||
|
}
|
||||||
|
// Build request
|
||||||
|
req, err := http.NewRequest(parts[0], parts[1], nil)
|
||||||
|
if err != nil {
|
||||||
|
return targets, fmt.Errorf("Failed to build request: %s", err)
|
||||||
|
}
|
||||||
|
targets = append(targets, req)
|
||||||
|
}
|
||||||
|
return targets, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t Targets) Iter(random bool) []int {
|
||||||
|
if random {
|
||||||
|
return rand.Perm(len(t))
|
||||||
|
}
|
||||||
|
iter := make([]int, len(t))
|
||||||
|
for i := 0; i < len(t); i++ {
|
||||||
|
iter[i] = i
|
||||||
|
}
|
||||||
|
return iter
|
||||||
|
}
|
||||||
38
urls.go
38
urls.go
@@ -1,38 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"math/rand"
|
|
||||||
"net/url"
|
|
||||||
)
|
|
||||||
|
|
||||||
type URLs []*url.URL
|
|
||||||
|
|
||||||
func NewURLsFromFile(filename string) (urls URLs, err error) {
|
|
||||||
lines, err := ioutil.ReadFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
return URLs{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, line := range bytes.Split(lines, []byte("\n")) {
|
|
||||||
uri, err := url.Parse(string(line))
|
|
||||||
if err != nil {
|
|
||||||
return URLs{}, fmt.Errorf("Failed to parse URI (%s): %s", line, err)
|
|
||||||
}
|
|
||||||
urls = append(urls, uri)
|
|
||||||
}
|
|
||||||
return urls, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (urls URLs) Iter(random bool) []int {
|
|
||||||
if random {
|
|
||||||
return rand.Perm(len(urls))
|
|
||||||
}
|
|
||||||
iter := make([]int, len(urls))
|
|
||||||
for i := 0; i < len(urls); i++ {
|
|
||||||
iter[i] = i
|
|
||||||
}
|
|
||||||
return iter
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user