-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
86 lines (81 loc) · 2.35 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
// Copyright 2020 The benchmark. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"runtime"
"strings"
)
var (
header = flag.String("h", "", "request header, split by \\r\\n")
method = flag.String("m", "GET", "request method")
timeout = flag.Int("t", 10000, "request/socket timeout in ms")
connectionNum = flag.Int("c", 1000, "number of connection")
requestNum = flag.Int("n", 100000, "number of request")
body = flag.String("b", "", "body of request")
cpu = flag.Int("cpu", 0, "number of cpu used")
host = flag.String("host", "", "host of request")
proxyUrl = flag.String("proxy", "", "proxy of request")
proxyTransport = flag.String("proxy-transport", "tcp", "proxy transport of request, \"tcp\" or \"quic\"")
quicProtocol = flag.String("quic-protocol", "h3", "tls application protocol of quic transport")
ignoreErr = flag.Bool("ignore-err", false, "`true` to ignore error when creating connection (default false)")
)
func main() {
flag.Parse()
if u, err := url.Parse(flag.Arg(0)); err != nil || u.Host == "" {
fmt.Printf("the request url %s is not correct \n", flag.Arg(0))
return
}
payload := strings.NewReader(*body)
req, err := http.NewRequest(*method, flag.Arg(0), payload)
if err != nil {
fmt.Println(err)
return
}
var target = req.Host
if *host != "" {
req.Host = *host
}
if *cpu > 0 {
runtime.GOMAXPROCS(*cpu)
}
if *header != "" {
for _, v := range strings.Split(*header, "\\r\\n") {
a := strings.Split(v, ":")
if len(a) == 2 {
req.Header.Set(strings.Trim(a[0], " "), strings.Trim(a[1], " "))
}
}
}
writeBytes, err := httputil.DumpRequest(req, true)
if err != nil {
fmt.Println(err)
return
}
if !strings.Contains(target, ":") {
if req.URL.Scheme == "http" {
target = target + ":80"
} else {
target = target + ":443"
}
}
p := &benchmark{
connectionNum: *connectionNum,
reqNum: int64(*requestNum),
requestBytes: writeBytes,
target: target,
schema: req.URL.Scheme,
timeout: *timeout,
reqConnList: make([]*ReqConn, 0),
proxy: *proxyUrl,
proxyTransport: *proxyTransport,
quicProtocol: *quicProtocol,
}
p.Run()
p.Print()
}