-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (87 loc) · 2.15 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"flag"
"fmt"
"net/http"
"strings"
"sync"
"time"
)
var wg sync.WaitGroup
var THREADS int
var URL string
var TIMEOUT int
var TOTALHITREQUEST int
var respCount map[int]int
var timeTaken []float64
var mux sync.Mutex
var choices = []byte{'-', '\\', '|', '/'}
func init() {
flag.StringVar(&URL, "u", "https://google.com", "URL")
flag.IntVar(&THREADS, "t", 4, "Number of threads.")
flag.IntVar(&TIMEOUT, "s", 10, "Time to Run")
flag.Parse()
fmt.Printf("%10s: %10s\n", "URL", URL)
fmt.Printf("%10s: %10d\n", "Threads", THREADS)
fmt.Printf("%10s: %10d\n", "Time", TIMEOUT)
}
func requester() {
for {
start := time.Now()
resp, _ := http.Get(URL)
end := time.Since(start)
mux.Lock()
if resp != nil {
respCount[resp.StatusCode]++
} else {
respCount[0]++
}
mux.Unlock()
TOTALHITREQUEST++
timeTaken = append(timeTaken, end.Seconds())
fmt.Printf("[%c]: Processing...\r", choices[TOTALHITREQUEST%4])
}
}
func main() {
timeTaken = make([]float64, 0)
respCount = make(map[int]int)
wg.Add(1)
for i := 0; i < THREADS; i++ {
go requester()
}
timer := time.NewTimer(time.Duration(TIMEOUT) * time.Second)
for {
select {
case <-timer.C:
fmt.Print("\r\n")
fmt.Println(strings.Repeat("-", 30))
fmt.Printf("|%15s | %10s|\n", "Status Code", "Hits")
fmt.Println(strings.Repeat("-", 30))
for k, v := range respCount {
fmt.Printf("|%15d | %10d|\n", k, v)
}
fmt.Println(strings.Repeat("-", 30))
var maxTime, minTime, avgTime float64
minTime = timeTaken[0]
for _, v := range timeTaken {
if v > maxTime {
maxTime = v
} else if v < minTime {
minTime = v
}
avgTime += v
}
avgTime = avgTime / float64(len(timeTaken))
fmt.Println(strings.Repeat("-", 30))
fmt.Printf("|%-15s | %10d|\n", "Total Hits", TOTALHITREQUEST)
fmt.Println(strings.Repeat("-", 30))
fmt.Printf("|%-15s | %10.2f|\n", "Max Time", maxTime)
fmt.Println(strings.Repeat("-", 30))
fmt.Printf("|%-15s | %10.2f|\n", "Min Time", minTime)
fmt.Println(strings.Repeat("-", 30))
fmt.Printf("|%-15s | %10.2f|\n", "Average Time", avgTime)
fmt.Println(strings.Repeat("-", 30))
return
}
}
}