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

@@ -97,17 +97,23 @@ import (
vegeta "github.com/tsenart/vegeta/lib" vegeta "github.com/tsenart/vegeta/lib"
"time" "time"
"os" "os"
"fmt"
) )
func main() { func main() {
targets, _ := vegeta.NewTargets([]string{"GET http://localhost:9100/"}) targets, _ := vegeta.NewTargets([]string{"GET http://localhost:9100/"})
rate := uint64(100) // per second rate := uint64(100) // per second
duration := 4 * time.Second duration := 4 * time.Second
reporter := vegeta.NewTextReporter()
vegeta.Attack(targets, rate, duration, reporter) results := vegeta.Attack(targets, rate, duration)
reporter.Report(os.Stdout) totalTime := time.Duration(0)
for _, result := range results {
totalTime += result.Timing
}
meanTime := time.Duration(float64(totalTime) / float64(len(results)))
fmt.Printf("Average timing: %s", meanTime)
} }
``` ```

View File

@@ -9,20 +9,25 @@ import (
// Attack hits the passed Targets (http.Requests) at the rate specified for // Attack hits the passed Targets (http.Requests) at the rate specified for
// duration time and then waits for all the requests to come back. // duration time and then waits for all the requests to come back.
// The results of the attack are put into the rep Reporter. // The results of the attack are put into a slice which is returned.
func Attack(targets Targets, rate uint64, duration time.Duration, rep Reporter) { func Attack(targets Targets, rate uint64, duration time.Duration) []Result {
hits := make(chan *http.Request, rate*uint64((duration).Seconds())) total := rate * uint64(duration.Seconds())
defer close(hits) hits := make(chan *http.Request, total)
results := make(chan *Result, cap(hits)) res := make(chan Result, total)
defer close(results) results := make([]Result, total)
go drill(rate, hits, results) // Attack! // Scatter
go drill(rate, hits, res)
for i := 0; i < cap(hits); i++ { for i := 0; i < cap(hits); i++ {
hits <- targets[i%len(targets)] hits <- targets[i%len(targets)]
} }
// Wait for all requests to finish close(hits)
for i := 0; i < cap(results); i++ { // Gather
rep.add(<-results) 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 // 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. // drill loops over the passed reqs channel and executes each request.
// It is throttled to the rate specified. // 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)) throttle := time.Tick(time.Duration(1e9 / rate))
for req := range reqs { for req := range reqs {
<-throttle <-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 // Both transport errors and unsucessfull requests (non {2xx,3xx}) are
// considered errors which are set in the Response. // considered errors.
func hit(req *http.Request, res chan *Result) { func hit(req *http.Request, res chan Result) {
began := time.Now() began := time.Now()
r, err := http.DefaultClient.Do(req) r, err := http.DefaultClient.Do(req)
result := &Result{ result := Result{
Timestamp: began, Timestamp: began,
Timing: time.Since(began), Timing: time.Since(began),
BytesOut: uint64(req.ContentLength), BytesOut: uint64(req.ContentLength),
@@ -63,6 +68,5 @@ func hit(req *http.Request, res chan *Result) {
result.Error = errors.New(string(body)) result.Error = errors.New(string(body))
} }
} }
res <- result res <- result
} }

View File

@@ -3,7 +3,6 @@ package vegeta
import ( import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time" "time"
@@ -18,10 +17,8 @@ func TestAttackRate(t *testing.T) {
) )
request, _ := http.NewRequest("GET", server.URL, nil) request, _ := http.NewRequest("GET", server.URL, nil)
rate := uint64(5000) rate := uint64(5000)
rep := NewTextReporter() Attack(Targets{request}, rate, 1*time.Second)
Attack(Targets{request}, rate, 1*time.Second, rep)
if hits := atomic.LoadUint64(&hitCount); hits != rate { if hits := atomic.LoadUint64(&hitCount); hits != rate {
rep.Report(os.Stdout)
t.Fatalf("Wrong number of hits: want %d, got %d\n", rate, hits) t.Fatalf("Wrong number of hits: want %d, got %d\n", rate, hits)
} }
} }

View File

@@ -7,5 +7,5 @@ import (
// Reporter represents any reporter of the results of the test // Reporter represents any reporter of the results of the test
type Reporter interface { type Reporter interface {
Report(io.Writer) error Report(io.Writer) error
add(res *Result) Add(res *Result)
} }

View File

@@ -69,8 +69,8 @@ func (r *TextReporter) Report(out io.Writer) error {
return w.Flush() return w.Flush()
} }
// add adds a response to be used in the report // Add adds a response to be used in the report
// Order of arrival is not relevant for this reporter // Order of arrival is not relevant for this reporter
func (r *TextReporter) add(res *Result) { func (r *TextReporter) Add(res *Result) {
r.results = append(r.results, res) r.results = append(r.results, res)
} }

View File

@@ -20,8 +20,8 @@ func NewTimingsPlotReporter() *TimingsPlotReporter {
return &TimingsPlotReporter{results: list.New()} return &TimingsPlotReporter{results: list.New()}
} }
// add inserts response to be used in the report, sorted by timestamp. // Add inserts response to be used in the report, sorted by timestamp.
func (r *TimingsPlotReporter) add(res *Result) { func (r *TimingsPlotReporter) Add(res *Result) {
// Empty list // Empty list
if r.results.Len() == 0 { if r.results.Len() == 0 {
r.results.PushFront(res) r.results.PushFront(res)

View File

@@ -94,9 +94,10 @@ func run(rate uint64, duration time.Duration, targetsf, ordering, reporter, outp
} }
log.Printf("Vegeta is attacking %d targets in %s order for %s...\n", len(targets), ordering, duration) log.Printf("Vegeta is attacking %d targets in %s order for %s...\n", len(targets), ordering, duration)
vegeta.Attack(targets, rate, duration, rep) for _, result := range vegeta.Attack(targets, rate, duration) {
rep.Add(&result)
}
log.Println("Done!") log.Println("Done!")
log.Printf("Writing report to '%s'...", output) log.Printf("Writing report to '%s'...", output)
if err = rep.Report(out); err != nil { if err = rep.Report(out); err != nil {
return fmt.Errorf(errReportingPrefix+"%s", err) return fmt.Errorf(errReportingPrefix+"%s", err)