-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
106 lines (91 loc) · 2.57 KB
/
main.go
File metadata and controls
106 lines (91 loc) · 2.57 KB
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
package main
import (
"log"
"flag"
"time"
"net"
"net/http"
"sync/atomic"
"io/ioutil"
)
var defReq = "https://google.com"
var ops uint32 = 0
type DialerFunc func(net, addr string) (net.Conn, error)
func make_dialer(keepAlive bool) DialerFunc {
return func(network, addr string) (net.Conn, error) {
conn, err := (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 3000 * time.Second,
}).Dial(network, addr)
if err != nil {
return conn, err
}
if !keepAlive {
conn.(*net.TCPConn).SetLinger(0)
}
return conn, err
}
}
func sendRequest(client *http.Client, req string, concurrency int) {
resp, err := client.Get(req)
if err != nil {
log.Println(err)
} else {
_, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Println(err)
}
atomic.AddUint32(&ops, 1)
}
}
func main() {
concurrency := flag.Int("concurrency", 10, "Concurent connection to the server")
maxQPS := flag.Int("maxQPS", 1000, "Maximum QPS the client will generate")
req := flag.String("req", defReq, "Request url")
keepAlive := flag.Bool("keepAlive", true, "Whether to keep connection alive, if enabled keep alive for 5 min")
flag.Parse()
log.Printf("Current concurrency: %d\n", *concurrency)
log.Printf("Current max QPS: %d\n", *maxQPS)
log.Printf("KeepAlive: %v\n", *keepAlive)
log.Printf("Url: %s\n", *req)
fin := make(chan bool)
bucket := make(chan bool, *maxQPS)
go func() {
// QPS calc
for {
currentOps := ops;
time.Sleep(time.Second)
var qps int32 = (int32)(ops - currentOps);
log.Printf("QPS: %d\n", qps)
}
}()
go func() {
for {
for i := 0; i < *maxQPS; i++ {
select {
case bucket <- true:
default:
}
}
time.Sleep(time.Second)
}
}()
for i := 0; i < *concurrency; i++ {
go func() {
tr := &http.Transport{
Dial: make_dialer(*keepAlive),
TLSHandshakeTimeout: 10 * time.Second,
DisableKeepAlives: !(*keepAlive),
MaxIdleConnsPerHost: *concurrency,
}
client := &http.Client{Transport: tr}
for {
<- bucket
sendRequest(client, *req, *concurrency)
}
}()
}
// make it never end
<- fin
}