Publicize vegeta.Result
This commit is contained in:
@@ -13,31 +13,31 @@ import (
|
||||
func Attack(targets Targets, rate uint64, duration time.Duration, rep Reporter) {
|
||||
hits := make(chan *http.Request, rate*uint64((duration).Seconds()))
|
||||
defer close(hits)
|
||||
responses := make(chan *result, cap(hits))
|
||||
defer close(responses)
|
||||
go drill(rate, hits, responses) // Attack!
|
||||
results := make(chan *Result, cap(hits))
|
||||
defer close(results)
|
||||
go drill(rate, hits, results) // Attack!
|
||||
for i := 0; i < cap(hits); i++ {
|
||||
hits <- targets[i%len(targets)]
|
||||
}
|
||||
// Wait for all requests to finish
|
||||
for i := 0; i < cap(responses); i++ {
|
||||
rep.add(<-responses)
|
||||
for i := 0; i < cap(results); i++ {
|
||||
rep.add(<-results)
|
||||
}
|
||||
}
|
||||
|
||||
// result represents the metrics we want out of an http.Response
|
||||
type result struct {
|
||||
code uint64
|
||||
timestamp time.Time
|
||||
timing time.Duration
|
||||
bytesOut uint64
|
||||
bytesIn uint64
|
||||
err error
|
||||
// Result represents the metrics we want out of an http.Response
|
||||
type Result struct {
|
||||
Code uint64
|
||||
Timestamp time.Time
|
||||
Timing time.Duration
|
||||
BytesOut uint64
|
||||
BytesIn uint64
|
||||
Error error
|
||||
}
|
||||
|
||||
// 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) {
|
||||
func drill(rate uint64, reqs chan *http.Request, res chan *Result) {
|
||||
throttle := time.Tick(time.Duration(1e9 / rate))
|
||||
for req := range reqs {
|
||||
<-throttle
|
||||
@@ -48,19 +48,19 @@ func drill(rate uint64, reqs chan *http.Request, res chan *result) {
|
||||
// hit executes the passed http.Request and puts a generated *result into res.
|
||||
// Both transport errors and unsucessfull requests (non {2xx,3xx}) are
|
||||
// considered errors which are set in the Response.
|
||||
func hit(req *http.Request, res chan *result) {
|
||||
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),
|
||||
err: err,
|
||||
result := &Result{
|
||||
Timestamp: began,
|
||||
Timing: time.Since(began),
|
||||
BytesOut: uint64(req.ContentLength),
|
||||
Error: err,
|
||||
}
|
||||
if err == nil {
|
||||
result.bytesIn, result.code = uint64(r.ContentLength), uint64(r.StatusCode)
|
||||
if body, err := ioutil.ReadAll(r.Body); err != nil && (result.code < 200 || result.code >= 300) {
|
||||
result.err = errors.New(string(body))
|
||||
result.BytesIn, result.Code = uint64(r.ContentLength), uint64(r.StatusCode)
|
||||
if body, err := ioutil.ReadAll(r.Body); err != nil && (result.Code < 200 || result.Code >= 300) {
|
||||
result.Error = errors.New(string(body))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user