-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (67 loc) · 1.79 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
)
func main() {
if len(os.Args) < 4 {
fmt.Println("Stresser usage: *url endpoint* *number of connections* *how long to run for in seconds*")
os.Exit(1)
}
url := os.Args[1]
threads, err := strconv.Atoi(os.Args[2])
check(err)
length, err := strconv.Atoi(os.Args[3])
check(err)
numRun := make(chan int)
numErr := make(chan int)
totalCon := 0
failedCon := 0
//Create a go routine for every thread asked for from command line
for i := 0; i < threads; i++ {
go stress(url, numRun, numErr)
}
//Set a timer to finish for the number of seconds passed in from command line
timer := time.After(time.Duration(time.Second * time.Duration(length)))
//Continually loop through this code receiving either successful connections or errors
//Also catches when the timer finishes and then returns out of the entire application
for {
select {
case count := <-numRun:
totalCon = count + totalCon
case count := <-numErr:
failedCon = count + failedCon
case <-timer:
log.Println("Finished stressing")
log.Println("Total connections:", totalCon)
log.Println("Total failures:", failedCon)
return
}
}
}
//check is a simple error checking helper function that makes it so we don't have the same code repeated throughout the app.
func check(err error) {
if err != nil {
log.Println(err)
os.Exit(1)
}
}
//stress infinitely loops while calling the url passed in.
//It also pushes a 1 to either the numRun channel or the numErr channel based on a successful query or not.
func stress(url string, numRun chan int, numErr chan int) {
//Infinitely call the url passed in
for {
resp, err := http.Get(url)
if err != nil {
log.Println(err.Error())
numErr <- 1
continue
}
numRun <- 1
resp.Body.Close()
}
}