forked from tsliwowicz/go-wrk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go-wrk.go
168 lines (147 loc) · 5.48 KB
/
go-wrk.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"runtime"
"strings"
"time"
"github.com/tsliwowicz/go-wrk/loader"
"github.com/tsliwowicz/go-wrk/util"
)
const APP_VERSION = "0.9"
//default that can be overridden from the command line
var versionFlag bool = false
var helpFlag bool = false
var duration int = 10 //seconds
var goroutines int = 2
var testUrl string
var method string = "GET"
var host string
var headerFlags util.HeaderList
var header map[string]string
var statsAggregator chan *loader.RequesterStats
var timeoutms int
var allowRedirectsFlag bool = false
var disableCompression bool
var disableKeepAlive bool
var skipVerify bool
var playbackFile string
var reqBody string
var clientCert string
var clientKey string
var caCert string
var http2 bool
func init() {
flag.BoolVar(&versionFlag, "v", false, "Print version details")
flag.BoolVar(&allowRedirectsFlag, "redir", false, "Allow Redirects")
flag.BoolVar(&helpFlag, "help", false, "Print help")
flag.BoolVar(&disableCompression, "no-c", false, "Disable Compression - Prevents sending the \"Accept-Encoding: gzip\" header")
flag.BoolVar(&disableKeepAlive, "no-ka", false, "Disable KeepAlive - prevents re-use of TCP connections between different HTTP requests")
flag.BoolVar(&skipVerify, "no-vr", false, "Skip verifying SSL certificate of the server")
flag.IntVar(&goroutines, "c", 10, "Number of goroutines to use (concurrent connections)")
flag.IntVar(&duration, "d", 10, "Duration of test in seconds")
flag.IntVar(&timeoutms, "T", 1000, "Socket/request timeout in ms")
flag.StringVar(&method, "M", "GET", "HTTP method")
flag.StringVar(&host, "host", "", "Host Header")
flag.Var(&headerFlags, "H", "Header to add to each request (you can define multiple -H flags)")
flag.StringVar(&playbackFile, "f", "<empty>", "Playback file name")
flag.StringVar(&reqBody, "body", "", "request body string or @filename")
flag.StringVar(&clientCert, "cert", "", "CA certificate file to verify peer against (SSL/TLS)")
flag.StringVar(&clientKey, "key", "", "Private key file name (SSL/TLS")
flag.StringVar(&caCert, "ca", "", "CA file to verify peer against (SSL/TLS)")
flag.BoolVar(&http2, "http", true, "Use HTTP/2")
}
//printDefaults a nicer format for the defaults
func printDefaults() {
fmt.Println("Usage: go-wrk <options> <url>")
fmt.Println("Options:")
flag.VisitAll(func(flag *flag.Flag) {
fmt.Println("\t-"+flag.Name, "\t", flag.Usage, "(Default "+flag.DefValue+")")
})
}
func main() {
//raising the limits. Some performance gains were achieved with the + goroutines (not a lot).
runtime.GOMAXPROCS(runtime.NumCPU() + goroutines)
statsAggregator = make(chan *loader.RequesterStats, goroutines)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
flag.Parse() // Scan the arguments list
header = make(map[string]string)
if headerFlags != nil {
for _, hdr := range headerFlags {
hp := strings.SplitN(hdr, ":", 2)
header[hp[0]] = hp[1]
}
}
if playbackFile != "<empty>" {
file, err := os.Open(playbackFile) // For read access.
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
url, err := ioutil.ReadAll(file)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
testUrl = string(url)
} else {
testUrl = flag.Arg(0)
}
if versionFlag {
fmt.Println("Version:", APP_VERSION)
return
} else if helpFlag || len(testUrl) == 0 {
printDefaults()
return
}
fmt.Printf("Running %vs test @ %v\n %v goroutine(s) running concurrently\n", duration, testUrl, goroutines)
if len(reqBody) > 0 && reqBody[0] == '@' {
bodyFilename := reqBody[1:]
data, err := ioutil.ReadFile(bodyFilename)
if err != nil {
fmt.Println(fmt.Errorf("could not read file %q: %v", bodyFilename, err))
os.Exit(1)
}
reqBody = string(data)
}
loadGen := loader.NewLoadCfg(duration, goroutines, testUrl, reqBody, method, host, header, statsAggregator, timeoutms,
allowRedirectsFlag, disableCompression, disableKeepAlive, skipVerify, clientCert, clientKey, caCert, http2)
for i := 0; i < goroutines; i++ {
go loadGen.RunSingleLoadSession()
}
responders := 0
aggStats := loader.RequesterStats{MinRequestTime: time.Minute}
for responders < goroutines {
select {
case <-sigChan:
loadGen.Stop()
fmt.Printf("stopping...\n")
case stats := <-statsAggregator:
aggStats.NumErrs += stats.NumErrs
aggStats.NumRequests += stats.NumRequests
aggStats.TotRespSize += stats.TotRespSize
aggStats.TotDuration += stats.TotDuration
aggStats.MaxRequestTime = util.MaxDuration(aggStats.MaxRequestTime, stats.MaxRequestTime)
aggStats.MinRequestTime = util.MinDuration(aggStats.MinRequestTime, stats.MinRequestTime)
responders++
}
}
if aggStats.NumRequests == 0 {
fmt.Println("Error: No statistics collected / no requests found\n")
return
}
avgThreadDur := aggStats.TotDuration / time.Duration(responders) //need to average the aggregated duration
reqRate := float64(aggStats.NumRequests) / avgThreadDur.Seconds()
avgReqTime := aggStats.TotDuration / time.Duration(aggStats.NumRequests)
bytesRate := float64(aggStats.TotRespSize) / avgThreadDur.Seconds()
fmt.Printf("%v requests in %v, %v read\n", aggStats.NumRequests, avgThreadDur, util.ByteSize{float64(aggStats.TotRespSize)})
fmt.Printf("Requests/sec:\t\t%.2f\nTransfer/sec:\t\t%v\nAvg Req Time:\t\t%v\n", reqRate, util.ByteSize{bytesRate}, avgReqTime)
fmt.Printf("Fastest Request:\t%v\n", aggStats.MinRequestTime)
fmt.Printf("Slowest Request:\t%v\n", aggStats.MaxRequestTime)
fmt.Printf("Number of Errors:\t%v\n", aggStats.NumErrs)
}