-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
136 lines (111 loc) · 3.08 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"bytes"
"crypto/tls"
"flag"
"io"
"io/ioutil"
"log"
"net/http"
"strconv"
"time"
"github.com/myzhan/boomer"
)
// This is a tool like Apache Benchmark a.k.a "ab".
// It doesn't implement all the features supported by ab.
var client *http.Client
var postBody []byte
var verbose bool
var method string
var url string
var timeout int
var postFile string
var contentType string
var disableCompression bool
var disableKeepalive bool
func worker() {
request, err := http.NewRequest(method, url, bytes.NewBuffer(postBody))
if err != nil {
log.Fatalf("%v\n", err)
}
request.Header.Set("Content-Type", contentType)
startTime := time.Now()
response, err := client.Do(request)
elapsed := time.Since(startTime)
if err != nil {
if verbose {
log.Printf("%v\n", err)
}
boomer.RecordFailure("http", "error", 0.0, err.Error())
} else {
boomer.RecordSuccess("http", strconv.Itoa(response.StatusCode),
elapsed.Nanoseconds()/int64(time.Millisecond), response.ContentLength)
if verbose {
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Printf("%v\n", err)
} else {
log.Printf("Status Code: %d\n", response.StatusCode)
log.Println(string(body))
}
} else {
io.Copy(ioutil.Discard, response.Body)
}
response.Body.Close()
}
}
func main() {
flag.StringVar(&method, "method", "GET", "HTTP method, one of GET, POST")
flag.StringVar(&url, "url", "", "URL")
flag.IntVar(&timeout, "timeout", 10, "Seconds to max. wait for each response")
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(&disableCompression, "disable-compression", false, "Disable compression")
flag.BoolVar(&disableKeepalive, "disable-keepalive", false, "Disable keepalive")
flag.BoolVar(&verbose, "verbose", false, "Print debug log")
flag.Parse()
log.Printf(`HTTP benchmark is running with these args:
method: %s
url: %s
timeout: %d
post-file: %s
content-type: %s
disable-compression: %t
disable-keepalive: %t
verbose: %t`, method, url, timeout, postFile, contentType, disableCompression, 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
}
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = 2000
tr := &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
MaxIdleConnsPerHost: 2000,
DisableCompression: disableCompression,
DisableKeepAlives: disableKeepalive,
}
client = &http.Client{
Transport: tr,
Timeout: time.Duration(timeout) * time.Second,
}
task := &boomer.Task{
Name: "worker",
Weight: 10,
Fn: worker,
}
boomer.Run(task)
}