1 Star 0 Fork 0

JoyAllen/config-selector

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
hey.go 2.65 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"fmt"
"github.com/kingdo777/hey/requester"
"math"
"net/http"
gourl "net/url"
"os"
"os/signal"
"runtime"
"strings"
"time"
)
const (
headerRegexp = `^([\w-]+):\s*(.+)`
authRegexp = `^(.+):([^\s].+)`
heyUA = "hey/0.0.1"
)
var (
m = "GET"
headers = ""
body = ""
bodyFile = ""
accept = ""
contentType = "text/html"
authHeader = ""
hostHeader = ""
output = ""
c = 50
n = 200
q = 0.0
t = 20
z = 0
h2 = false
cpus = runtime.GOMAXPROCS(-1)
disableCompression = false
disableKeepAlives = false
disableRedirects = false
proxyAddr = ""
)
func hey(url string, conc int, d string, ch chan float64) {
runtime.GOMAXPROCS(cpus)
num := n
//conc := c
//dur := z
//flag.Duration("z", 0, "")
//dur := new(time.Duration)
dur, err := time.ParseDuration(d)
if err != nil {
crashAndExit(err.Error())
}
if dur > 0 {
num = math.MaxInt32
if conc <= 0 {
crashAndExit("-c cannot be smaller than 1.")
}
} else {
if num <= 0 || conc <= 0 {
crashAndExit("-n and -c cannot be smaller than 1.")
}
if num < conc {
crashAndExit("-n cannot be less than -c.")
}
}
method := strings.ToUpper(m)
// set content-type
header := make(http.Header)
header.Set("Content-Type", contentType)
// set basic auth if set
var username, password string
var bodyAll []byte
var proxyURL *gourl.URL
req, err := http.NewRequest(method, url, nil)
if err != nil {
crashAndExit(err.Error())
}
req.ContentLength = int64(len(bodyAll))
if username != "" || password != "" {
req.SetBasicAuth(username, password)
}
ua := req.UserAgent()
if ua == "" {
ua = heyUA
} else {
ua += " " + heyUA
}
header.Set("User-Agent", ua)
req.Header = header
w := &requester.Work{
Request: req,
RequestBody: bodyAll,
N: num,
C: conc,
QPS: q,
Timeout: t,
DisableCompression: disableCompression,
DisableKeepAlives: disableKeepAlives,
DisableRedirects: disableRedirects,
H2: h2,
ProxyAddr: proxyURL,
Output: output,
}
w.Init()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
w.Stop()
}()
if dur > 0 {
go func() {
time.Sleep(dur)
w.Stop()
}()
}
w.Run()
ch <- w.Report.GetLatency()
}
func crashAndExit(msg string) {
if msg != "" {
fmt.Fprintf(os.Stderr, msg)
fmt.Fprintf(os.Stderr, "\n\n")
}
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/joyallen/config-selector.git
[email protected]:joyallen/config-selector.git
joyallen
config-selector
config-selector
master

搜索帮助