Files
old-vegeta/lib/attack.go
2013-09-10 18:58:49 +01:00

88 lines
2.2 KiB
Go

package vegeta
import (
"errors"
"io/ioutil"
"net/http"
"sort"
"time"
)
// Result represents the metrics defined out of an http.Response
// generated by each target hit
type Result struct {
Code uint16
Timestamp time.Time
Timing time.Duration
BytesOut uint64
BytesIn uint64
Error error
}
// Attack hits the passed Targets (http.Requests) at the rate specified for
// duration time and then waits for all the requests to come back.
// The results of the attack are put into a slice which is returned.
func Attack(targets Targets, rate uint64, duration time.Duration) []Result {
total := rate * uint64(duration.Seconds())
hits := make(chan *http.Request, total)
res := make(chan Result, total)
results := make(results, total)
// Scatter
go drill(rate, hits, res)
for i := 0; i < cap(hits); i++ {
hits <- targets[i%len(targets)]
}
close(hits)
// Gather
for i := 0; i < cap(res); i++ {
results[i] = <-res
}
close(res)
sort.Sort(results)
return results
}
// results is a slice of Result defined only to be sortable with sort.Interface
type results []Result
func (r results) Len() int { return len(r) }
func (r results) Less(i, j int) bool { return r[i].Timestamp.Before(r[j].Timestamp) }
func (r results) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
// drill loops over the passed reqs channel and executes each request.
// It is throttled to the rate specified.
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)
}
}
// 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) {
began := time.Now()
r, err := http.DefaultClient.Do(req)
result := Result{
Timestamp: began,
Timing: time.Since(began),
BytesOut: uint64(req.ContentLength),
Error: err,
}
if err == nil {
result.Code = uint16(r.StatusCode)
if body, err := ioutil.ReadAll(r.Body); err != nil {
if result.Code < 200 || result.Code >= 300 {
result.Error = errors.New(string(body))
}
} else {
result.BytesIn = uint64(len(body))
}
}
res <- result
}