/
client.go
121 lines (100 loc) · 2.96 KB
/
client.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
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"flag"
"io/ioutil"
"log"
"strconv"
"time"
"github.com/myzhan/boomer"
"github.com/valyala/fasthttp"
)
var client *fasthttp.Client
var postBody []byte
var verbose bool
var method string
var url string
var timeout time.Duration
var postFile string
var contentType string
var disableKeepalive bool
func worker() {
req := fasthttp.AcquireRequest()
req.Header.SetMethod(method)
req.Header.SetContentType(contentType)
if disableKeepalive {
req.Header.SetConnectionClose()
}
req.SetRequestURI(url)
if postFile != "" {
req.SetBody(postBody)
}
resp := fasthttp.AcquireResponse()
startTime := time.Now()
err := client.DoTimeout(req, resp, timeout)
elapsed := time.Since(startTime)
if err != nil {
switch err {
case fasthttp.ErrTimeout:
boomer.RecordFailure("http", "timeout", elapsed.Nanoseconds()/int64(time.Millisecond), err.Error())
case fasthttp.ErrNoFreeConns:
// all Client.MaxConnsPerHost connections to the requested host are busy
// try to increase MaxConnsPerHost
boomer.RecordFailure("http", "connections all busy", elapsed.Nanoseconds()/int64(time.Millisecond), err.Error())
default:
boomer.RecordFailure("http", "unknown", elapsed.Nanoseconds()/int64(time.Millisecond), err.Error())
}
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
return
}
boomer.RecordSuccess("http", strconv.Itoa(resp.StatusCode()), elapsed.Nanoseconds()/int64(time.Millisecond), int64(len(resp.Body())))
if verbose {
log.Println(string(resp.Body()))
}
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
flag.StringVar(&method, "method", "GET", "HTTP method, one of GET, POST")
flag.StringVar(&url, "url", "", "URL")
flag.DurationVar(&timeout, "timeout", 10*time.Second, "HTTP request timeout")
flag.StringVar(&postFile, "post-file", "", "File containing data to POST. Remember also to set --content-type")
flag.StringVar(&contentType, "content-type", "text/plain", "Content-type header")
flag.BoolVar(&disableKeepalive, "disable-keepalive", false, "Disable keepalive")
flag.BoolVar(&verbose, "verbose", false, "Print debug log")
flag.Parse()
log.Printf(`Fasthttp benchmark is running with these args:
method: %s
url: %s
timeout: %v
post-file: %s
content-type: %s
disable-keepalive: %t
verbose: %t`, method, url, timeout, postFile, contentType, disableKeepalive, verbose)
if url == "" {
log.Fatalln("--url can't be empty string, please specify a URL that you want to test.")
}
if method != "GET" && method != "POST" {
log.Fatalln("HTTP method must be one of GET, POST.")
}
if method == "POST" {
if postFile == "" {
log.Fatalln("--post-file can't be empty string when method is POST")
}
tmp, err := ioutil.ReadFile(postFile)
if err != nil {
log.Fatalf("%v\n", err)
}
postBody = tmp
}
client = &fasthttp.Client{
MaxConnsPerHost: 2000,
}
task := &boomer.Task{
Name: "worker",
Weight: 10,
Fn: worker,
}
boomer.Run(task)
}