@@ -2,6 +2,7 @@ package pkg
22
33import (
44 "fmt"
5+ "github.com/oldthreefeng/stress/utils"
56 "strings"
67 "sync"
78 "time"
1516// ReceivingResults is 接收结果并处理
1617// 统计的时间都是纳秒,显示的时间 都是毫秒
1718// concurrent 并发数
18- func ReceivingResults (concurrent uint64 , ch <- chan * RequestResults , wg * sync.WaitGroup ) {
19+ func ReceivingResults (concurrent int , ch <- chan * RequestResults , wg * sync.WaitGroup ) {
1920
2021 defer func () {
2122 wg .Done ()
@@ -34,13 +35,13 @@ func ReceivingResults(concurrent uint64, ch <-chan *RequestResults, wg *sync.Wai
3435 successNum uint64 // 成功处理数,code为0
3536 failureNum uint64 // 处理失败数,code不为0
3637 chanIdLen int // 并发数
37- chanIds = make ( map [ uint64 ] bool )
38+ chanIds = utils . New ( Concurrency )
3839 )
3940
4041 statTime := uint64 (time .Now ().UnixNano ())
4142
4243 // 错误码/错误个数
43- var errCode = make ( map [ int ] int )
44+ var errCode = utils . New ( )
4445
4546 // 定时输出一次计算结果
4647 ticker := time .NewTicker (exportStatisticsTime )
@@ -83,16 +84,18 @@ func ReceivingResults(concurrent uint64, ch <-chan *RequestResults, wg *sync.Wai
8384 }
8485
8586 // 统计错误码
86- if value , ok := errCode [data .ErrCode ]; ok {
87- errCode [data .ErrCode ] = value + 1
87+ strErrCode := fmt .Sprintf ("%d" , data .ErrCode )
88+ if value , ok := errCode .Get (strErrCode ); ok {
89+ errCode .Set (strErrCode , value .(int )+ 1 )
8890 } else {
89- errCode [ data . ErrCode ] = 1
91+ errCode . Set ( strErrCode , 1 )
9092 }
91-
92- if _ , ok := chanIds [ data . ChanId ] ; ! ok {
93- chanIds [ data . ChanId ] = true
93+ strChanId := fmt . Sprintf ( "%d" , data . ChanId )
94+ if _ , ok := chanIds . Get ( strChanId ) ; ! ok {
95+ chanIds . Set ( strChanId , true )
9496 chanIdLen = len (chanIds )
9597 }
98+
9699 }
97100
98101 // 数据全部接受完成,停止定时输出统计数据
@@ -117,7 +120,7 @@ func ReceivingResults(concurrent uint64, ch <-chan *RequestResults, wg *sync.Wai
117120}
118121
119122// 计算数据
120- func calculateData (concurrent , processingTime , requestTime , maxTime , minTime , successNum , failureNum uint64 , chanIdLen int , errCode map [ int ] int ) {
123+ func calculateData (concurrent int , processingTime , requestTime , maxTime , minTime , successNum , failureNum uint64 , chanIdLen int , errCode utils. ConcurrentMap ) {
121124 if processingTime == 0 {
122125 processingTime = 1
123126 }
@@ -132,12 +135,12 @@ func calculateData(concurrent, processingTime, requestTime, maxTime, minTime, su
132135
133136 // 平均 每个协程成功数*总协程数据/总耗时 (每秒)
134137 if processingTime != 0 {
135- qps = float64 (successNum * 1e9 * concurrent ) / float64 (processingTime )
138+ qps = float64 (successNum * 1e9 * uint64 ( concurrent ) ) / float64 (processingTime )
136139 }
137140
138141 // 平均时长 总耗时/总请求数/并发数 纳秒=>毫秒
139142 if successNum != 0 && concurrent != 0 {
140- averageTime = float64 (processingTime ) / float64 (successNum * 1e6 * concurrent )
143+ averageTime = float64 (processingTime ) / float64 (successNum * 1e6 * uint64 ( concurrent ) )
141144 }
142145
143146 // 纳秒=>毫秒
@@ -166,7 +169,7 @@ func header() {
166169}
167170
168171// 打印表格
169- func table (successNum , failureNum uint64 , errCode map [ int ] int , qps , averageTime , maxTimeFloat , minTimeFloat , requestTimeFloat float64 , chanIdLen int ) {
172+ func table (successNum , failureNum uint64 , errCode utils. ConcurrentMap , qps , averageTime , maxTimeFloat , minTimeFloat , requestTimeFloat float64 , chanIdLen int ) {
170173 // 打印的时长都为毫秒
171174 result := fmt .Sprintf ("%4.0fs│%7d│%7d│%7d│%8.2f│%8.2f│%8.2f│%8.2f│%v" , requestTimeFloat , chanIdLen , successNum , failureNum , qps , maxTimeFloat , minTimeFloat , averageTime , printMap (errCode ))
172175 fmt .Println (result )
@@ -175,13 +178,13 @@ func table(successNum, failureNum uint64, errCode map[int]int, qps, averageTime,
175178}
176179
177180// 输出错误码、次数 节约字符(终端一行字符大小有限)
178- func printMap (errCode map [ int ] int ) (mapStr string ) {
181+ func printMap (errCode utils. ConcurrentMap ) (mapStr string ) {
179182
180183 var (
181184 mapArr []string
182185 )
183- for key , value := range errCode {
184- mapArr = append (mapArr , fmt .Sprintf ("%d :%d" , key , value ))
186+ for key , value := range errCode . Items () {
187+ mapArr = append (mapArr , fmt .Sprintf ("%s :%d" , key , value ))
185188 }
186189
187190 mapStr = strings .Join (mapArr , ";" )
0 commit comments