forked from mrichman/hargo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.go
More file actions
164 lines (142 loc) · 3.82 KB
/
load.go
File metadata and controls
164 lines (142 loc) · 3.82 KB
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
package hargo
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"net/http/cookiejar"
"net/url"
"os"
"time"
log "github.com/sirupsen/logrus"
)
// LoadTest executes all HTTP requests in order concurrently
// for a given number of workers.
func LoadTest(harfile string, file *os.File, hc HarConfig, isSequence bool, workers int, timeout time.Duration, u url.URL, ignoreHarCookies bool, insecureSkipVerify bool) error {
log.Infof("Starting load test with %d workers. Duration %v.", workers, timeout)
results := make(chan TestResult)
defer close(results)
stop := make(chan bool)
entries := make(chan Entry, workers)
go ReadStream(file, entries, stop, isSequence)
// if a InfluxDB URL is given the metrics will be written to that instance
// if not the dummy consumer is initiated.
if (url.URL{}) != u {
go WritePoint(u, results)
} else {
go func(results chan TestResult) {
for {
<-results
}
}(results)
}
go wait(stop, timeout, workers)
if isSequence {
for i := 0; i < workers; i++ {
go processEntries(harfile, hc, i, entries, results, ignoreHarCookies, insecureSkipVerify, stop)
}
<-stop
} else {
loop:
for {
select {
case entry := <-entries:
for i := 0; i < workers; i++ {
ch := make(chan Entry)
go func(en Entry) {
loop:
for {
select {
default:
ch <- en
case <-stop:
break loop
}
}
}(entry)
go processEntries(harfile, hc, i, ch, results, ignoreHarCookies, insecureSkipVerify, stop)
}
case <-stop:
break loop
}
}
}
fmt.Printf("\nTimeout of %.1fs elapsed. Terminating load test.\n", timeout.Seconds())
return nil
}
// wait will close the stop chan when the timeout is hit.
func wait(stop chan bool, timeout time.Duration, workers int) {
time.Sleep(timeout)
close(stop)
}
func processEntries(harfile string, hc HarConfig, worker int, entries chan Entry, results chan TestResult, ignoreHarCookies bool, insecureSkipVerify bool, stop chan bool) {
jar, _ := cookiejar.New(nil)
httpClient := http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify},
TLSHandshakeTimeout: 10 * time.Second,
ResponseHeaderTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
CheckRedirect: func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
Jar: jar,
}
iter := 0
loop:
for {
select {
case <-stop:
break loop
case entry := <-entries:
msg := fmt.Sprintf("[%d,%d] %s", worker, iter, entry.Request.URL)
req, err := EntryToRequest(&entry, hc, ignoreHarCookies)
check(err)
jar.SetCookies(req.URL, req.Cookies())
startTime := time.Now()
resp, err := httpClient.Do(req)
endTime := time.Now()
latency := int(endTime.Sub(startTime) / time.Millisecond)
method := req.Method
if err != nil {
log.Error(err)
log.Error(entry)
tr := TestResult{
URL: req.URL.String(),
URLShort: hc.ReplaceAlias(req.URL.RequestURI()),
Status: 0,
StartTime: startTime,
EndTime: endTime,
Latency: latency,
Method: method,
HarFile: harfile}
results <- tr
continue
}
if resp != nil {
resp.Body.Close()
}
msg += fmt.Sprintf(" %d %dms", resp.StatusCode, latency)
log.Infoln(msg)
tr := TestResult{
URL: req.URL.String(),
URLShort: hc.ReplaceAlias(req.URL.RequestURI()),
Status: resp.StatusCode,
StartTime: startTime,
EndTime: endTime,
Latency: latency,
Method: method,
Size: int64(entry.Request.HeaderSize + entry.Request.BodySize + entry.Response.HeadersSize + entry.Response.BodySize),
HarFile: harfile,
}
results <- tr
}
iter++
}
}