-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhammer.go
189 lines (166 loc) · 4.39 KB
/
hammer.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"crypto/tls"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"runtime"
"runtime/pprof"
"strings"
"time"
)
// header field type
type hfield struct {
name string
value string
}
// header type: a slice of strings implementing the flag.Value interface.
type header []hfield
// String is the method to format the flag's value, part of the flag.Value interface.
func (h *header) String() string {
return fmt.Sprint(*h)
}
type errorString string
func (e errorString) Error() string {
return string(e)
}
// Set is the method to set the flag value, part of the flag.Value interface.
func (h *header) Set(value string) error {
i := strings.IndexRune(value, ':')
if i < 0 {
return errorString("Header field format must be `name: value'")
}
hf := hfield{value[0:i], value[i+1:]}
*h = append(*h, hf)
return nil
}
var ready_ch = make(chan bool)
var start_ch = make(chan bool)
var done_ch = make(chan bool)
func send_requests(client *http.Client, iter int, method string, url string, body string, hdr header, user string, pass string) {
var body_reader io.ReadSeeker
if 0 < len(body) {
body_reader = strings.NewReader(body)
}
req, err := http.NewRequest(method, url, body_reader)
if err != nil {
log.Println(err)
return
}
for _, hf := range hdr {
req.Header.Add(hf.name, hf.value)
}
if user != "" {
req.SetBasicAuth(user, pass)
}
// Tell main thread we are ready
ready_ch <- true
// Wait for main thread to start the injection
<-start_ch
var buf = make([]byte, 4096)
// Perform injection
for i := 0; i < iter; i++ {
if body_reader != nil {
_, err = body_reader.Seek(0, 0)
if err != nil {
log.Println(err)
break
}
}
resp, err := client.Do(req)
if err != nil {
log.Println(err)
break
}
for {
_, err = resp.Body.Read(buf)
if err != nil {
if err != io.EOF {
log.Println(err)
}
break
}
}
resp.Body.Close()
}
done_ch <- true
}
func main() {
// Command line parameters
var conc, reqs, cpus int
var ka, comp bool
var method, url, body, user, pass, cpuprof /*, memprof*/ string
var hdr header
flag.StringVar(&body, "body", "", "Request body")
flag.IntVar(&conc, "concurrency", 100, "Number of concurrent connections")
flag.IntVar(&cpus, "cpus", 2, "Number of CPUs/kernel threads used")
flag.StringVar(&cpuprof, "cpu-prof", "", "CPU profile file name (pprof format)")
flag.BoolVar(&comp, "compress", false, "Use HTTP compression")
flag.Var(&hdr, "header", "Additional request header (can be set multiple time)")
flag.BoolVar(&ka, "keep-alive", true, "Use HTTP keep-alive")
flag.StringVar(&pass, "pass", "", "HTTP authentication password")
//flag.StringVar(&memprof, "mem-prof", "", "Memory allocation profile file name (pprof format)")
flag.StringVar(&method, "method", "GET", "HTTP method (GET, POST, PUT, DELETE...)")
flag.IntVar(&reqs, "requests", 10000, "Total number of requests")
flag.StringVar(&url, "url", "http://127.0.0.1/", "URL")
flag.StringVar(&user, "user", "", "HTTP authentication user name")
flag.Parse()
// Use cpus kernel threads
runtime.GOMAXPROCS(cpus)
// Create HTTP client according to configuration
var transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true, CipherSuites: []uint16{tls.TLS_RSA_WITH_AES_128_CBC_SHA}},
DisableKeepAlives: !ka,
DisableCompression: !comp,
MaxIdleConnsPerHost: conc,
}
var client = &http.Client{
Transport: transport,
}
// Profiling
if cpuprof != "" {
f, err := os.Create(cpuprof)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// Create goroutines
remaining := reqs
for i := 0; i < conc; i++ {
n := remaining / (conc - i)
go send_requests(client, n, method, url, body, hdr, user, pass)
remaining -= n
}
// Wait for worker goroutines to get ready
for i := 0; i < conc; i++ {
<-ready_ch
}
begin := time.Now()
// Start sending requests
for i := 0; i < conc; i++ {
start_ch <- true
}
// Wait for jobs to complete
for i := 0; i < conc; i++ {
<-done_ch
}
end := time.Now()
elapsed := float32(end.Sub(begin))
throughput := float32(reqs) * 1000000000 / elapsed
fmt.Printf("%d requests sent in %.2f seconds - average throughput %.2f tps\n", reqs, elapsed/1000000000, throughput)
// Profiling
//if memprof != "" {
//f, err := os.Create(memprof)
//if err != nil {
//log.Fatal(err)
//}
//pprof.Lookup("heap").WriteTo(f, 0)
//f.Close()
//return
//}
}