Decouple Reporters from Attack function

This changeset breaks the API of Attack in order to decouple Reporters
and the Attack function. Attack now returns a slice with
non-deterministic order of Results which one can use on the calling code
with or without Reporters, hence making it much more useful on a library
usage setting.
These developments could be of interest to issue #11 which was closed in
the past.
This commit is contained in:
Tomás Senart
2013-09-09 01:49:57 +01:00
parent bab3d9f89a
commit f689362188
7 changed files with 38 additions and 30 deletions

View File

@@ -9,20 +9,25 @@ import (
// 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 the rep Reporter.
func Attack(targets Targets, rate uint64, duration time.Duration, rep Reporter) {
hits := make(chan *http.Request, rate*uint64((duration).Seconds()))
defer close(hits)
results := make(chan *Result, cap(hits))
defer close(results)
go drill(rate, hits, results) // Attack!
// 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([]Result, total)
// Scatter
go drill(rate, hits, res)
for i := 0; i < cap(hits); i++ {
hits <- targets[i%len(targets)]
}
// Wait for all requests to finish
for i := 0; i < cap(results); i++ {
rep.add(<-results)
close(hits)
// Gather
for i := 0; i < cap(res); i++ {
results[i] = <-res
}
close(res)
return results
}
// Result represents the metrics we want out of an http.Response
@@ -37,7 +42,7 @@ type Result struct {
// 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
@@ -45,13 +50,13 @@ func drill(rate uint64, reqs chan *http.Request, res chan *Result) {
}
}
// hit executes the passed http.Request and puts a generated *result into 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 which are set in the Response.
func hit(req *http.Request, res chan *Result) {
// considered errors.
func hit(req *http.Request, res chan Result) {
began := time.Now()
r, err := http.DefaultClient.Do(req)
result := &Result{
result := Result{
Timestamp: began,
Timing: time.Since(began),
BytesOut: uint64(req.ContentLength),
@@ -63,6 +68,5 @@ func hit(req *http.Request, res chan *Result) {
result.Error = errors.New(string(body))
}
}
res <- result
}