-
Notifications
You must be signed in to change notification settings - Fork 0
/
excecutor.go
112 lines (91 loc) · 3.09 KB
/
excecutor.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
// RequestInfo contains the information needed to build the request
type RequestInfo struct {
url string
contentType string
headerFilePath string
postFilePath string
}
// RequestExecutor will execute a request
type RequestExecutor interface {
execute(requestInfo RequestInfo) (*http.Response, error)
}
// GetRequestExecutor will execute a GET request
type GetRequestExecutor struct{}
// PostRequestExecutor will execute a POST request
type PostRequestExecutor struct{}
func (getRequestExecutor GetRequestExecutor) execute(requestInfo RequestInfo) (*http.Response, error) {
return getSingle(requestInfo.url, requestInfo.headerFilePath)
}
func (postRequestExecutor PostRequestExecutor) execute(requestInfo RequestInfo) (*http.Response, error) {
return postSingle(requestInfo.url, requestInfo.contentType, requestInfo.postFilePath, false, false, requestInfo.headerFilePath)
}
func executeN(count int, requestInfo RequestInfo, requestExecutor RequestExecutor, callStatistic chan<- CallStatistic) {
success, failure := 0, 0
start := time.Now()
for i := 0; i < count; i++ {
resp, err := requestExecutor.execute(requestInfo)
if err != nil {
failure++
continue
}
if resp.StatusCode == http.StatusOK {
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
failure++
continue
}
if len(bodyBytes) >= 0 {
success++
}
}
}
callStatistic <- CallStatistic{success, failure, time.Now().Sub(start).Nanoseconds() / 1000.0 / 1000.0}
}
func createRequestExecutor(opt Options) RequestExecutor {
if opt.post {
return PostRequestExecutor{}
}
return GetRequestExecutor{}
}
func createRequestInfo(url string, opt Options) RequestInfo {
return RequestInfo{
url: url,
contentType: opt.contentType,
headerFilePath: opt.headerFilePath,
postFilePath: opt.postFilePath,
}
}
func concurrentExecute(url string, opt Options) {
remain := opt.number % opt.concurrent
requestExecutor := createRequestExecutor(opt)
requestInfo := createRequestInfo(url, opt)
callStatisticChan := make(chan CallStatistic)
t1 := time.Now()
for i := 0; i < opt.concurrent; i++ {
count := opt.number / opt.concurrent
if i < remain {
count++
}
go executeN(count, requestInfo, requestExecutor, callStatisticChan)
}
totalSuccess, totalFailure, sumTimeMilliSeconds := 0, 0, int64(0)
callStatistic := <-callStatisticChan
for i := 0; i < opt.concurrent; i++ {
totalSuccess += callStatistic.successCount
totalFailure += callStatistic.failureCount
sumTimeMilliSeconds += callStatistic.timeMilliSeconds
}
totalTimeMilliSeconds := time.Now().Sub(t1).Nanoseconds() / 1000.0 / 1000.0
avgTimeMilliSeconds := sumTimeMilliSeconds / int64(opt.number)
qps := int64(opt.number) * 1000.0 / totalTimeMilliSeconds
successRatio := totalSuccess * 100.0 / opt.number
fmt.Println("concurrent=", opt.concurrent, ",totalSuccess=", totalSuccess, ", totalFailure=", totalFailure, ", success ratio=", successRatio, "%")
fmt.Println("total time(ms)=", totalTimeMilliSeconds, ", qps=", qps, ", avgTime(ms)=", avgTimeMilliSeconds)
}