Parameterize rate in Drill
This commit is contained in:
16
client.go
16
client.go
@@ -9,10 +9,7 @@ import (
|
|||||||
|
|
||||||
// Client is an http.Client with rate limiting
|
// Client is an http.Client with rate limiting
|
||||||
// TODO: Add timeouts
|
// TODO: Add timeouts
|
||||||
type Client struct {
|
type Client struct{ http.Client }
|
||||||
http.Client
|
|
||||||
rate uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// Response represents the metrics we want out of an http.Response
|
// Response represents the metrics we want out of an http.Response
|
||||||
type Response struct {
|
type Response struct {
|
||||||
@@ -24,15 +21,10 @@ type Response struct {
|
|||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient returns an initialized Client
|
|
||||||
func NewClient(rate uint64) *Client {
|
|
||||||
return &Client{http.Client{}, rate}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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 in the initializer
|
// It is throttled to the rate specified
|
||||||
func (c *Client) Drill(reqs chan *http.Request, res chan *Response) {
|
func (c *Client) Drill(rate uint64, reqs chan *http.Request, res chan *Response) {
|
||||||
throttle := time.Tick(time.Duration(1e9 / c.rate))
|
throttle := time.Tick(time.Duration(1e9 / rate))
|
||||||
for req := range reqs {
|
for req := range reqs {
|
||||||
<-throttle
|
<-throttle
|
||||||
go c.Do(req, res)
|
go c.Do(req, res)
|
||||||
|
|||||||
4
main.go
4
main.go
@@ -89,8 +89,8 @@ func attack(targets Targets, ordering string, rate uint64, duration time.Duratio
|
|||||||
defer close(hits)
|
defer close(hits)
|
||||||
responses := make(chan *Response, cap(hits))
|
responses := make(chan *Response, cap(hits))
|
||||||
defer close(responses)
|
defer close(responses)
|
||||||
client := NewClient(rate)
|
client := Client{}
|
||||||
go client.Drill(hits, responses) // Attack!
|
go client.Drill(rate, hits, responses) // Attack!
|
||||||
for i := 0; i < cap(hits); i++ {
|
for i := 0; i < cap(hits); i++ {
|
||||||
hits <- targets[i%len(targets)]
|
hits <- targets[i%len(targets)]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,10 +21,10 @@ func TestAttackRate(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
rate := uint(5000)
|
rate := uint64(5000)
|
||||||
rep := NewTextReporter()
|
rep := NewTextReporter()
|
||||||
attack(targets, "random", rate, 1*time.Second, rep)
|
attack(targets, "random", rate, 1*time.Second, rep)
|
||||||
if hits := atomic.LoadUint64(&hitCount); uint(hits) != rate {
|
if hits := atomic.LoadUint64(&hitCount); hits != rate {
|
||||||
rep.Report(os.Stdout)
|
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)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user