blob: ddb57fb1cd67e8a8df681bbbbaac82ca8173e2f8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package throttled
import (
"fmt"
"net/http"
"net/http/httptest"
"sync"
"time"
"github.com/PuerkitoBio/boom/commands"
)
type stats struct {
sync.Mutex
ok int
dropped int
ts []time.Time
body func()
}
func (s *stats) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if s.body != nil {
s.body()
}
s.Lock()
defer s.Unlock()
s.ts = append(s.ts, time.Now())
s.ok++
w.WriteHeader(200)
}
func (s *stats) DeniedHTTP(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()
s.dropped++
w.WriteHeader(deniedStatus)
}
func (s *stats) Stats() (int, int, []time.Time) {
s.Lock()
defer s.Unlock()
return s.ok, s.dropped, s.ts
}
func runTest(h http.Handler, b ...commands.Boom) []*commands.Report {
srv := httptest.NewServer(h)
defer srv.Close()
var rpts []*commands.Report
var wg sync.WaitGroup
var mu sync.Mutex
wg.Add(len(b))
for i, bo := range b {
bo.Req.Url = srv.URL + fmt.Sprintf("/%d", i)
go func(bo commands.Boom) {
mu.Lock()
defer mu.Unlock()
rpts = append(rpts, bo.Run())
wg.Done()
}(bo)
}
wg.Wait()
return rpts
}
|