Compare commits

8 Commits

Author SHA1 Message Date
Senad Uka
124cf9bb0e rapid.go 2014-02-26 13:47:19 +01:00
Senad Uka
ba4851cf0f updated readme 2013-11-07 15:31:00 +00:00
Senad Uka
f536e10f5c fixed bug with stdin reading in rapid, updated readme 2013-11-07 15:30:16 +00:00
Senad Uka
de85e8709a finished csv reporter support 2013-11-07 12:12:39 +00:00
Senad Uka
681a7f2d02 modified results and metrics 2013-11-07 11:27:05 +00:00
Senad Uka
441a898718 restored original create file instead of append 2013-11-05 15:37:49 +00:00
Senad Uka
ef70acad23 added support for appending to results file and added Rate to result struct 2013-11-05 15:08:06 +00:00
Senad Uka
bd8d1633b9 added support for appending to results file 2013-11-05 15:06:29 +00:00
7 changed files with 235 additions and 16 deletions

View File

@@ -4,14 +4,9 @@ Vegeta is a versatile HTTP load testing tool built out of need to drill
HTTP services with a constant request rate.
It can be used both as a command line utility and a library.
![Vegeta](http://fc09.deviantart.net/fs49/i/2009/198/c/c/ssj2_vegeta_by_trunks24.jpg)
*This fork ads support to attacking with multiple rates and a csv reporter suitable for import into spreadsheet applications.*
## Install
### Pre-compiled executables
* [Mac OSX 64 bit](https://dl.dropboxusercontent.com/u/83217940/vegeta-darwin-amd64-1517f2d.tar.gz)
* [Mac OSX 32 bit](https://dl.dropboxusercontent.com/u/83217940/vegeta-darwin-386-1517f2d.tar.gz)
* [Linux 64 bit](https://dl.dropboxusercontent.com/u/83217940/vegeta-linux-amd64-1517f2d.tar.gz)
* [Linux 32 bit](https://dl.dropboxusercontent.com/u/83217940/vegeta-linux-386-1517f2d.tar.gz)
![Vegeta](http://fc09.deviantart.net/fs49/i/2009/198/c/c/ssj2_vegeta_by_trunks24.jpg)
### Source
You need go installed and `GOBIN` in your `PATH`. Once that is done, run the
@@ -23,10 +18,7 @@ $ go install github.com/tsenart/vegeta
## Usage examples
```shell
$ echo "GET http://localhost/" | vegeta attack -rate=100 -duration=5s | vegeta report
$ vegeta attack -targets=targets.txt > results.vr
$ vegeta report -input=results.vr -reporter=json > metrics.json
$ cat results.vr | vegeta report -reporter=plot > plot.html
$ echo "GET http://localhost/" | vegeta rapid -rates=100,200,300 -duration=10s | vegeta report -reporter=csv
```
## Usage manual
@@ -37,6 +29,7 @@ Usage: vegeta [globals] <command> [options]
Commands:
attack Hit the targets
report Report the results
rapid Hit the targets with multiple rates
Globals:
-cpus=8 Number of CPUs to use

View File

@@ -36,7 +36,7 @@ func drill(rate uint64, reqs chan *http.Request, res chan Result) {
throttle := time.Tick(time.Duration(1e9 / rate))
for req := range reqs {
<-throttle
go hit(req, res)
go hit(req, res, rate)
}
}
@@ -51,13 +51,14 @@ var client = &http.Client{
// hit executes the passed http.Request and puts the result into results.
// Both transport errors and unsucessfull requests (non {2xx,3xx}) are
// considered errors.
func hit(req *http.Request, res chan Result) {
func hit(req *http.Request, res chan Result, rate uint64) {
began := time.Now()
r, err := client.Do(req)
result := Result{
Timestamp: began,
Latency: time.Since(began),
BytesOut: uint64(req.ContentLength),
Rate: rate,
}
if err != nil {
result.Error = err.Error()

View File

@@ -3,8 +3,9 @@ package vegeta
import (
"strconv"
"time"
"github.com/bmizerany/perks/quantile"
"fmt"
"strings"
)
// Metrics holds the stats computed out of a slice of Results
@@ -76,3 +77,17 @@ func NewMetrics(results []Result) *Metrics {
return m
}
func csvString(d time.Duration) string {
var result float64 = float64(d.Nanoseconds() / 1000.0 / 1000.0) // in miliseconds
return strconv.FormatFloat(result, 'f', -1, 64)
}
func(m *Metrics) Csv(rate uint64) []string {
result := fmt.Sprintf("%d req/s,%s,%s,%s,%s,%f,%f,%.2f",rate,
csvString(m.Latencies.Mean), csvString(m.Latencies.P95), csvString(m.Latencies.P99), csvString(m.Latencies.Max),
m.BytesIn.Mean, m.BytesOut.Mean, m.Success * 100)
return strings.Split(result, ",")
}

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"text/tabwriter"
"encoding/csv"
)
// Reporter represents any function which takes a slice of Results and
@@ -88,3 +89,69 @@ var plotsTemplate = `<!doctype>
</script>
</body>
</html>`
type ResultGroup struct {
from int
to int
rate uint64
}
func ReportCSV(results []Result) ([]byte, error) {
out := &bytes.Buffer{}
// result := fnmt.Sprintf("%d req/s,%s,%s,%s,%s,%f,%f,%f",rate,
// m.Latencies.Mean.CsvString(), m.Latencies.P95.CsvString(), m.Latencies.P99.CsvString(), m.Latencies.Max.CsvString(),
// m.BytesIn.Mean, m.BytesOut.Mean, m.Success)
header := []string{ "rate" , "mean_ms" , "p95_ms", "p99_ms" , "max_ms", "bytesIn_B", "bytesOut_B", "success_percent" }
w := csv.NewWriter(out)
w.Write(header)
resultGroups := slicesPerAttackRate(results)
for _,resultGroup := range resultGroups {
m := NewMetrics(results[resultGroup.from:resultGroup.to])
w.Write(m.Csv(resultGroup.rate))
}
w.Flush()
return out.Bytes(), nil
}
func slicesPerAttackRate(results []Result) ([]ResultGroup) {
resultGroups := []ResultGroup{}
if len(results) > 0 {
resultGroup := ResultGroup{}
resultGroup.from = 0
resultGroup.to = 0
resultGroup.rate = results[0].Rate
for i, result := range results {
if result.Rate != resultGroup.rate {
resultGroup.to = i
resultGroups = append(resultGroups, resultGroup)
resultGroup = ResultGroup{}
resultGroup.from = i
resultGroup.rate = result.Rate
}
}
resultGroup.to = len(results)
resultGroups = append(resultGroups, resultGroup)
}
return resultGroups
}

139
rapid.go Normal file
View File

@@ -0,0 +1,139 @@
package main
import (
// "bytes"
"flag"
"fmt"
vegeta "github.com/senaduka/vegeta/lib"
"log"
"net/http"
"strings"
"time"
"strconv"
)
type rateList []uint64
// String is the method to format the flag's value, part of the flag.Value interface.
// The String method's output will be used in diagnostics.
func (i *rateList) String() string {
return fmt.Sprint(*i)
}
// Set is the method to set the flag value, part of the flag.Value interface.
// Set's argument is a string to be parsed to set the flag.
// It's a comma-separated list, so we split it.
func (i *rateList) Set(value string) error {
for _, singleRate := range strings.Split(value, ",") {
oneRate, err := strconv.ParseUint(singleRate, 10, 64)
if err != nil {
return err
}
*i = append(*i, oneRate)
}
return nil
}
// attack validates the attack arguments, sets up the
// required resources, launches the attack and writes the results
func rapidAttack(rate uint64, duration time.Duration, targets *vegeta.Targets , ordering, output string, header http.Header, previousResults vegeta.Results ) (vegeta.Results, error) {
if rate == 0 {
return nil, fmt.Errorf(errRatePrefix + "can't be zero")
}
if duration == 0 {
return nil, fmt.Errorf(errDurationPrefix + "can't be zero")
}
targets.SetHeader(header)
switch ordering {
case "random":
targets.Shuffle(time.Now().UnixNano())
case "sequential":
break
default:
return nil, fmt.Errorf(errOrderingPrefix+"`%s` is invalid", ordering)
}
log.Printf("Vegeta is attacking %d targets in %s order for %s with %d requests/sec...\n", len(*targets), ordering, duration, rate)
results := vegeta.Attack(*targets, rate, duration)
log.Println("Done!")
return append(previousResults, results...), nil
}
func writeResults(results vegeta.Results , output string) error {
out, err := file(output, true)
if err != nil {
return fmt.Errorf(errOutputFilePrefix+"(%s): %s", output, err)
}
defer out.Close()
log.Printf("Writing results to '%s'...", output)
if err := results.Encode(out); err != nil {
return err
}
return nil
}
func rapidCmd(args []string) command {
fs := flag.NewFlagSet("rapid", flag.ExitOnError)
targetsf := fs.String("targets", "stdin", "Targets file")
ordering := fs.String("ordering", "random", "Attack ordering [sequential, random]")
duration := fs.Duration("duration", 10*time.Second, "Duration of the attack on every rate")
output := fs.String("output", "stdout", "Output file")
hdrs := headers{Header: make(http.Header)}
fs.Var(hdrs, "header", "Targets request header")
var rateFlag rateList = []uint64{}
fs.Var(&rateFlag, "rates", "One or more rates, comma separated in requests per second")
fs.Parse(args)
in, err := file(*targetsf, false)
if err != nil {
return nil
}
defer in.Close()
targets, err := vegeta.NewTargetsFrom(in)
if err != nil {
return nil
}
return func() error {
results := make(vegeta.Results,0)
var err error = nil
for _, rate := range rateFlag {
if results, err = rapidAttack(rate, *duration, &targets, *ordering, *output, hdrs.Header, results); err != nil {
return err
}
}
if err = writeResults(results, *output); err != nil {
return err
}
return nil
}
}

View File

@@ -2,14 +2,14 @@ package main
import (
"flag"
vegeta "github.com/tsenart/vegeta/lib"
vegeta "github.com/senaduka/vegeta/lib"
"log"
"strings"
)
func reportCmd(args []string) command {
fs := flag.NewFlagSet("report", flag.ExitOnError)
reporter := fs.String("reporter", "text", "Reporter [text, json, plot]")
reporter := fs.String("reporter", "text", "Reporter [text, json, plot, csv]")
input := fs.String("input", "stdin", "Input files (comma separated)")
output := fs.String("output", "stdout", "Output file")
fs.Parse(args)
@@ -30,6 +30,8 @@ func report(reporter, input, output string) error {
rep = vegeta.ReportJSON
case "plot":
rep = vegeta.ReportPlot
case "csv":
rep = vegeta.ReportCSV
default:
log.Println("Reporter provided is not supported. Using text")
rep = vegeta.ReportText
@@ -45,6 +47,8 @@ func report(reporter, input, output string) error {
results := vegeta.Results{}
if err := results.Decode(in); err != nil {
return err
} else {
log.Printf("Number of results: %d", len(results))
}
all = append(all, results...)
}

BIN
result Normal file

Binary file not shown.