Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
link1st committed Apr 8, 2024
1 parent 5a9450e commit a6108e3
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 34 deletions.
24 changes: 12 additions & 12 deletions model/curl_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,7 @@ func ParseTheFile(path string) (curl *CURL, err error) {
key = strings.Trim(key, "'")
curl.Data["curl"] = []string{key}
// 去除首尾空格
data = strings.TrimFunc(data, func(r rune) bool {
if r == ' ' || r == '\\' || r == '\n' {
return true
}
return false
})
data = removeSpaces(data)
continue
}
if strings.HasPrefix(data, "-") {
Expand All @@ -107,12 +102,7 @@ func ParseTheFile(path string) (curl *CURL, err error) {
data = ""
}
// 去除首尾空格
data = strings.TrimFunc(data, func(r rune) bool {
if r == ' ' || r == '\\' || r == '\n' {
return true
}
return false
})
data = removeSpaces(data)
if key == "" {
continue
}
Expand All @@ -121,6 +111,16 @@ func ParseTheFile(path string) (curl *CURL, err error) {
return
}

func removeSpaces(data string) string {
data = strings.TrimFunc(data, func(r rune) bool {
if r == ' ' || r == '\\' || r == '\n' {
return true
}
return false
})
return data
}

// String string
func (c *CURL) String() (url string) {
curlByte, _ := json.Marshal(c)
Expand Down
9 changes: 3 additions & 6 deletions server/client/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ func HTTPRequest(chanID uint64, request *model.Request) (resp *http.Response, re
return
}
req.Close = true
tr := &http.Transport{}
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
if request.HTTP2 {
// 使用真实证书 验证证书 模拟真实请求
tr = &http.Transport{
Expand All @@ -73,11 +75,6 @@ func HTTPRequest(chanID uint64, request *model.Request) (resp *http.Response, re
if err = http2.ConfigureTransport(tr); err != nil {
return
}
} else {
// 跳过证书验证
tr = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
client = &http.Client{
Transport: tr,
Expand Down
23 changes: 10 additions & 13 deletions server/client/http_longclinet/long_clinet.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ func setClient(i uint64, request *model.Request) *http.Client {

// createLangHTTPClient 初始化长连接客户端参数
func createLangHTTPClient(request *model.Request) *http.Client {
tr := &http.Transport{}
tr := &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 0, // 最大连接数,默认0无穷大
MaxIdleConnsPerHost: request.MaxCon, // 对每个host的最大连接数量(MaxIdleConnsPerHost<=MaxIdleConns)
IdleConnTimeout: 90 * time.Second, // 多长时间未使用自动关闭连接
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
if request.HTTP2 {
// 使用真实证书 验证证书 模拟真实请求
tr = &http.Transport{
Expand All @@ -57,18 +66,6 @@ func createLangHTTPClient(request *model.Request) *http.Client {
TLSClientConfig: &tls.Config{InsecureSkipVerify: false},
}
_ = http2.ConfigureTransport(tr)
} else {
// 跳过证书验证
tr = &http.Transport{
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 0, // 最大连接数,默认0无穷大
MaxIdleConnsPerHost: request.MaxCon, // 对每个host的最大连接数量(MaxIdleConnsPerHost<=MaxIdleConns)
IdleConnTimeout: 90 * time.Second, // 多长时间未使用自动关闭连接
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
return &http.Client{
Transport: tr,
Expand Down
2 changes: 1 addition & 1 deletion server/golink/http_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func sendList(chanID uint64, requestList []*model.Request) (isSucceed bool, errC
func send(chanID uint64, request *model.Request) (bool, int, uint64, int64) {
var (
isSucceed = false
errCode = model.HTTPOk
errCode int
body []byte
contentLength = int64(0)
err error
Expand Down
2 changes: 1 addition & 1 deletion server/golink/websocket_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func webSocketRequest(chanID uint64, ch chan<- *model.RequestResults, i uint64,
var (
startTime = time.Now()
isSucceed = false
errCode = model.HTTPOk
errCode int
msg []byte
)
// 需要发送的数据
Expand Down
2 changes: 1 addition & 1 deletion server/statistics/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func printTop(requestTimeList []uint64) {
if requestTimeList == nil {
return
}
all := tools.Uint64List{}
var all tools.Uint64List
all = requestTimeList
sort.Sort(all)
fmt.Println("tp90:", fmt.Sprintf("%.3f", float64(all[int(float64(len(all))*0.90)]/1e6)))
Expand Down

0 comments on commit a6108e3

Please sign in to comment.