├── go.mod ├── ysab2.jpeg ├── conf ├── version.go └── conf.go ├── examples ├── get_urls.txt ├── post_urls.txt └── create_urls.py ├── tools ├── float.go ├── math.go ├── numtostr.go └── regexp.go ├── ysab.go ├── .gitignore ├── logger └── logger.go ├── CHANGELOG.md ├── summary ├── print.go ├── summary.go_back └── summary.go ├── worker ├── worker.go_back └── worker.go ├── http └── http.go ├── README.md ├── README-ENGLISH.md └── LICENSE /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/yunsonbai/ysab 2 | 3 | go 1.22 4 | -------------------------------------------------------------------------------- /ysab2.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yunsonbai/ysab/HEAD/ysab2.jpeg -------------------------------------------------------------------------------- /conf/version.go: -------------------------------------------------------------------------------- 1 | package conf 2 | 3 | var ( 4 | VERSION = "1.1.0-alpha5" 5 | ) 6 | -------------------------------------------------------------------------------- /examples/get_urls.txt: -------------------------------------------------------------------------------- 1 | -u http://127.0.0.1:8080/test?a=1&b=2 2 | -u http://127.0.0.1:8080/test?a=3&b=4 3 | -------------------------------------------------------------------------------- /tools/float.go: -------------------------------------------------------------------------------- 1 | package tools 2 | 3 | import ( 4 | "fmt" 5 | ) 6 | 7 | func FloatToStr3f(value float64) string { 8 | return fmt.Sprintf("%.3f", value) 9 | } 10 | -------------------------------------------------------------------------------- /examples/post_urls.txt: -------------------------------------------------------------------------------- 1 | -u http://127.0.0.1:8080/add -d {"name":"other", "blog": "https://other.top"} 2 | -u http://127.0.0.1:8080/add -d {"name":"yunson", "blog": "https://yunsonbai.top"} 3 | -------------------------------------------------------------------------------- /tools/math.go: -------------------------------------------------------------------------------- 1 | package tools 2 | 3 | func MinInt64(a, b int64) int64 { 4 | if a > b { 5 | return b 6 | } 7 | return a 8 | } 9 | 10 | func MaxInt64(a, b int64) int64 { 11 | if a > b { 12 | return a 13 | } 14 | return b 15 | } 16 | -------------------------------------------------------------------------------- /examples/create_urls.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | 4 | os.remove("./urls.txt") 5 | 6 | with open("./urls.txt", "a+") as f: 7 | for i in range(2): 8 | # f.write("-u http://127.0.0.1:8080/add\n") 9 | f.write('-u http://127.0.0.1:8080/add -d {"name":"yunson"}\n') 10 | -------------------------------------------------------------------------------- /tools/numtostr.go: -------------------------------------------------------------------------------- 1 | package tools 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | ) 7 | 8 | func FloatToPercent(num float64) string { 9 | var buf bytes.Buffer 10 | buf.WriteString(fmt.Sprintf(" %2.2f", num*100)) 11 | buf.WriteString("%") 12 | return buf.String() 13 | } 14 | -------------------------------------------------------------------------------- /ysab.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "github.com/yunsonbai/ysab/worker" 5 | ) 6 | 7 | func main() { 8 | // f, _ := os.OpenFile("cpu.prof", os.O_RDWR|os.O_CREATE, 0644) 9 | // pprof.StartCPUProfile(f) 10 | worker.StartWork() 11 | // defer pprof.StopCPUProfile() 12 | } 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | install_linux.sh 2 | install_mac.sh 3 | *.sh 4 | ._.DS_Store 5 | .DS_Store 6 | *log.txt 7 | .idea/ 8 | .vscode 9 | .coverage 10 | *.idea 11 | *.pyc 12 | *.iml 13 | *.xml 14 | *.db 15 | *.bak 16 | __pycache__ 17 | pip.ingnore 18 | *.sqlite3 19 | *.py[cod] 20 | !.gitkeep 21 | tmp.py 22 | *.tgz 23 | ysfe-mac-v* 24 | ysfe-linux-v* 25 | tmp 26 | bins -------------------------------------------------------------------------------- /logger/logger.go: -------------------------------------------------------------------------------- 1 | package logger 2 | 3 | import ( 4 | "flag" 5 | "log" 6 | "os" 7 | ) 8 | 9 | var ( 10 | Log *log.Logger 11 | ) 12 | 13 | func init() { 14 | // set location of log file 15 | var logpath = "./info.log" 16 | 17 | flag.Parse() 18 | var file, err1 = os.Create(logpath) 19 | 20 | if err1 != nil { 21 | panic(err1) 22 | } 23 | // Log = log.New(file, "", log.LstdFlags|log.Lshortfile) 24 | Log = log.New(file, "", 0) 25 | } 26 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## v1.1.0 4 | 2023-1118 5 | 6 | ### Changed 7 | 1. 当使用urlsfile参数时,r参数也生效支持多轮测试 (r takes effect when using urlsfile, supporting multiple rounds of testing) 8 | 2. 新增T参数,支持连续测试T秒。T大于0时,r失效 (Added T to support continuous testing for T seconds. r is invalid when T is greater than 0) 9 | 3. 优化一些代码 (Optimize some code) 10 | 4. 添加-b参数, 读取body体的buf大小, 默认256K (-b reader buf size, default 256K) 11 | 5. 基于go1.22.12编写 (Based on go1.22.12) 12 | 13 | ## v1.0.2 14 | 2023-0424 15 | 16 | ### Changed 17 | 1. Correction result description 18 | 2. Optimization Statistics. 19 | 3. Fix Put/Delete bug. 20 | 4. Add transfer metrics: TransferRate/sec (Byte). 21 | 22 | Compared with 1.0.1, the performance is improved by about 10%. 23 | 24 | 25 | ## v1.0.1 26 | 2021-0804 27 | 28 | ### Changed 29 | 1. Allow setting Host in header 30 | 31 | ## v1.0.0 32 | 2021-0528 33 | 34 | ### Changed 35 | 1. More stable performance 36 | 2. Make the number of tcp connections more stable 37 | 3. Faster, increase by about 10% -------------------------------------------------------------------------------- /tools/regexp.go: -------------------------------------------------------------------------------- 1 | package tools 2 | 3 | import ( 4 | "regexp" 5 | "strings" 6 | ) 7 | 8 | var ( 9 | keyValueRegexp = regexp.MustCompile(`^([\w-]+):\s*(.+)`) 10 | replaceSpaceRegexp = regexp.MustCompile(`\s+`) 11 | ignoreSpaceRegexp = regexp.MustCompile(`(:|,)\s+`) 12 | replaceQmarksRegexp = regexp.MustCompile(`"|'`) 13 | ) 14 | 15 | type ReqData struct { 16 | Url string 17 | Body string 18 | } 19 | 20 | func KeyValueRexpGetKV(inputsStr string) []string { 21 | return keyValueRegexp.FindStringSubmatch(inputsStr) 22 | } 23 | 24 | func ParseStr(str string) map[string]string { 25 | rdataMap := map[string]string{} 26 | str = string(replaceSpaceRegexp.ReplaceAll([]byte(str), []byte(" "))) 27 | str = string(ignoreSpaceRegexp.ReplaceAll([]byte(str), []byte("${1}_LL_"))) 28 | str = strings.TrimSpace(str) 29 | sl := strings.Split(str, " ") 30 | l := len(sl) 31 | for index := 0; index < l; index += 2 { 32 | rdataMap[sl[index]] = strings.Replace(sl[index+1], "_LL_", " ", -1) 33 | } 34 | return rdataMap 35 | } 36 | 37 | func GetReqData(str string) ReqData { 38 | reqData := ReqData{} 39 | if str == "" { 40 | return reqData 41 | } 42 | dataMap := ParseStr(str) 43 | for k, v := range dataMap { 44 | if k == "-u" { 45 | reqData.Url = v 46 | } else if k == "-d" { 47 | reqData.Body = v 48 | } 49 | } 50 | return reqData 51 | } 52 | 53 | // 替换所有的引号 54 | func ReplaceQmarks(str string, new string) string { 55 | return string(replaceSpaceRegexp.ReplaceAll([]byte(str), []byte(""))) 56 | } 57 | -------------------------------------------------------------------------------- /summary/print.go: -------------------------------------------------------------------------------- 1 | package summary 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "os" 7 | "sort" 8 | "text/template" 9 | ) 10 | 11 | var ( 12 | htmlTemplate = `Ysab: {{ .Version }} 13 | 14 | Summary: 15 | Complete requests: {{ .CompleteRequests }} 16 | Failed requests: {{ .FailedRequests }} 17 | Time taken (s): {{ .TimeToken }} 18 | Total data size (Byte): {{ .TotalDataSize }} 19 | Data size/request (Byte): {{ .AvgDataSize }} 20 | Max use time (ms): {{.MaxUseTime}} 21 | Min use time (ms): {{.MinUseTime}} 22 | Average use time (ms): {{.AvgUseTime}} 23 | Requests/sec: {{ .RequestsPerSec }} 24 | SuccessRequests/sec: {{ .SuccessRequestsPerSec }} 25 | TransferRate/sec (Byte): {{ .STransferRatePerSec }} 26 | 27 | Percentage of waiting time (ms): 28 | {{ formatMap .WaitingTimeDetail }} 29 | 30 | Time detail (ms) 31 | item min mean max 32 | dns {{.MinDNS}} {{.AvgDNS}} {{.MaxDNS}} 33 | conn {{.MinConn}} {{.AvgConn}} {{.MaxConn}} 34 | wait {{.MinDelay}} {{.AvgDelay}} {{.MaxDelay}} 35 | resp {{.MinRes}} {{.AvgRes}} {{.MaxRes}} 36 | 37 | Status code detail: 38 | {{ formatMap .CodeDetail }} 39 | ` 40 | ) 41 | 42 | func formatMap(data map[string]int) string { 43 | var keys []string 44 | 45 | for k, _ := range data { 46 | keys = append(keys, k) 47 | 48 | } 49 | sort.Strings(keys) 50 | res := new(bytes.Buffer) 51 | for _, k := range keys { 52 | res.WriteString(fmt.Sprintf(" %s:\t\t%d\n", k, data[k])) 53 | } 54 | return res.String() 55 | } 56 | 57 | var tmplFuncMap = template.FuncMap{ 58 | "formatMap": formatMap, 59 | } 60 | 61 | func Print(summaryData SummaryDataStruct) { 62 | 63 | tmpl, err := template.New("test").Funcs(tmplFuncMap).Parse(htmlTemplate) 64 | if err != nil { 65 | panic(err) 66 | } 67 | err = tmpl.Execute(os.Stdout, summaryData) 68 | if err != nil { 69 | panic(err) 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /worker/worker.go_back: -------------------------------------------------------------------------------- 1 | package worker 2 | 3 | import ( 4 | "bufio" 5 | "io" 6 | "os" 7 | "sync" 8 | "time" 9 | 10 | "github.com/yunsonbai/ysab/conf" 11 | yshttp "github.com/yunsonbai/ysab/http" 12 | "github.com/yunsonbai/ysab/summary" 13 | ystools "github.com/yunsonbai/ysab/tools" 14 | ) 15 | 16 | var ( 17 | rwg sync.WaitGroup 18 | urlChanel = make(chan [2]string, 30000) 19 | ) 20 | 21 | func worker(method string) { 22 | wf := yshttp.Get 23 | switch method { 24 | case "GET": 25 | wf = yshttp.Get 26 | case "POST": 27 | wf = yshttp.Post 28 | case "PUT": 29 | wf = yshttp.Put 30 | case "DELETE": 31 | wf = yshttp.Delete 32 | case "HEAD": 33 | wf = yshttp.Head 34 | default: 35 | return 36 | } 37 | readBuf := make([]byte, 32*1024) 38 | for { 39 | data, ok := <-urlChanel 40 | if !ok { 41 | return 42 | } 43 | summary.ResChanel <- wf(data[0], data[1], conf.Conf.Headers, readBuf) 44 | } 45 | } 46 | 47 | func addTask() { 48 | i := 0 49 | url := conf.Conf.Url 50 | body := conf.Conf.Body 51 | var fbr *bufio.Reader 52 | if conf.Conf.UrlFilePath != "" { 53 | fi, _ := os.Open(conf.Conf.UrlFilePath) 54 | defer fi.Close() 55 | fbr = bufio.NewReader(fi) 56 | } 57 | done := 0 58 | for { 59 | i++ 60 | if conf.Conf.UrlFilePath != "" { 61 | line, _, err := fbr.ReadLine() 62 | if err == io.EOF { 63 | done = 1 64 | } 65 | reqData := ystools.GetReqData(string(line)) 66 | url = reqData.Url 67 | body = reqData.Body 68 | } else { 69 | if i == int(conf.Conf.UrlNum) { 70 | done = 1 71 | } 72 | } 73 | if url != "" { 74 | data := [2]string{url, body} 75 | urlChanel <- data 76 | } 77 | if done == 1 { 78 | break 79 | } 80 | } 81 | if done == 1 { 82 | for { 83 | time.Sleep(time.Duration(50) * time.Millisecond) 84 | if len(urlChanel) == 0 { 85 | close(urlChanel) 86 | return 87 | } 88 | } 89 | } 90 | } 91 | 92 | func StartWork() { 93 | rwg.Add(1) 94 | go addTask() 95 | go func() { 96 | summary.HandleRes() 97 | rwg.Done() 98 | }() 99 | for index := 0; index < int(conf.Conf.N); index++ { 100 | go func() { 101 | worker(conf.Conf.Method) 102 | }() 103 | } 104 | rwg.Wait() 105 | } 106 | -------------------------------------------------------------------------------- /worker/worker.go: -------------------------------------------------------------------------------- 1 | package worker 2 | 3 | import ( 4 | "bufio" 5 | "io" 6 | "net/http" 7 | "os" 8 | "sync" 9 | "time" 10 | 11 | "github.com/yunsonbai/ysab/conf" 12 | yshttp "github.com/yunsonbai/ysab/http" 13 | "github.com/yunsonbai/ysab/summary" 14 | ystools "github.com/yunsonbai/ysab/tools" 15 | ) 16 | 17 | var ( 18 | rwg sync.WaitGroup 19 | // urlChanel = make(chan [2]string, 30000) 20 | urlChanel = make(chan [2]string, conf.Conf.UrlChNum) 21 | ) 22 | 23 | func worker(method string) { 24 | wf := yshttp.Get 25 | switch method { 26 | case "GET": 27 | wf = yshttp.Get 28 | case "POST": 29 | wf = yshttp.Post 30 | case "PUT": 31 | wf = yshttp.Put 32 | case "DELETE": 33 | wf = yshttp.Delete 34 | case "HEAD": 35 | wf = yshttp.Head 36 | default: 37 | return 38 | } 39 | // readBuf := make([]byte, 32*1024) 40 | bufioReader := bufio.NewReaderSize(nil, conf.Conf.ReaderBufSize) 41 | var req *http.Request 42 | if conf.Conf.UrlFilePath == "" { 43 | req = yshttp.GetReq(conf.Conf.Url, method, conf.Conf.Body, conf.Conf.Headers) 44 | } 45 | for { 46 | data, ok := <-urlChanel 47 | if !ok { 48 | return 49 | } 50 | summary.ResChanel <- wf(req, data[0], data[1], conf.Conf.Headers, bufioReader) 51 | } 52 | } 53 | 54 | func addTaskByFile(useDuration bool) { 55 | var url string 56 | var body string 57 | var curR uint32 58 | var count int64 59 | var over bool 60 | file, err := os.Open(conf.Conf.UrlFilePath) 61 | if err != nil { 62 | panic(err) 63 | } 64 | defer file.Close() 65 | fbr := bufio.NewReader(file) 66 | stopChan := make(chan struct{}) 67 | if useDuration { 68 | go func() { 69 | time.Sleep(time.Duration(conf.Conf.Duration) * time.Second) 70 | close(stopChan) 71 | }() 72 | } 73 | for { 74 | if useDuration { 75 | select { 76 | case <-stopChan: 77 | over = true 78 | default: 79 | } 80 | } 81 | if over { 82 | break 83 | } 84 | line, _, err := fbr.ReadLine() 85 | if err == io.EOF { 86 | if !useDuration { 87 | curR++ 88 | if curR >= conf.Conf.Round { 89 | over = true 90 | break 91 | } 92 | } 93 | if _, err := file.Seek(0, io.SeekStart); err != nil { 94 | panic(err) 95 | } 96 | continue 97 | } 98 | reqData := ystools.GetReqData(string(line)) 99 | url = reqData.Url 100 | body = reqData.Body 101 | if url != "" { 102 | data := [2]string{url, body} 103 | count++ 104 | urlChanel <- data 105 | } 106 | } 107 | conf.Conf.UrlNum = count 108 | summary.ResChanel <- summary.ResStruct{EndMk: true} 109 | } 110 | 111 | func addTaskByCmd(useDuration bool) { 112 | totalUrlNum := int64(conf.Conf.Round) * int64(conf.Conf.N) 113 | var count int64 114 | var over bool 115 | stopChan := make(chan struct{}) 116 | if useDuration { 117 | go func() { 118 | time.Sleep(time.Duration(conf.Conf.Duration) * time.Second) 119 | close(stopChan) 120 | }() 121 | } 122 | for { 123 | if useDuration { 124 | select { 125 | case <-stopChan: 126 | over = true 127 | default: 128 | } 129 | } else { 130 | if count >= totalUrlNum { 131 | over = true 132 | } 133 | } 134 | 135 | if over { 136 | break 137 | } 138 | data := [2]string{conf.Conf.Url, conf.Conf.Body} 139 | count++ 140 | urlChanel <- data 141 | } 142 | conf.Conf.UrlNum = count 143 | summary.ResChanel <- summary.ResStruct{EndMk: true} 144 | } 145 | 146 | func addTask() { 147 | var useDuration bool 148 | if conf.Conf.Duration > 0 { 149 | useDuration = true 150 | } 151 | if conf.Conf.UrlFilePath != "" { 152 | addTaskByFile(useDuration) 153 | } else { 154 | addTaskByCmd(useDuration) 155 | } 156 | for { 157 | time.Sleep(time.Duration(50) * time.Millisecond) 158 | if len(urlChanel) == 0 { 159 | close(urlChanel) 160 | return 161 | } 162 | } 163 | 164 | } 165 | 166 | func StartWork() { 167 | rwg.Add(1) 168 | go addTask() 169 | conf.Conf.StartTime = time.Now().UnixMicro() 170 | go func() { 171 | summary.HandleRes() 172 | rwg.Done() 173 | }() 174 | for index := 0; index < int(conf.Conf.N); index++ { 175 | go func() { 176 | worker(conf.Conf.Method) 177 | }() 178 | } 179 | rwg.Wait() 180 | } 181 | -------------------------------------------------------------------------------- /http/http.go: -------------------------------------------------------------------------------- 1 | package http 2 | 3 | import ( 4 | "bufio" 5 | "bytes" 6 | "io" 7 | "math/rand" 8 | "net/http" 9 | "net/http/httptrace" 10 | netulr "net/url" 11 | "strings" 12 | "time" 13 | 14 | "github.com/yunsonbai/ysab/conf" 15 | "github.com/yunsonbai/ysab/summary" 16 | ) 17 | 18 | const ( 19 | clientsN int = 2 20 | ) 21 | 22 | var ( 23 | HttpClients []*http.Client 24 | ) 25 | 26 | func init() { 27 | for i := 0; i < clientsN; i++ { 28 | HttpClients = append(HttpClients, creteHttpClient()) 29 | } 30 | } 31 | 32 | func creteHttpClient() *http.Client { 33 | client := &http.Client{ 34 | Transport: &http.Transport{ 35 | MaxConnsPerHost: int(conf.Conf.N)/clientsN + 128, 36 | MaxIdleConnsPerHost: int(conf.Conf.N)/clientsN + 32, 37 | DisableKeepAlives: false, 38 | DisableCompression: false, 39 | }, 40 | Timeout: time.Duration(conf.Conf.TimeOut) * time.Second, 41 | } 42 | return client 43 | } 44 | 45 | func do(req *http.Request, bufioReader *bufio.Reader) (sumRes summary.ResStruct) { 46 | var tmpt int64 47 | var dnsStart, connStart, respStart, reqStart, delayStart int64 48 | trace := &httptrace.ClientTrace{ 49 | DNSStart: func(info httptrace.DNSStartInfo) { 50 | dnsStart = time.Now().UnixMicro() 51 | }, 52 | DNSDone: func(dnsInfo httptrace.DNSDoneInfo) { 53 | sumRes.DNSTime = time.Now().UnixMicro() - dnsStart 54 | }, 55 | GetConn: func(h string) { 56 | connStart = time.Now().UnixMicro() 57 | }, 58 | GotConn: func(connInfo httptrace.GotConnInfo) { 59 | tmpt = time.Now().UnixMicro() 60 | if !connInfo.Reused { 61 | if connStart <= 0 { 62 | sumRes.ConnTime = 0 63 | } else { 64 | sumRes.ConnTime = tmpt - connStart 65 | } 66 | } 67 | reqStart = tmpt 68 | }, 69 | 70 | WroteRequest: func(w httptrace.WroteRequestInfo) { 71 | tmpt = time.Now().UnixMicro() 72 | sumRes.ReqTime = tmpt - reqStart 73 | delayStart = tmpt 74 | }, 75 | GotFirstResponseByte: func() { 76 | tmpt = time.Now().UnixMicro() 77 | sumRes.DelayTime = tmpt - delayStart 78 | respStart = tmpt 79 | }, 80 | } 81 | newReq := req.WithContext(httptrace.WithClientTrace(req.Context(), trace)) 82 | tStart := time.Now().UnixMicro() 83 | 84 | client := HttpClients[rand.Intn(clientsN)] 85 | response, err := client.Do(newReq) 86 | tEnd := time.Now() 87 | if response != nil { 88 | 89 | sumRes.Code = response.StatusCode 90 | defer response.Body.Close() 91 | bufioReader.Reset(response.Body) 92 | _, err = io.Copy(io.Discard, bufioReader) 93 | if err != nil { 94 | sumRes.Code = 500 95 | } 96 | tEnd = time.Now() 97 | if response.ContentLength > -1 { 98 | sumRes.Size = response.ContentLength 99 | } else { 100 | sumRes.Size = 0 101 | } 102 | 103 | } else { 104 | sumRes.Code = 503 105 | if err, ok := err.(*netulr.Error); ok { 106 | if err.Timeout() { 107 | sumRes.Code = 504 108 | } 109 | } 110 | } 111 | sumRes.TimeStamp = tEnd.UnixMicro() 112 | sumRes.TotalUseTime = tEnd.UnixMicro() - tStart 113 | sumRes.ResTime = tEnd.UnixMicro() - respStart 114 | 115 | return 116 | } 117 | 118 | func GetReq(url, method, bodydata string, headers map[string]string) (req *http.Request) { 119 | req, err := http.NewRequest(method, url, bytes.NewBuffer([]byte(bodydata))) 120 | if err != nil { 121 | return 122 | } 123 | req.Header.Set("Accept", "*/*") 124 | req.Header.Set("Connection", "keep-alive") 125 | req.Header.Set("Accept-Encoding", "gzip, deflate") 126 | for k, v := range headers { 127 | if strings.ToLower(k) == "host" { 128 | req.Host = v 129 | } else { 130 | req.Header.Set(k, v) 131 | } 132 | } 133 | return 134 | } 135 | 136 | func Head(req *http.Request, url, data string, headers map[string]string, bufioReader *bufio.Reader) summary.ResStruct { 137 | if req == nil { 138 | req = GetReq(url, "HEAD", data, headers) 139 | } 140 | return do(req, bufioReader) 141 | } 142 | 143 | func Get(req *http.Request, url, data string, headers map[string]string, bufioReader *bufio.Reader) summary.ResStruct { 144 | if req == nil { 145 | req = GetReq(url, "GET", data, headers) 146 | } 147 | return do(req, bufioReader) 148 | } 149 | func Post(req *http.Request, url, data string, headers map[string]string, bufioReader *bufio.Reader) summary.ResStruct { 150 | if req == nil { 151 | req = GetReq(url, "POST", data, headers) 152 | } 153 | return do(req, bufioReader) 154 | } 155 | func Put(req *http.Request, url, data string, headers map[string]string, bufioReader *bufio.Reader) summary.ResStruct { 156 | if req == nil { 157 | req = GetReq(url, "PUT", data, headers) 158 | } 159 | return do(req, bufioReader) 160 | } 161 | func Delete(req *http.Request, url, data string, headers map[string]string, bufioReader *bufio.Reader) summary.ResStruct { 162 | if req == nil { 163 | req = GetReq(url, "DELETE", data, headers) 164 | } 165 | return do(req, bufioReader) 166 | } 167 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | ![ysab](https://github.com/yunsonbai/ysab/blob/master/ysab2.jpeg) 3 | 4 | ysab 是一个可以帮助你获取http服务器压力测试性能指标的工具,有点像Apache的ab,不同的是,它可以帮你发送携带不同参数的请求,这样你就可以便捷地重放线上的真实请求。 5 | 6 | [English](./README-ENGLISH.md) 7 | 8 | ## 安装 9 | * mac 10 | 11 | * curl -L -o install_mac.sh https://github.com/yunsonbai/ysab/releases/download/install-tool/install_mac.sh && sh install_mac.sh && rm -rf install_mac.sh 12 | 13 | * 如果报权限问题请执行: curl -L -o install_mac.sh https://github.com/yunsonbai/ysab/releases/download/install-tool/install_mac.sh && sudo sh install_mac.sh && rm -rf install_mac.sh 14 | 15 | ```如果安装完后不能输入 ysab 命令,可以重启终端``` 16 | 17 | * linux 18 | 19 | * curl -L -o install_linux.sh https://github.com/yunsonbai/ysab/releases/download/install-tool/install_linux.sh && sh install_linux.sh && rm -rf install_linux.sh 20 | 21 | * 如果报权限问题请执行: curl -L -o install_linux.sh https://github.com/yunsonbai/ysab/releases/download/install-tool/install_linux.sh && sudo sh install_linux.sh && rm -rf install_linux.sh 22 | 23 | * arm 24 | * 如果需要运行arm版本,可以clone一份代码,在arm机上build一下(go build -o ysab),然后把可执行文件ysab放到/usr/local/bin/下即可 25 | 26 | ## 参数说明 27 | * ysab -h 28 | 29 | ``` 30 | Options: 31 | -r 压测轮数,总的请求量是 r * n 32 | -n 并发数,最大900,最小1 33 | -T 运行时间(单位秒, 默认0), T大于0时, r无效 34 | -m HTTP method, 可选值 GET,POST,PUT,DELETE,Head,默认GET 35 | -u Url of request, 如果有特殊符号需要用引号 36 | 例如: 37 | -u "https://yunsonbai.top/?name=yunson" 38 | -u 'https://yunsonbai.top/?name=yunson' 39 | -u https://yunsonbai.top/?name=yunson 40 | -H 添加请求头 41 | 例如: 42 | -H "Accept: text/html" 设置 Accept 43 | -H "Host: yunsonbai.top" 设置 Host 44 | -H "Uid: yunson" -H "Content-Type: application/json" 设置Uid和Content-Type 45 | -t 每个请求的超时时间,单位为秒,默认10 46 | -d 请求体 47 | 例如: 48 | '{"a": "a"}' 49 | -b 读取body体的buf大小(KB), 默认256 50 | -h 帮助 51 | -v 显示版本号 52 | -urlsfile 包含所有请求信息的文件,如果设置了该参数, -u,-d 将会失效 53 | 例如: 54 | -urlsfile /tmp/urls.txt 55 | ``` 56 | 57 | * 注意: -urlsfile 是实现发送携带不同参数请求的关键参数,文件详细内容,可参照examples/post_urls.txt 和 examples/get_urls.txt 58 | 59 | ## 一些例子 60 | 1. GET请求,300个协程一起处理,每个协程做2轮 61 | 62 | ysab -n 300 -r 2 -u 'http://10.10.10.10:8080/test' 63 | 64 | 2. POST请求,400个协程一起处理,每个协程做2轮 65 | 66 | ysab -n 300 -r 2 -m POST -u 'http://10.10.10.10:8080/add' -d '{"name": "yunson"}' 67 | 68 | 3. GET请求,400个协程一起处理,持续100秒 69 | 70 | ysab -n 300 -T 100 -u 'http://10.10.10.10:8080/test' 71 | 72 | 4. POST请求,400个协程一起处理,持续100秒测试 73 | 74 | ysab -n 300 -T 100 -m POST -u 'http://10.10.10.10:8080/add' -d '{"name": "yunson"}' 75 | 76 | 5. GET请求,400个协程一起处理urls.txt中的连接,处理2次urls.txt中的连接 77 | 78 | ysab -r 2 -n 400 -urlsfile ./examples/urls.txt 79 | 80 | 6. GET请求,400个协程一起处理urls.txt中的连接,一直循环执行文件中的连接,持续100秒 81 | 82 | ysab -T 100 -n 400 -urlsfile ./examples/urls.txt 83 | 84 | 7. POST请求,400个协程一起处理urls.txt中的连接,一直循环执行文件中的连接,持续100秒 85 | 86 | ysab -T 100 -n 400 -m POST -urlsfile ./examples/urls.txt 87 | 88 | ## 结果展示 89 | ``` 90 | (http://10.10.10.10:8080/test 是一个借助gin完成的测试API, 限速. 这个 API 的 response 是 "hello world".) 91 | 92 | [yunson ~]# ysab -n 900 -r 3 -u 'http://10.10.10.10:8080/test' 93 | 94 | Summary: 95 | Complete requests: 2700 96 | Failed requests: 2550 97 | Time taken (s): 2.203996471 98 | Total data size (Byte): 0 99 | Data size/request (Byte): 0 100 | Max use time (ms): 2076 101 | Min use time (ms): 3 102 | Average use time (ms): 139.997 103 | Requests/sec: 1225.047333571706 104 | SuccessRequests/sec: 68.05818519842812 105 | 106 | Percentage of waiting time (ms): 107 | 10.00%: 12 108 | 25.00%: 26 109 | 50.00%: 46 110 | 75.00%: 69 111 | 90.00%: 122 112 | 95.00%: 1291 113 | 99.00%: 1992 114 | 99.90%: 2040 115 | 99.99%: 2052 116 | 117 | 118 | Time detail (ms) 119 | item min mean max 120 | dns 0 0 0 121 | conn 0 11.714 78 122 | wait 3 69.509 1010 123 | resp 0 10.53 50 124 | 125 | Response Time histogram (code: requests): 126 | 200: 150 127 | 429: 2550 128 | ``` 129 | 130 | ## 关于 http code 131 | * 2xx: Success 132 | * != 2xx: Faild 133 | * 5xx: 134 | * 500: Server Error 135 | * 503: May be connection refused or connection reset by peer, you need to check your server. 136 | * other: [http code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes) 137 | 138 | ## 关于引号的使用 139 | 以下三个方式都可以, 若有特殊符号建议用''包裹 140 | * ysab -n 900 -r 50 -u "http://10.121.130.218:8080/test" 141 | * ysab -n 900 -r 50 -u 'http://10.121.130.218:8080/test' 142 | * ysab -n 900 -r 50 -u http://10.121.130.218:8080/test 143 | 144 | ## 其他 145 | 推荐使用 -urlsfile, 你可以使用 -urlsfile 发送携带不同 body 或 url 的请求 146 | 147 | * 样例: 148 | * ysab -r 1 -n 500 -urlsfile ./examples/urls.txt 149 | * ysab -r 2 -n 500 -m POST -urlsfile ./examples/post_urls.txt 150 | * ysab -T 30 -n 500 -m POST -urlsfile ./examples/post_urls.txt 151 | 152 | * urls.txt example: 153 | * examples/xx_urls.txt 154 | * You can use create_urls.py to create a urls.txt file. 155 | 156 | 157 | ## 鸣谢 158 | * [Jason-Liu-Dream](https://github.com/Jason-Liu-Dream) 159 | * [zbing3](https://github.com/zbing3) 160 | * [cugbtang](https://github.com/cugbtang) 161 | -------------------------------------------------------------------------------- /conf/conf.go: -------------------------------------------------------------------------------- 1 | package conf 2 | 3 | import ( 4 | "errors" 5 | "flag" 6 | "fmt" 7 | "os" 8 | "runtime" 9 | 10 | "github.com/yunsonbai/ysab/tools" 11 | ) 12 | 13 | var usage = `Usage: ysab [Options] 14 | 15 | Options: 16 | -r Rounds of request to run, total requests equal r * n 17 | -n Number of simultaneous requests, 0 65535 { 139 | confError(errors.New("(-n) Number must be 0 60 { 160 | confError(errors.New("(-t) timeout must be 0 900 { 171 | Conf.N = 900 172 | } 173 | if Conf.TimeOut <= 0 || Conf.TimeOut > 60 { 174 | Conf.TimeOut = 60 175 | } 176 | Conf.TimeBase = 1000000 177 | Conf.TimeOut = Conf.TimeOut * Conf.TimeBase 178 | // Conf.StartTime = time.Now().UnixMicro() 179 | numCPU := runtime.NumCPU() 180 | Conf.UrlChNum = int(Conf.N) * numCPU 181 | Conf.SumResChNum = int(Conf.N) * numCPU / 2 182 | if Conf.UrlChNum < 4000 { 183 | Conf.UrlChNum = 4000 184 | } 185 | if Conf.SumResChNum < 2000 { 186 | Conf.SumResChNum = 2000 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /README-ENGLISH.md: -------------------------------------------------------------------------------- 1 | 2 | ![ysab](https://github.com/yunsonbai/ysab/blob/master/ysab2.jpeg) 3 | 4 | ysab is a tool that can help you get some performance parameters of http server stress test. 5 | It can help you send requests with different parameters, so you can easily replay the real request online. 6 | 7 | ## Installation 8 | * mac 9 | * curl -L -o install_mac.sh https://github.com/yunsonbai/ysab/releases/download/install-tool/install_mac.sh && sh install_mac.sh && rm -rf install_mac.sh 10 | 11 | * If report a permission problem, please execute: curl -L -o install_mac.sh https://github.com/yunsonbai/ysab/releases/download/install-tool/install_mac.sh && sudo sh install_mac.sh && rm -rf install_mac.sh 12 | 13 | ```If you cannot enter the ysab command after installation, you can restart the terminal.``` 14 | 15 | * linux 16 | * curl -L -o install_linux.sh https://github.com/yunsonbai/ysab/releases/download/install-tool/install_linux.sh && sh install_linux.sh && rm -rf install_linux.sh 17 | 18 | * If report a permission problem, please execute: curl -L -o install_linux.sh https://github.com/yunsonbai/ysab/releases/download/install-tool/install_linux.sh && sudo sh install_linux.sh && rm -rf install_linux.sh 19 | 20 | * arm 21 | * If you need to run the arm version, you can clone a copy of the code, build it on the arm machine (go build -o ysab), and then put the executable file ysab under /usr/local/bin/ 22 | 23 | ## Usage 24 | * ysab -h 25 | 26 | ``` 27 | Options: 28 | -r Rounds of request to run, total requests equal r * n 29 | -n Number of simultaneous requests, 0 299 || code < 200 { 133 | summaryDataTmp.FailedRequests++ 134 | } else { 135 | summaryDataTmp.SuccessRequests++ 136 | } 137 | summaryDataTmp.AvgUseTime += res.TotalUseTime 138 | summaryDataTmp.AvgConn += res.ConnTime 139 | summaryDataTmp.AvgDNS += res.DNSTime 140 | summaryDataTmp.AvgDelay += res.DelayTime 141 | summaryDataTmp.AvgReq += res.ReqTime 142 | summaryDataTmp.AvgRes += res.ResTime 143 | 144 | summaryDataTmp.MinUseTime = tools.MinInt64(res.TotalUseTime, summaryDataTmp.MinUseTime) 145 | summaryDataTmp.MinConn = tools.MinInt64(res.ConnTime, summaryDataTmp.MinConn) 146 | summaryDataTmp.MinDNS = tools.MinInt64(res.DNSTime, summaryDataTmp.MinDNS) 147 | summaryDataTmp.MinDelay = tools.MinInt64(res.DelayTime, summaryDataTmp.MinDelay) 148 | summaryDataTmp.MinReq = tools.MinInt64(res.ReqTime, summaryDataTmp.MinReq) 149 | summaryDataTmp.MinRes = tools.MinInt64(res.ResTime, summaryDataTmp.MinRes) 150 | 151 | summaryDataTmp.MaxUseTime = tools.MaxInt64(res.TotalUseTime, summaryDataTmp.MaxUseTime) 152 | summaryDataTmp.MaxConn = tools.MaxInt64(res.ConnTime, summaryDataTmp.MaxConn) 153 | summaryDataTmp.MaxDNS = tools.MaxInt64(res.DNSTime, summaryDataTmp.MaxDNS) 154 | summaryDataTmp.MaxDelay = tools.MaxInt64(res.DelayTime, summaryDataTmp.MaxDelay) 155 | summaryDataTmp.MaxReq = tools.MaxInt64(res.ReqTime, summaryDataTmp.MaxReq) 156 | summaryDataTmp.MaxRes = tools.MaxInt64(res.ResTime, summaryDataTmp.MaxRes) 157 | waitTimes = append(waitTimes, microToMilli(res.TotalUseTime)) 158 | } 159 | 160 | summaryData := SummaryDataStruct{ 161 | CompleteRequests: summaryDataTmp.CompleteRequests, 162 | FailedRequests: summaryDataTmp.FailedRequests, 163 | SuccessRequests: summaryDataTmp.SuccessRequests, 164 | TotalDataSize: summaryDataTmp.TotalDataSize, 165 | MinUseTime: tools.FloatToStr3f(microToSecond(summaryDataTmp.MinUseTime)), 166 | MaxUseTime: tools.FloatToStr3f(microToSecond(summaryDataTmp.MaxUseTime)), 167 | CodeDetail: make(map[string]int), 168 | WaitingTimeDetail: make(map[string]int), 169 | 170 | MaxConn: tools.FloatToStr3f(microToSecond(summaryDataTmp.MaxConn)), 171 | MinConn: tools.FloatToStr3f(microToSecond(summaryDataTmp.MinConn)), 172 | MaxDNS: tools.FloatToStr3f(microToSecond(summaryDataTmp.MaxDNS)), 173 | MinDNS: tools.FloatToStr3f(microToSecond(summaryDataTmp.MinDNS)), 174 | MaxReq: tools.FloatToStr3f(microToSecond(summaryDataTmp.MaxReq)), 175 | MinReq: tools.FloatToStr3f(microToSecond(summaryDataTmp.MinReq)), 176 | MaxDelay: tools.FloatToStr3f(microToSecond(summaryDataTmp.MaxDelay)), 177 | MinDelay: tools.FloatToStr3f(microToSecond(summaryDataTmp.MinDelay)), 178 | MaxRes: tools.FloatToStr3f(microToSecond(summaryDataTmp.MaxRes)), 179 | MinRes: tools.FloatToStr3f(microToSecond(summaryDataTmp.MinRes))} 180 | 181 | summaryData.AvgUseTime = tools.FloatToStr3f(microToSecond(summaryDataTmp.AvgUseTime) / float64(config.UrlNum)) 182 | summaryData.AvgConn = tools.FloatToStr3f(microToSecond(summaryDataTmp.AvgConn) / float64(config.UrlNum)) 183 | summaryData.AvgDNS = tools.FloatToStr3f(microToSecond(summaryDataTmp.AvgDNS) / float64(config.UrlNum)) 184 | summaryData.AvgDelay = tools.FloatToStr3f(microToSecond(summaryDataTmp.AvgDelay) / float64(config.UrlNum)) 185 | summaryData.AvgReq = tools.FloatToStr3f(microToSecond(summaryDataTmp.AvgReq) / float64(config.UrlNum)) 186 | summaryData.AvgRes = tools.FloatToStr3f(microToSecond(summaryDataTmp.AvgRes) / float64(config.UrlNum)) 187 | summaryData.AvgDataSize = tools.FloatToStr3f(float64(summaryDataTmp.TotalDataSize) / float64(config.UrlNum)) 188 | 189 | for k, v := range codeDetail { 190 | summaryData.CodeDetail[strconv.Itoa(k)] = v 191 | } 192 | t := microToSecond(config.EndTime - config.StartTime) 193 | summaryData.TimeToken = tools.FloatToStr3f(t) 194 | summaryData.RequestsPerSec = tools.FloatToStr3f(float64(config.UrlNum) / t) 195 | summaryData.SuccessRequestsPerSec = tools.FloatToStr3f(float64(summaryData.SuccessRequests) / t) 196 | sort.Float64s(waitTimes) 197 | waitTimesL := float64(len(waitTimes)) 198 | tps := []float64{0.1, 0.25, 0.5, 0.75, 0.9, 0.95, 0.99, 0.999, 0.9999} 199 | tpsL := len(tps) 200 | for i := 0; i < tpsL; i++ { 201 | summaryData.WaitingTimeDetail[tools.FloatToPercent( 202 | tps[i])] = int(waitTimes[int(waitTimesL*tps[i]-1)]) 203 | } 204 | Print(summaryData) 205 | } 206 | -------------------------------------------------------------------------------- /summary/summary.go: -------------------------------------------------------------------------------- 1 | package summary 2 | 3 | import ( 4 | "sort" 5 | "strconv" 6 | "sync" 7 | 8 | "github.com/yunsonbai/ysab/conf" 9 | "github.com/yunsonbai/ysab/tools" 10 | ) 11 | 12 | var ( 13 | AnalysisData sync.Map 14 | ResChanel = make(chan ResStruct, conf.Conf.SumResChNum) 15 | RunOverSignal = make(chan int, 1) 16 | 17 | codeDetail = make(map[int]int) 18 | summaryDataTmp = summaryDataTmpStruct{ 19 | MinConn: conf.Conf.TimeOut, 20 | MinDNS: conf.Conf.TimeOut, 21 | MinDelay: conf.Conf.TimeOut, 22 | MinReq: conf.Conf.TimeOut, 23 | MinUseTime: conf.Conf.TimeOut, 24 | MinRes: conf.Conf.TimeOut, 25 | } 26 | waitTimes = make(map[int64]int) 27 | ) 28 | 29 | type ResStruct struct { 30 | EndMk bool 31 | Size int64 32 | TimeStamp int64 33 | Code int 34 | TotalUseTime int64 35 | ConnTime int64 36 | DNSTime int64 37 | ReqTime int64 38 | DelayTime int64 39 | ResTime int64 40 | } 41 | 42 | type summaryDataTmpStruct struct { 43 | CompleteRequests int64 44 | FailedRequests int64 45 | SuccessRequests int64 46 | TotalDataSize int64 47 | 48 | MinUseTime int64 // 微妙级 49 | MaxUseTime int64 // 微妙级 50 | AvgUseTime int64 // 微妙级 51 | 52 | AvgConn int64 // 微妙级 53 | MaxConn int64 // 微妙级 54 | MinConn int64 // 微妙级 55 | AvgDNS int64 // 微妙级 56 | MaxDNS int64 // 微妙级 57 | MinDNS int64 // 微妙级 58 | AvgReq int64 // 微妙级 59 | MaxReq int64 // 微妙级 60 | MinReq int64 // 微妙级 61 | AvgDelay int64 // 微妙级 62 | MaxDelay int64 // 微妙级 63 | MinDelay int64 // 微妙级 64 | AvgRes int64 // 微妙级 65 | MaxRes int64 // 微妙级 66 | MinRes int64 // 微妙级 67 | } 68 | 69 | type SummaryDataStruct struct { 70 | Version string 71 | CompleteRequests int64 72 | FailedRequests int64 73 | SuccessRequests int64 74 | TimeToken string 75 | TotalDataSize int64 76 | AvgDataSize string 77 | RequestsPerSec string 78 | SuccessRequestsPerSec string 79 | STransferRatePerSec string 80 | 81 | MinUseTime string 82 | MaxUseTime string 83 | AvgUseTime string 84 | CodeDetail map[string]int 85 | WaitingTimeDetail map[string]int 86 | 87 | AvgConn string 88 | MaxConn string 89 | MinConn string 90 | AvgDNS string 91 | MaxDNS string 92 | MinDNS string 93 | AvgReq string 94 | MaxReq string 95 | MinReq string 96 | AvgDelay string 97 | MaxDelay string 98 | MinDelay string 99 | AvgRes string 100 | MaxRes string 101 | MinRes string 102 | } 103 | 104 | func microToSecond(t int64) float64 { 105 | return float64(t) / float64(conf.Conf.TimeBase) 106 | } 107 | 108 | func microToMillI(t int64) int64 { 109 | return 1000 * t / conf.Conf.TimeBase 110 | } 111 | 112 | func microToMillF(t int64) float64 { 113 | return 1000 * float64(t) / float64(conf.Conf.TimeBase) 114 | } 115 | 116 | func microToMilliStr(t int64) string { 117 | return strconv.Itoa(int(microToMillI(t))) 118 | } 119 | 120 | func HandleRes() { 121 | var tkey int64 122 | var code int 123 | for { 124 | res, ok := <-ResChanel 125 | if !ok { 126 | break 127 | } 128 | if res.EndMk { 129 | if summaryDataTmp.CompleteRequests == conf.Conf.UrlNum { 130 | close(ResChanel) 131 | break 132 | } else { 133 | continue 134 | } 135 | } 136 | summaryDataTmp.CompleteRequests++ 137 | summaryDataTmp.TotalDataSize += res.Size 138 | if conf.Conf.UrlNum > 0 && summaryDataTmp.CompleteRequests == conf.Conf.UrlNum { 139 | close(ResChanel) 140 | } 141 | code = res.Code 142 | codeDetail[code]++ 143 | if conf.Conf.EndTime < res.TimeStamp { 144 | conf.Conf.EndTime = res.TimeStamp 145 | } 146 | if code > 299 || code < 200 { 147 | summaryDataTmp.FailedRequests++ 148 | } else { 149 | summaryDataTmp.SuccessRequests++ 150 | } 151 | summaryDataTmp.AvgUseTime += res.TotalUseTime 152 | summaryDataTmp.AvgConn += res.ConnTime 153 | summaryDataTmp.AvgDNS += res.DNSTime 154 | summaryDataTmp.AvgDelay += res.DelayTime 155 | summaryDataTmp.AvgReq += res.ReqTime 156 | summaryDataTmp.AvgRes += res.ResTime 157 | 158 | summaryDataTmp.MinUseTime = tools.MinInt64(res.TotalUseTime, summaryDataTmp.MinUseTime) 159 | summaryDataTmp.MinConn = tools.MinInt64(res.ConnTime, summaryDataTmp.MinConn) 160 | summaryDataTmp.MinDNS = tools.MinInt64(res.DNSTime, summaryDataTmp.MinDNS) 161 | summaryDataTmp.MinDelay = tools.MinInt64(res.DelayTime, summaryDataTmp.MinDelay) 162 | summaryDataTmp.MinReq = tools.MinInt64(res.ReqTime, summaryDataTmp.MinReq) 163 | summaryDataTmp.MinRes = tools.MinInt64(res.ResTime, summaryDataTmp.MinRes) 164 | 165 | summaryDataTmp.MaxUseTime = tools.MaxInt64(res.TotalUseTime, summaryDataTmp.MaxUseTime) 166 | summaryDataTmp.MaxConn = tools.MaxInt64(res.ConnTime, summaryDataTmp.MaxConn) 167 | summaryDataTmp.MaxDNS = tools.MaxInt64(res.DNSTime, summaryDataTmp.MaxDNS) 168 | summaryDataTmp.MaxDelay = tools.MaxInt64(res.DelayTime, summaryDataTmp.MaxDelay) 169 | summaryDataTmp.MaxReq = tools.MaxInt64(res.ReqTime, summaryDataTmp.MaxReq) 170 | summaryDataTmp.MaxRes = tools.MaxInt64(res.ResTime, summaryDataTmp.MaxRes) 171 | 172 | tkey = microToMillI(res.TotalUseTime) 173 | // if tkey > 3000 { 174 | // tkey = tkey / 1000 * 1000 175 | // } 176 | waitTimes[tkey]++ 177 | } 178 | 179 | summaryData := SummaryDataStruct{ 180 | CompleteRequests: summaryDataTmp.CompleteRequests, 181 | FailedRequests: summaryDataTmp.FailedRequests, 182 | SuccessRequests: summaryDataTmp.SuccessRequests, 183 | TotalDataSize: summaryDataTmp.TotalDataSize, 184 | MinUseTime: microToMilliStr(summaryDataTmp.MinUseTime), 185 | MaxUseTime: microToMilliStr(summaryDataTmp.MaxUseTime), 186 | CodeDetail: make(map[string]int), 187 | WaitingTimeDetail: make(map[string]int), 188 | 189 | MaxConn: microToMilliStr(summaryDataTmp.MaxConn), 190 | MinConn: microToMilliStr(summaryDataTmp.MinConn), 191 | MaxDNS: microToMilliStr(summaryDataTmp.MaxDNS), 192 | MinDNS: microToMilliStr(summaryDataTmp.MinDNS), 193 | MaxReq: microToMilliStr(summaryDataTmp.MaxReq), 194 | MinReq: microToMilliStr(summaryDataTmp.MinReq), 195 | MaxDelay: microToMilliStr(summaryDataTmp.MaxDelay), 196 | MinDelay: microToMilliStr(summaryDataTmp.MinDelay), 197 | MaxRes: microToMilliStr(summaryDataTmp.MaxRes), 198 | MinRes: microToMilliStr(summaryDataTmp.MinRes)} 199 | 200 | summaryData.AvgUseTime = tools.FloatToStr3f(microToMillF(summaryDataTmp.AvgUseTime) / float64(conf.Conf.UrlNum)) 201 | summaryData.AvgConn = tools.FloatToStr3f(microToMillF(summaryDataTmp.AvgConn) / float64(conf.Conf.UrlNum)) 202 | summaryData.AvgDNS = tools.FloatToStr3f(microToMillF(summaryDataTmp.AvgDNS) / float64(conf.Conf.UrlNum)) 203 | summaryData.AvgDelay = tools.FloatToStr3f(microToMillF(summaryDataTmp.AvgDelay) / float64(conf.Conf.UrlNum)) 204 | summaryData.AvgReq = tools.FloatToStr3f(microToMillF(summaryDataTmp.AvgReq) / float64(conf.Conf.UrlNum)) 205 | summaryData.AvgRes = tools.FloatToStr3f(microToMillF(summaryDataTmp.AvgRes) / float64(conf.Conf.UrlNum)) 206 | summaryData.AvgDataSize = tools.FloatToStr3f(float64(summaryDataTmp.TotalDataSize) / float64(conf.Conf.UrlNum)) 207 | 208 | for k, v := range codeDetail { 209 | summaryData.CodeDetail[strconv.Itoa(k)] = v 210 | } 211 | t := microToSecond(conf.Conf.EndTime - conf.Conf.StartTime) 212 | summaryData.TimeToken = tools.FloatToStr3f(t) 213 | summaryData.RequestsPerSec = tools.FloatToStr3f(float64(conf.Conf.UrlNum) / t) 214 | summaryData.SuccessRequestsPerSec = tools.FloatToStr3f(float64(summaryData.SuccessRequests) / t) 215 | summaryData.STransferRatePerSec = tools.FloatToStr3f(float64(summaryData.TotalDataSize) / t) 216 | 217 | tps := []float64{0.1, 0.25, 0.5, 0.75, 0.9, 0.95, 0.99, 0.999, 0.9999} 218 | tpsL := len(tps) 219 | tpsCount := make([]int, tpsL) 220 | tkeys := []int{} 221 | for i, v := range tps { 222 | tpsCount[i] = int(float64(conf.Conf.UrlNum) * v) 223 | } 224 | for k, _ := range waitTimes { 225 | tkeys = append(tkeys, int(k)) 226 | } 227 | tkeysL := len(tkeys) 228 | sort.Ints(tkeys) 229 | tmpN := 0 230 | j := 0 231 | for i := 0; i < tpsL; i++ { 232 | for { 233 | if j >= tkeysL { 234 | if _, ok := summaryData.WaitingTimeDetail[tools.FloatToPercent(tps[i])]; !ok { 235 | summaryData.WaitingTimeDetail[tools.FloatToPercent(tps[i])] = tkeys[tkeysL-1] 236 | } 237 | break 238 | } 239 | if tmpN >= tpsCount[i] { 240 | summaryData.WaitingTimeDetail[tools.FloatToPercent(tps[i])] = tkeys[j] 241 | break 242 | } 243 | tmpN = tmpN + waitTimes[int64(tkeys[j])] 244 | j++ 245 | } 246 | } 247 | summaryData.Version = conf.VERSION 248 | Print(summaryData) 249 | } 250 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------