-
Notifications
You must be signed in to change notification settings - Fork 0
/
humot.go
83 lines (75 loc) · 1.61 KB
/
humot.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
package main
import (
"fmt"
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
)
const TICK_MS = 2000
var transport = &http.Transport{
DisableCompression: true,
Dial: (&net.Dialer{
Timeout: 10000 * time.Millisecond,
KeepAlive: 30000 * time.Millisecond,
}).Dial,
TLSHandshakeTimeout: 10000 * time.Millisecond,
}
var httpClient = &http.Client{Transport: transport}
var errorMsg = regexp.MustCompilePOSIX(`[^:]*$`)
func humot(url string, responses chan<- string) {
for {
res, err := httpClient.Get(url)
if err == nil {
responses <- strconv.Itoa(res.StatusCode)
res.Body.Close()
} else {
errStr := strings.TrimSpace(errorMsg.FindString(err.Error()))
if errStr == "" {
errStr = err.Error()
}
responses <- "Error(" + errStr + ")"
}
}
}
func main() {
URL := ""
if len(os.Args) > 1 {
URL = os.Args[1]
} else {
fmt.Println("Usage: humot <url> [<concurrency>]")
os.Exit(0)
}
CONCURRENCY := 250
if len(os.Args) > 2 {
arg2, err := strconv.Atoi(os.Args[2])
if err == nil {
CONCURRENCY = arg2
}
}
fmt.Printf("Hitting %s with %d concurrent requests ... (^C to stop)\n", URL, CONCURRENCY)
responses := make(chan string, CONCURRENCY)
for i := 1; i <= CONCURRENCY; i++ {
go humot(URL, responses)
}
statusCount := make(map[string]int)
total := 0
ticker := time.NewTicker(time.Millisecond * TICK_MS).C
startTime := time.Now()
for {
select {
case s := <-responses:
statusCount[s]++
total++
case t := <-ticker:
fmt.Print(t.Sub(startTime))
for k, v := range statusCount {
fmt.Printf(" | %v: %v", k, v)
}
fmt.Println(" | Total: ", total)
}
}
}