-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
84 lines (74 loc) · 2.17 KB
/
main.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"flag"
"io"
"net/http"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
func fulfillRequest(url string) int {
log.Info("Making requests to:" + url)
resp, err := http.Get(url)
defer resp.Body.Close()
buf := new(strings.Builder)
w, _ := io.Copy(buf, resp.Body)
if err != nil {
log.Errorf("Finished with error: %v", err)
return 0
} else if resp.StatusCode > 400 {
log.Errorf("Finished with status code over 400, code was: %d", resp.StatusCode)
return 0
}
log.Infof("Recieved status code: %d when call url: %s with %d bytes", resp.StatusCode, url, w)
if debug {
log.Info("response data:" + buf.String())
}
return 1
}
func addFixedRequests(reqCh chan int, maxRequests int) {
log.Infof("Adding %d requests", maxRequests)
completedRequests := 0
for completedRequests < maxRequests {
reqCh <- 1
completedRequests++
}
}
func addInifiniteRequests(reqCh chan int, maxRequests int, d time.Duration) {
ticker := time.NewTicker(d)
completedRequests := 0
for range ticker.C {
reqCh <- 1
completedRequests++
if maxRequests > 0 && completedRequests > maxRequests {
ticker.Stop()
}
}
}
var (
debug bool
url string
maxRequests, concurrency int
requestInterval time.Duration
)
func main() {
flag.IntVar(&concurrency, "concurrency", 1, "how many requests to concurrently run")
flag.StringVar(&url, "url", "http://localhost:8000", "host name")
flag.IntVar(&maxRequests, "max-requests", 0, "Total number of requests. A value of 0 is infinite max requests")
flag.DurationVar(&requestInterval, "request-interval", 0, "Time in millisconds between requests added to the channel")
flag.BoolVar(&debug, "debug", false, "Should we display debug info on the calls")
flag.Parse()
log.Info("Traffic Generator started...")
reqCh := make(chan int, concurrency)
if maxRequests > 0 && requestInterval == 0 {
go addFixedRequests(reqCh, maxRequests)
} else if requestInterval > 0 {
go addInifiniteRequests(reqCh, maxRequests, requestInterval)
} else {
go addInifiniteRequests(reqCh, maxRequests, time.Millisecond*10)
}
log.Info("Looping through requests")
for range reqCh {
fulfillRequest(url)
}
}