Skip to content

Commit bb83936

Browse files
committed
fix: qps
1 parent 94e265d commit bb83936

File tree

6 files changed

+28
-12
lines changed

6 files changed

+28
-12
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ go get github.com/BUGLAN/stress
1414
stress -c 200 -n 10 -u https://www.baidu.com
1515
```
1616

17+
## 指标解释
18+
19+
* 请求数: 当前发出的所有请求数量
20+
* 成功数: 当前发出的所有请求数量中成功数量
21+
* 失败数: 当前发出的所有请求数量中失败数量
22+
* QPS: 每秒钟处理请求数量
23+
* 最长耗时: 成功请求数量里面单个请求最长耗时
24+
* 最短耗时: 成功请求数量里面单个请求最短耗时
25+
* 平均耗时: 成功请求平均耗时
26+
1727
## 感谢
1828

1929
* https://github.com/link1st/go-stress-testing

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func main() {
4141

4242
ch := make(chan *model.ReqResult, 200)
4343
wgReceive.Add(1)
44-
go server.ReceiveData(ch, &wgReceive)
44+
go server.ReceiveData(ch,startTime, &wgReceive)
4545

4646
for i := 0; i < model.Coroutines; i++ {
4747
wg.Add(1)

server/statistic.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package server
22

33
import (
44
"fmt"
5-
"math"
65
"sync"
76
"time"
87

98
"github.com/BUGLAN/stress/model"
109
)
1110

12-
func ReceiveData(ch chan *model.ReqResult, wg *sync.WaitGroup) {
11+
func ReceiveData(ch chan *model.ReqResult, startTime time.Time, wg *sync.WaitGroup) {
1312
defer wg.Done()
1413
stopChan := make(chan struct{})
1514
ticker := time.NewTicker(time.Second * 1)
@@ -26,17 +25,18 @@ func ReceiveData(ch chan *model.ReqResult, wg *sync.WaitGroup) {
2625
avgTime float64 // 平均请求耗时
2726
concurrentNum uint64 // 并发数
2827
currRequestNum uint64 // 当前请求数
28+
elapsedTime int // 耗时
2929
)
3030

3131
// 排除minTime为0的情况
32-
minTime = math.MaxFloat64
3332

3433
go func() {
3534
for {
3635
select {
3736
case <-ticker.C:
37+
elapsedTime += 1
3838
// 定时输出相应的指标
39-
out(totalProcessTime, concurrentNum, totalSuccessNum, totalFailureNum, qps, maxTime, minTime, avgTime)
39+
out(elapsedTime, concurrentNum, totalSuccessNum, totalFailureNum, qps, maxTime, minTime, avgTime)
4040
case <-stopChan:
4141
fmt.Println()
4242
return
@@ -59,7 +59,9 @@ func ReceiveData(ch chan *model.ReqResult, wg *sync.WaitGroup) {
5959
// fmt.Printf("minTime %f\n", minTime)
6060

6161
// 最小耗时
62-
if data.ProcessTime < minTime {
62+
if minTime == 0 {
63+
minTime = data.ProcessTime
64+
} else if data.ProcessTime < minTime {
6365
minTime = data.ProcessTime
6466
}
6567

@@ -79,7 +81,7 @@ func ReceiveData(ch chan *model.ReqResult, wg *sync.WaitGroup) {
7981
avgTime = totalProcessTime / float64(concurrentNum)
8082

8183
// qps
82-
qps = 1e9 / avgTime
84+
qps = float64(totalSuccessNum) / float64(elapsedTime)
8385

8486
}
8587

@@ -92,18 +94,18 @@ func ReceiveData(ch chan *model.ReqResult, wg *sync.WaitGroup) {
9294
_ = concurrentNum
9395
_ = currRequestNum
9496
// 最后的输出到控制台 传入
95-
out(totalProcessTime, concurrentNum, totalSuccessNum, totalFailureNum, qps, maxTime, minTime, avgTime)
97+
out(elapsedTime, concurrentNum, totalSuccessNum, totalFailureNum, qps, maxTime, minTime, avgTime)
9698

9799
}
98100

99101
func tableHeader() {
100102
fmt.Printf("\n")
101-
fmt.Println(" 耗时│ 并发数│ 成功数│ 失败数│ QPS │最长耗时│最短耗时│平均耗时│ 错误码")
102-
fmt.Println("─────┼───────┼───────┼───────┼────────┼────────┼────────┼────────┼────────")
103+
fmt.Println(" 耗时│ 请求数│ 成功数│ 失败数│ QPS │最长耗时│最短耗时│平均耗时")
104+
fmt.Println("─────┼───────┼───────┼───────┼────────┼────────┼────────┼────────")
103105
}
104106

105107
// out 输出到控制台 单位为纳秒(ns)
106-
func out(totalProcessTime float64, concurrentNum, totalSuccessNum, totalFailureNum uint64, qps, maxTime, minTime, avgTime float64) {
107-
fmt.Printf("%4.2fs│%7d│%7d│%7d│%8.2f│%8.2fs│%8.2fs│%8.2fs│错误码\n", totalProcessTime/1e9, concurrentNum, totalSuccessNum, totalFailureNum, qps, maxTime/1e9, minTime/1e9, avgTime/1e9)
108+
func out(elapsedTime int, concurrentNum, totalSuccessNum, totalFailureNum uint64, qps, maxTime, minTime, avgTime float64) {
109+
fmt.Printf("%4ds│%7d│%7d│%7d│%8.2f│%7.2fs│%7.2fs│%7.2fs \n", elapsedTime, concurrentNum, totalSuccessNum, totalFailureNum, qps, maxTime/1e9, minTime/1e9, avgTime/1e9)
108110

109111
}

server/work.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ func Process(url string, ch chan *model.ReqResult, requestTime int64, wg *sync.W
3535
}
3636
}
3737

38+
// verify 验证 进行验证 (errcode)
39+
3840
ch <- &model.ReqResult{
3941
IsSuccess: isSuccess,
4042
StatusCode: 200,

verify/errcode.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package verify

verify/status.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package verify

0 commit comments

Comments
 (0)