Simplify Reporter objects to be simple functions

This commit is contained in:
Tomás Senart
2013-09-09 03:03:13 +01:00
parent f689362188
commit 93310995c7
6 changed files with 118 additions and 185 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors"
"io/ioutil"
"net/http"
"sort"
"time"
)
@@ -14,7 +15,7 @@ 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)
results := make(Results, total)
// Scatter
go drill(rate, hits, res)
for i := 0; i < cap(hits); i++ {
@@ -27,6 +28,8 @@ func Attack(targets Targets, rate uint64, duration time.Duration) []Result {
}
close(res)
sort.Sort(results)
return results
}
@@ -40,6 +43,13 @@ type Result struct {
Error error
}
// 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) {